Description
Repost from squizlabs/PHP_CodeSniffer#3115:
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 theinstanceof
operator.This defies the principle of least astonishment as both
self
andparent
in combination withinstanceof
are tokenized asT_SELF
andT_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 asT_STATIC
when preceded byinstanceof
.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 theSquiz.WhiteSpace.ScopeKeywordSpacing
sniff, not in the Tokenizer, same as is done in the sniff with other typical late static binding occurrences.Code sample
if ($geometry instanceof static) { echo 'foo'; }
static
in the above code sample is tokenized asT_STRING
, notT_STATIC
.