PHP 8.0 | Add utilities to handle the nullsafe object operator #176
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.
Collections: deprecate $objectOperators in favour of objectOperators()
Deprecate the
$objectOperators
property in favour of a newCollections::objectOperators()
method.PHP 8.0 introduces a new
T_NULLSAFE_OBJECT_OPERATOR
token. As that token will not always be available, a property can't handle this.In anticipation of the introduction of the new token, I'm deprecating the
Collections::$objectOperators
property straight away to prevent BC breaks at a later point in time.Includes adding unit tests for the new method.
Collections::objectOperators(): support the PHP 8 nullsafe object operator
Includes adjusted unit test.
Tokens\Collections: add new `objectOperatorsBC() method
Sister-method to the
objectOperators()
method, which returns the tokens which can be encountered as object operators in PHP < 8.0 in combination with a PHPCS version in which the token was not backfilled yet.The backfill for PHPCS has been pulled and is expected to be included in PHPCS 3.5.6 or 3.6.0, but as it stands, is not available yet in a released version.
Includes unit test.
Related:
Tokens\Collections: add new `nullsafeObjectOperatorBC() method
... to retrieve the tokens which can represent the nullsafe object operator.
This method will return the appropriate token(s) based on the PHP/PHPCS version used.
Includes unit test.
New Operators::isNullsafeObjectOperator() method
... which emulates the backfill as pulled to PHPCS itself.
PHP 8 introduces a new object chaining operator
?->
which short-circuits moving to the next expression if the left-hand side evaluates tonull
.This operator can not be used in write-context, but that is not the concern of this method.
For PHPCS versions where the backfill is not available yet, this method can be used to determine whether a
?
or->
token is actually part of the?->
nullsafe object operator token.Includes perfunctory unit tests.
Includes mentioning the method in appropriate places in the documentation elsewhere.
Refs:
Namespaces::getType: switch to objectOperators() method
... and add a unit test using the nullsafe object operator.
No additional code is needed for the correct functioning of this method, as when the nullsafe object operator is not yet backfilled, the method will correctly signal on the object operator part of the token.