-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
The static keyword in PHP is used for a number of different things:
- Static properties
- Static methods
- Static closures and arrow functions
- Static variables
- Late static binding
Now, for all these uses, the token is tokenized as T_STATIC, with one exception: late static binding in combination with the instanceof operator.
This defies the principle of least astonishment as both self and parent in combination with instanceof are tokenized as T_SELF and T_PARENT respectively.
I've been caught out by this a couple of times already and I imagine that others have too, or if they haven't, that sniffs may be throwing false positives/negatives because of it.
It's not completely clear to me why this very specific exception was put in place, but I'd like to propose to remove this exception in PHPCS 4.x and let the static keyword be tokenized as T_STATIC when preceded by instanceof.
Based on a history search, it looks to have been put in place in response to PEAR#19726.
In my opinion, that issue should have been fixed in the sniff code for the Squiz.WhiteSpace.ScopeKeywordSpacing sniff, not in the Tokenizer, same as is done in the sniff with other typical late static binding occurrences.
PHP_CodeSniffer/src/Tokenizers/PHP.php
Lines 2086 to 2101 in 9cce685
| } else if ($this->tokens[$i]['code'] === T_STATIC) { | |
| for ($x = ($i - 1); $x > 0; $x--) { | |
| if (isset(Util\Tokens::$emptyTokens[$this->tokens[$x]['code']]) === false) { | |
| break; | |
| } | |
| } | |
| if ($this->tokens[$x]['code'] === T_INSTANCEOF) { | |
| $this->tokens[$i]['code'] = T_STRING; | |
| $this->tokens[$i]['type'] = 'T_STRING'; | |
| if (PHP_CODESNIFFER_VERBOSITY > 1) { | |
| $line = $this->tokens[$i]['line']; | |
| echo "\t* token $i on line $line changed from T_STATIC to T_STRING".PHP_EOL; | |
| } | |
| } |
Code sample
if ($geometry instanceof static) {
echo 'foo';
}static in the above code sample is tokenized as T_STRING, not T_STATIC.