PHP 8.0: NewMagicClassConstant: detect ::class on objects by jrfnl · Pull Request #1166 · PHPCompatibility/PHPCompatibility

@jrfnl

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