Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.0 | Add utilities to handle the nullsafe object operator #176

Merged
merged 6 commits into from
Aug 7, 2020

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Aug 7, 2020

Collections: deprecate $objectOperators in favour of objectOperators()

Deprecate the $objectOperators property in favour of a new Collections::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.

⚠️ BC-break: This needs a changelog entry ⚠️

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

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.

jrfnl added 6 commits August 7, 2020 00:59
Deprecate the `$objectOperators` property in favour of a new `Collections::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.
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 in yet in a released version.

Includes unit test.

Related:
* squizlabs/PHP_CodeSniffer 3046
... 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.

Related:
* squizlabs/PHP_CodeSniffer 3046
... 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 to `null`.

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:
* squizlabs/PHP_CodeSniffer 3046
* https://wiki.php.net/rfc/nullsafe_operator
* php/php-src@9bf1198
... 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.
@jrfnl jrfnl added this to the 1.0.0-alpha4 milestone Aug 7, 2020
@jrfnl jrfnl merged commit 52530d7 into develop Aug 7, 2020
@jrfnl jrfnl deleted the feature/php8-nullsafe-operator branch August 7, 2020 15:40
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.

1 participant