-
-
Notifications
You must be signed in to change notification settings - Fork 77
Generic/ConstructorName: fix minor bug #649
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: fix minor bug #649
Conversation
This commit fixes a minor bug in `Generic.NamingConventions .ConstructorName` when checking if a given class has a parent. The code was checking if the lower case version of the value returned by `File::findExtendedClassName()`` is `false`. The problem is that `strtolower()` never returns `false`, it always returns a `string`. Thus, the condition would never evaluate to `true` and the sniff would not bail at this point when a given class has no parent. This did not cause any issues to the sniff, even for invalid code, as there is not a scenario where a class method can have a `T_DOUBLE_COLON` token followed by a `T_STRING` token with an empty `content`. This is true even for empty strings as PHPCS includes the quotes in the `content`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rodrigoprimo Thanks for this PR. Good catch!
I only have a little feedback on the analysis (but the conclusion is still the same):
This is true even for empty strings as PHPCS includes the quotes in the
content
.
I'm not sure I understand what you are trying to say here ?
The content
of T_STRING
tokens will not include quotes, but AFAIK, the content
of a T_STRING
token can never be empty, as in that case PHP wouldn't tokenize it as T_STRING
.
So while this is a "sniff code" bug, it is not a bug which would have affected userland.
I've also marked this change now as a performance improvement as it allows the sniff to bow out earlier and do less work.
Looking at the code I do see a completely different bug, but that's outside the scope of this PR. ;-)
I've opened PR #652 to address the other bug(s) I saw. |
I should have provided a code sample to illustrate my point as I was trying to check if it was possible for code to trigger a false positive for this sniff, and I crafted the following invalid code: class ClassName {
public function __construct() {
}
public function ClassName() {
ClassName::''();
}
} In this particular case, the |
@rodrigoprimo I see what you mean, but there are limits to the amount of fantasy code PHPCS should support ;-) |
Description
This PR fixes a minor bug in
Generic.NamingConventions .ConstructorName
when checking if a given class has a parent. The code was checking if the lowercase version of the value returned byFile::findExtendedClassName()
isfalse
. The problem is thatstrtolower()
never returnsfalse
. It always returns astring
. Thus, the condition would never be evaluated astrue
, and the sniff would not bail at this point when a given class has no parent.As far as I can check, this did not cause any issues to the sniff, even for invalid code, as there is not a scenario where a class method can have a
T_DOUBLE_COLON
token followed by aT_STRING
token with an emptycontent
. This is true even for empty strings as PHPCS includes the quotes in thecontent
.Types of changes
PR checklist