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 support for union types #168

Merged
merged 12 commits into from
Jul 17, 2020
Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 17, 2020

This PR effectively adds support for union type declarations to all the PHPCSUtils native functionality.

  • FunctionDeclarations::getParameters()
  • FunctionDeclarations::getProperties()
  • Variables::getMemberProperties()
  • FunctionDeclarations::isArrowFunction()
  • FunctionDeclaraions::getArrowFunctionOpenClose()

In anticipation of how these changes will be pulled upstream, the Collections::$parameterTypeTokens, Collections::$propertyTypeTokens and Collections::$returnTypeTokens properties are now deprecated in favour of methods by the same name, i.e. Collections::parameterTypeTokens(), Collections::propertyTypeTokens() and Collections::returnTypeTokens().
The deprecated properties will be removed before the final 1.0.0 version of PHPCSUtils is tagged.

The changes are accompanied by ample unit tests.

Ref: https://wiki.php.net/rfc/union_types_v2

Commit Details

Collections::propertyTypeTokensBC(): uncouple from the parameterTypeTokensBC() method

Collections: deprecate $parameterTypeTokens in favour of parameterTypeTokens()

Deprecate the $parameterTypeTokens property in favour of a new Collections::parameterTypeTokens() method.

PHPCS is expected to introduce a new T_TYPE_UNION token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the Collections::$parameterTypeTokens property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.

PHP 8.0 | Collection::parameterTypeTokensBC: add support for union types

Includes adjusted unit test.

Collections: deprecate $propertyTypeTokens in favour of propertyTypeTokens()

Deprecate the $propertyTypeTokens property in favour of a new Collections::propertyTypeTokens() method.

PHPCS is expected to introduce a new T_TYPE_UNION token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the Collections::$propertyTypeTokens property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.

PHP 8.0 | Collection::propertyTypeTokensBC: add support for union types

Includes adjusted unit test.

Collections: deprecate $returnTypeTokens in favour of returnTypeTokens()

Deprecate the $returnTypeTokens property in favour of a new Collections::returnTypeTokens() method.

PHPCS is expected to introduce a new T_TYPE_UNION token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the Collections::$returnTypeTokens property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.

PHP 8.0 | Collection::returnTypeTokensBC: add support for union types

In addition to the other tokens needed to support union types, the returnTypeTokens[BC]() methods also needs the T_ARRAY token as the array keyword in union type return types is not always correctly re-tokenized to T_STRING.

A change to the PHPCS tokenizer to mitigate this will be pulled upstream, but until that PR has been merged, T_ARRAY will continue to be needed.

This changes the versions supported by the returnTypeTokens() method from "minimum PHPCS 3.5.4" to "minimum 3.3.0", which also brings the method more in line with the parameterTypeTokens() and the propertyTypeTokens() methods.

Includes adjusted unit tests.

FunctionDeclarations::getParameters(): add support for PHP 8 "union" types

This commit adds support for union types to the FunctionDeclarations::getParameters() method.

Changes along the same lines as included in this PR will also be pulled upstream to PHPCS itself. Once the upstream PR has been merged, those changes will also be applied to the BCFile::getMethodParameters() method.

Includes extensive unit tests.

Ref: https://wiki.php.net/rfc/union_types_v2

FunctionDeclarations::getProperties(): add tests verifying support for PHP 8 "union" types

The commit which added union type support to the Collections::returnTypeTokens[BC]() methods implicitly added support for union types to the FunctionDeclarations::getProperties() method.

This commit adds extensive unit tests to safeguard this support.

The same commit also silently added support for union types to the BCFile::getMethodProperties() method. Union types are not supported yet in PHPCS itself and until they are, the fact that the BCFile::getMethodProperties() method supports them should be regarded as an artefact and not as official support.

Variables::getMemberProperties(): add tests verifying support for PHP 8 "union" types

The commit which added union type support to the Collections::propertyTypeTokens[BC]() methods implicitly added support for union types to the Variables::getMemberProperties() method.

This commit adds extensive unit tests to safeguard this support.

