Skip to content

4.0 | Let T_STATIC be T_STATIC #22

Closed
@jrfnl

Description

@jrfnl

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 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.

https://github.com/squizlabs/PHP_CodeSniffer/blob/9cce6859a595b3fa77fc823d9b0d451951dcf9d2/src/Tokenizers/PHP.php#L2086-L2101

Code sample

if ($geometry instanceof static) {
    echo 'foo';
}

static in the above code sample is tokenized as T_STRING, not T_STATIC.


Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions