PHP 8.0: NewMagicClassConstant: detect ::class on objects #1166
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refs:
This adjusts the existing
PHPCompatibility.Constants.NewMagicClassConstantsniff to differentiate between::classbeing used on class names/self/parent/static, as allowed per PHP 5.5 and::classbeing 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
$varin$var::classcould 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
::classon 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