The same commit also silently added support for union types to the BCFile::getMemberProperties() method. Union types are not supported yet in PHPCS itself and until they are, the fact that the BCFile::getMemberProperties() method supports them should be regarded as an artefact and not as official support.

FunctionDeclarations::isArrowFunction(): add tests verifying support for union types

Adds tests, same as wil be pulled upstream, to the tests for the FunctionDeclarations::isArrowFunction() and the FunctionDeclarations::getArrowFunctionOpenClose() methods to verify and safeguard support for arrow functions using union type declarations.

Docs: update "nullable_type" comments to clarify meaning

jrfnl added 12 commits July 15, 2020 19:52
…eTokens()

Deprecate the `$parameterTypeTokens` property in favour of a new `Collections::parameterTypeTokens()` method.

PHPCS is expected to introduce a new `T_TYPE_UNION` token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the `Collections::$parameterTypeTokens` property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.
…okens()

Deprecate the `$propertyTypeTokens` property in favour of a new `Collections::propertyTypeTokens()` method.

PHPCS is expected to introduce a new `T_TYPE_UNION` token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the `Collections::$propertyTypeTokens` property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.
Deprecate the `$returnTypeTokens` property in favour of a new `Collections::returnTypeTokens()` method.

PHPCS is expected to introduce a new `T_TYPE_UNION` token. As that token would not always be available, a property can't handle this.

In anticipation of the introduction of the new token, I'm deprecating the `Collections::$returnTypeTokens` property straight away to prevent BC breaks at a later point.

Includes adding unit tests for the new method.
…ypes

In addition to the other tokens needed to support union types, the `returnTypeTokens[BC]()` methods also needs the `T_ARRAY` token as the `array` keyword in union type return types is not always correctly re-tokenized to `T_STRING`.

A change to the PHPCS tokenizer to mitigate this will be pulled upstream, but until that PR has been merged, `T_ARRAY` will continue to be needed.

This changes the versions supported by the `returnTypeTokens()` method from "minimum PHPCS 3.5.4" to "minimum 3.3.0", which also brings the method more in line with the `parameterTypeTokens()` and the `propertyTypeTokens()` methods.

Includes adjusted unit tests.
…types

This commit adds support for union types to the `FunctionDeclarations::getParameters()` method.

Changes along the same lines as included in this PR will also be pulled upstream to PHPCS itself. Once the upstream PR has been merged, those changes will also be applied to the `BCFile::getMethodParameters()` method.

Includes extensive unit tests.

Ref: https://wiki.php.net/rfc/union_types_v2
…r PHP 8 "union" types

The commit which added union type support to the `Collections::returnTypeTokens[BC]()` methods implicitly added support for union types to the `FunctionDeclarations::getProperties()` method.

This commit adds extensive unit tests to safeguard this support.

The same commit also _silently_ added support for union types to the `BCFile::getMethodProperties()` method. Union types are not supported yet in PHPCS itself and until they are, the fact that the `BCFile::getMethodProperties()` method supports them should be regarded as an artefact and not as official support.
… 8 "union" types

The commit which added union type support to the `Collections::propertyTypeTokens[BC]()` methods implicitly added support for union types to the `Variables::getMemberProperties()` method.

This commit adds extensive unit tests to safeguard this support.

The same commit also _silently_ added support for union types to the `BCFile::getMemberProperties()` method. Union types are not supported yet in PHPCS itself and until they are, the fact that the `BCFile::getMemberProperties()` method supports them should be regarded as an artefact and not as official support.
…for union types

Adds tests, same as wil be pulled upstream, to the tests for the `FunctionDeclarations::isArrowFunction()` and the `FunctionDeclarations::getArrowFunctionOpenClose()` methods to verify and safeguard support for arrow functions using union type declarations.
@jrfnl jrfnl added this to the 1.0.0-alpha4 milestone Jul 17, 2020
@jrfnl jrfnl merged commit 4d02dd8 into develop Jul 17, 2020
@jrfnl jrfnl deleted the feature/php8-union-types branch July 17, 2020 19:51
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.

None yet

1 participant