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

Generic/ConstructorName: bug fix x 2 #652

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 30, 2024

Description

Generic/ConstructorName: bug fix - allow for unconventional spacing and comments

When determining whether or not a "parent" constructor was being called using a call to a PHP4-style constructor, the sniff would only look at the next token after a double colon, while the next token may be whitespace or a comment, which should be ignored.

Fixed now by making the sniff check for the next non empty token instead.

Includes tests.

Generic/ConstructorName: bug fix - false positive on static call to other class

When determining whether or not a "parent" constructor was being called using a call to a PHP4-style constructor, the sniff would only look at the next token after a double colon, disregarding completely the class the method is being called on.

This would lead to false positives for method calls to other classes, which would just happen to have a method named the same as the class being extended.

This commit fixes this by verifying that the previous non-empty token before the double colon is either a hierarchy keyword or the name of the class being extended and only throwing an error if that's the case.

Includes tests.

Generic/ConstructorName: minor efficiency tweak

As things were, the $startIndex would be the function keyword in the function declaration, which means that the complete declaration (parameters, defaults etc) would be walked, while we only really want to look inside the function body.

Fixed by starting the while loop on the scope_opener instead.

Additionally, while the $startIndex would be moved forward on each loop, it would still examine one token too much (scope_opener cannot be a T_DOUBLE_COLON, T_STRING after T_DOUBLE_COLON cannot be a T_DOUBLE_COLON).
Also fixed now.

Suggested changelog entry

  • Fixed Generic.NamingConventions.ConstructorName: false positives for PHP-4 style calls to PHP-4 style parent constructor when a method with the same name as the parent class was called on another class.
  • Fixed Generic.NamingConventions.ConstructorName: false negatives for PHP-4 style calls to parent constructor for function calls with whitespace and comments in unconventional places.

Related issues/external references

Inspired by #649

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

…nd comments

When determining whether or not a "parent" constructor was being called using a call to a PHP4-style constructor, the sniff would only look at the _next_ token after a double colon, while the next token may be whitespace or a comment, which should be ignored.

Fixed now by making the sniff check for the _next non empty_ token instead.

Includes tests.
…ther class

When determining whether or not a "parent" constructor was being called using a call to a PHP4-style constructor, the sniff would only look at the _next_ token after a double colon, disregarding completely the class the method is being called on.

This would lead to false positives for method calls to other classes, which would just happen to have a method named the same as the class being extended.

This commit fixes this by verifying that the previous non-empty token before the double colon is either a hierarchy keyword or the name of the class being extended and only throwing an error if that's the case.

Includes tests.
As things were, the `$startIndex` would be the `function` keyword in the function declaration, which means that the complete declaration (parameters, defaults etc) would be walked, while we only really want to look _inside_ the function body.

Fixed by starting the `while` loop on the `scope_opener` instead.

Additionally, while the `$startIndex` would be moved forward on each loop, it would still examine one token too much (`scope_opener` cannot be a `T_DOUBLE_COLON`, `T_STRING` after `T_DOUBLE_COLON` cannot be a `T_DOUBLE_COLON`).
Also fixed now.
Copy link
Contributor

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants