-
-
Notifications
You must be signed in to change notification settings - Fork 91
PSR2.Classes.PropertyDeclaration: add T_STATIC to tokens list #1360
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
base: 4.x
Are you sure you want to change the base?
Conversation
In addition to scope modifiers, class properties currently have five modifier tokens: T_ABSTRACT, T_FINAL, T_READONLY, T_STATIC and T_VAR. The blame for this sniff does not explain why all but T_STATIC were added to the set of modifier tokens in the search list. T_VAR has been present since the file's creation and the other three were added one at a time as those modifiers were added. To that extent, perhaps T_STATIC was missed because the sniff itself uses getMemberProperties() and code to perform explicit checks on those properties? I'm not sure.
|
I wanted to add a test, but I honestly don't understand why the existing tests aren't flagging this (unless - as mentioned above - the rest of the code is compensating). 😞 |
|
@jrchamp Thanks for this PR. I'm looking at the code now and as far as I can see, the PHP_CodeSniffer/src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php Lines 70 to 73 in 8a54aaa
In other words, it's trying to find the previous variable if public $propA = null, $propB = 10, $propC;Now, whether or not Consider the following code class Foo {
var $propA;
static $propB; // <= The `$prev` token for `$stackPtr = $propB` would become the `T_SEMICOLON` on the line before instead of `T_STATIC`, but in both cases, it would be correctly recognized as not part of a multi-property declaration.
}
class Bar {
static $propB; // <= The `$prev` token for `$stackPtr = $propB` would become the `T_OPEN_CURLY_BRACKET` on the line before instead of `T_STATIC`, but in both cases, it would be correctly recognized as not part of a multi-property declaration.
}
// And even for classes using unconventional class order, it isn't a problem:
class Foo {
public function bar() {
$var = 10;
}
static $prop = 10; // <= The `$prev` token for `$stackPtr = $prop` would become the `T_SEMICOLON` within the function body.
}
class Foo {
abstract public function bar($param);
static $prop = 10; // <= The `$prev` token for `$stackPtr = $prop` would become the `T_SEMICOLON` of the function.
}
class Foo {
public function bar() {}
static $prop = 10; // <= The `$prev` token for `$stackPtr = $prop` would become the `T_OPEN_CURLY_BRACKET` of the function.
}And even if the class uses property hooks, (though that is not currently supported at all yet in PHPCS), it would still not get confused over the class Foo {
public string $lc {
set {
$this->lc = strtolower($value);
}
}
static $propA; // <= The `$prev` token for `$stackPtr = $propA` would become the `T_SEMICOLON` for the statement in `set()`.
public string $fullName { get => $this->first . " " . $this->last; }
static $propB; // <= The `$prev` token for `$stackPtr = $propB` would become the `T_SEMICOLON` for the statement in `get()`.
}Does this help explain sufficiently why the sniff was working correctly, even though it was missing the And yes, considering the above analysis, adding extra tests is not strictly necessary as this case is already covered by existing tests, like the test on line 10. |
jrfnl
left a comment
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.
@jrchamp Thanks for this PR!
I'm marking this as an efficiency improvement, not a bug, as the sniff behaviour is effectively unchanged. Agreed ?
I will also adjust the changelog entry to reflect that.
|
@jrchamp Thinking this over some more, I think this check can be simplified significantly by just checking the token code of the previous non-empty token before the I can't think of any code sample (safe for possibly property hooks, but that's outside of the scope of this PR) in which this would break. Can you ? That should allow for removing the complete What do you think ? |
|
@jrchamp Just checking in to see if my review/feedback notes were clear and if you intend to continue with this PR. No pressure, just want to make sure there's nothing blocking you. |
|
I agree, the reason this "bug" wasn't caught before is because the code works even without most of the special modifier tokens in the find array. Sorry I didn't get back to you sooner - I've been busy. It seems like you know exactly how you would like to fix this, so please move forward as I feel like my attempt to implement it would take more of your limited time ❤️ |
Description
In addition to scope modifiers, class properties currently have five modifier tokens: T_ABSTRACT, T_FINAL, T_READONLY, T_STATIC and T_VAR. The blame for this sniff does not explain why all but T_STATIC were added to the set of modifier tokens in the search list. T_VAR has been present since the file's creation and the other three were added one at a time as those modifiers were added. To that extent, perhaps T_STATIC was missed because the sniff itself uses getMemberProperties() and code to perform explicit checks on those properties? I'm not sure.
Suggested changelog entry
PSR2.Classes.PropertyDeclaration: add T_STATIC to tokens list
Related issues/external references
Found while discussing #1359
Types of changes
PR checklist