Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 11, 2020

It is now possible to fetch the class name of an object using $object::class. The result is the same as get_class($object).

Refs:

This adjusts the existing PHPCompatibility.Constants.NewMagicClassConstant sniff to differentiate between ::class being used on class names/self/parent/static, as allowed per PHP 5.5 and ::class being used on objects, as allowed per PHP 8.0.

Note: there are a number of syntaxes which are still not allowed (and which don't make any sense anyhow). These would cause a fatal error when used in PHP.

Differentiating between what is allowed and what isn't, is pretty hard though and realistically beyond the capabilities of a static analysis tool as $var in $var::class could be either an object or a literal, with the first being allowed, while the second isn't.

So I have taken the executive decision that, for now, everything which wasn't yet explicitly allowed per PHP 5.5 will throw an error for PHP < 8, independently of whether it is now supported or still forbidden.

In a way, this is no different from how the sniff behaved before, as previously, using ::class on any context would throw an error for PHP < 5.5, independently of whether the used syntax was actually supported in PHP 5.5+.

Includes unit tests.

Includes minor fixes to the existing unit tests.

Related to #809

> It is now possible to fetch the class name of an object using `$object::class`. The result is the same as `get_class($object)`.

Refs:
* https://wiki.php.net/rfc/class_name_literal_on_object
* php/php-src#5065
* php/php-src@d933591

This adjusts the existing `PHPCompatibility.Constants.NewMagicClassConstant` sniff to differentiate between `::class` being used on class names/`self`/`parent`/`static`, as allowed per PHP 5.5 and `::class` being used on objects, as allowed per PHP 8.0.

Note: there are a number of syntaxes which are still not allowed (and which don't make any sense anyhow). These would cause a fatal error when used in PHP.

Differentiating between what is allowed and what isn't, is pretty hard though and realistically beyond the capabilities of a static analysis tool as `$var` in `$var::class` could be either an object or a literal, with the first being allowed, while the second isn't.

So I have taken the executive decision that, for now, everything which wasn't yet explicitly allowed per PHP 5.5 will throw an error for PHP < 8, independently of whether it is now supported or still forbidden.

In a way, this is no different from how the sniff behaved before, as previously, using `::class` on any context would throw an error for PHP < 5.5, independently of whether the used syntax was actually supported in PHP 5.5+.

Includes unit tests.

Includes minor fixes to the existing unit tests.
@wimg wimg merged commit e4bfd01 into develop Aug 9, 2020
@wimg wimg deleted the php-8.0/newmagicclassconstant-detect-use-on-object branch August 9, 2020 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants