Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File: add new File::findExtendedInterfaceNames() utility method #2128

Closed

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Aug 20, 2018

As I've just looked at the findExtendedClassName() method again for PR #2127, I remembered that @SenseException reported another issue with it a while back via Twitter: https://twitter.com/SenseException/status/997988691468484609.

Summary: interfaces can extend more than one interface (in contrast to classes) and the findExtendedClassName() method does not allow for that.
See: http://php.net/manual/en/language.oop5.interfaces.php#example-208

To allow for this from within the existing findExtendedClassName() method would change the function signature from @return string|false to @return string|array|false, thus breaking the API.

So, instead, I've elected to add a new File::findExtendedInterfaceNames() method and to recommend using that method for interfaces in the documentation of the findExtendedClassName() method .

As the logic needed for these two methods, as well as for the findImplementedInterfaces() method, is basically the same, I've extracted the logic out to a private File::examineObjectDeclarationSignature() method which is now called by all three functions to DRY out the code.

Includes dedicated unit tests for the new method.

Copy link

@SenseException SenseException left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your effort. I took a small look at the changes and wonder, if we need a new method or if findImplementedInterfaceNames could be used with an interface too. I assume you chose a new method because of the clearer naming and for extending the File class with a new public method instead of changing existing method logic. I think I prefer that too even though File gets bigger by it.

Any thoughts by other reviewers?

src/Files/File.php Outdated Show resolved Hide resolved
if ($name === '') {
$interfaces = $this->examineObjectDeclarationSignature($stackPtr, $validStructures, T_EXTENDS);

if (empty($interfaces) === true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this is for consistency, but would returning an empty array be better than returning false in the if-block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the end-user (= sniff developers) point of view, the difference is negligible, if (empty($interfacesNames)) will catch both false as well as en empty array.

What's better is a matter of opinion and in this case, I've elected consistency with the similar methods over always having the same type of return.

It can be adjusted easily enough and I'd be happy to do so if @gsherwood would deem always returning an array appropriate.

src/Files/File.php Outdated Show resolved Hide resolved
jrfnl added a commit to jrfnl/WordPress-Coding-Standards that referenced this pull request Aug 22, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
@jrfnl
Copy link
Contributor Author

jrfnl commented Aug 22, 2018

... and wonder, if we need a new method or if findImplementedInterfaceNames could be used with an interface too. I assume you chose a new method because of the clearer naming and for extending the File class with a new public method instead of changing existing method logic. I think I prefer that too even though File gets bigger by it.

In my opinion, findImplementedInterfaceNames() can/should not be adjusted for this.

findImplementedInterfaceNames() checks that the token passed is a (anonymous) class and looks for the T_IMPLEMENTS keyword in the class declaration.

The new findExtendedInterfaceNames() method checks that the token passed is a T_INTERFACE and checks for the T_EXTENDS keyword in the interface declaration.

So as both the variables are different, I don't think it makes sense to try and get this into one method.

@jrfnl jrfnl force-pushed the feature/fix-findextendedclass-method branch from 946fab1 to 45db0c4 Compare August 22, 2018 02:28
As I've just looked at the `findExtendedClassName()` method again for PR 2127, I remembered that SenseException reported another issue with it a while back via Twitter: https://twitter.com/SenseException/status/997988691468484609.

Summary: interfaces can extend more than one interface (in contrast to classes) and the `findExtendedClassName()` method does not allow for that.
See: http://php.net/manual/en/language.oop5.interfaces.php#example-208

To allow for this from within the existing `findExtendedClassName()` method would change the function signature from `@return string|false` to `@return string|array|false`, thus breaking the API.

So, instead, I've elected to add a new `File::findExtendedInterfaceNames()` method and to recommend using that method for interfaces in the documentation of the `findExtendedClassName()` method .

As the logic needed for these two methods, as well as for the `findImplementedInterfaces()` method, is basically the same, I've extracted the logic out to a private `File::examineObjectDeclarationSignature()` method which is now called by all three functions to DRY out the code.

Includes dedicated unit tests for the new method.
@jrfnl jrfnl force-pushed the feature/fix-findextendedclass-method branch from 45db0c4 to dcc8223 Compare August 22, 2018 02:32
jrfnl added a commit to jrfnl/WordPress-Coding-Standards that referenced this pull request Dec 17, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
jrfnl added a commit to jrfnl/WordPress-Coding-Standards that referenced this pull request Dec 17, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
jrfnl added a commit to WordPress/WordPress-Coding-Standards that referenced this pull request Dec 18, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
jrfnl added a commit to WordPress/WordPress-Coding-Standards that referenced this pull request Dec 19, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
jrfnl added a commit to WordPress/WordPress-Coding-Standards that referenced this pull request Dec 19, 2018
…multiple interfaces

Interfaces can extend multiple other interfaces. Looking at the sniff code, no changes are needed in the sniff to account for this, however, adding an additional unit test to warrant against sniff changes in the future which may not take this into account.

N.B.: I've verified all other usages of the `T_INTERFACE` token in the whole of WPCS and this was the only one which looked at `extends/implements`, so the only one for which interfaces extending multiple other interfaces could be problematic.

See:
* http://php.net/manual/en/language.oop5.interfaces.php#example-208

Loosely related to:
* squizlabs/PHP_CodeSniffer#2128
@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 10, 2019

If the utility method refactor will in fact become PHPCS 3.5.0, this PR should probably be ignored until then and will be addressed and included in the refactor instead.

@jrfnl
Copy link
Contributor Author

jrfnl commented May 23, 2019

As discussed in #2189, this commit is included within #2456.

@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 30, 2020

This has been addressed and published now in PHPCSUtils. For more context, see #2456 (comment)

@jrfnl jrfnl closed this Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants