Skip to content

Update for new upstream PHPCS 4.x branch #674

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

Merged
merged 14 commits into from
Jun 5, 2025

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 4, 2025

Context

The PHPCS 4 branch has been recreated - see PHPCSStandards/PHP_CodeSniffer#120

The new branch is called 4.x - in contrast to the "old" 4.0 branch.

PHPCSUtils needs to be made compatible with the additional changes made for PHPCS 4.x, as well as start testing against the new PHPCS 4 branch.

This PR intends to do that.

Commits

GH Actions: update for new upstream 4.x branch

BCTokens: sync with PHPCS 4.0 [1] / removed JS tokens

PHPCSStandards/PHP_CodeSniffer#983 (JS/CSS support drop) removes the T_ZSR_EQUAL, T__PROPERTY and T_OBJECT tokens, including removing them from the Tokens::$assignmentTokens, Tokens::$blockOpeners and Tokens::$scopeOpeners token arrays.

This commit aligns these token arrays in the BCFile class with their PHPCS 4.0 contents.

Includes updated tests.

BCTokens: sync with PHPCS 4.0 [2] / anon class is function name token

PHPCSStandards/PHP_CodeSniffer#47 adds the T_ANON_CLASS token to the Tokens::$functionNameTokens array.

Includes updated tests.

BCTokens: sync with PHPCS 4.0 [3] / closure use is parenthesis owner

PHPCSStandards/PHP_CodeSniffer#1013 made T_USE for closure use a parenthesis owner.

While the token will now be included in the BCTokens::parenthesisOpeners() return value, while on PHPCS 3.x, it will still not be a parentheses owner.
Use the methods in the Parentheses class to work out whether T_USE is actually a parenthesis owner in a PHPCS cross-version compatible manner (commit upcoming).

Includes updated tests.

BCTokens: sync with PHPCS 4.0 [4] / more parenthesis owners

PHPCSStandards/PHP_CodeSniffer#1014 made T_ISSET, T_UNSET, T_EMPTY, T_EVAL and T_EXIT parenthesis owners.

While the token will now be included in the BCTokens::parenthesisOpeners() return value, while on PHPCS 3.x, they will still not be parentheses owners.
Use the methods in the Parentheses class to work out whether any of these tokens is actually a parenthesis owner in a PHPCS cross-version compatible manner.

Note: The Parentheses methods have supported these tokens as parenthesis owners since PHPCSUtils 1.0.0.

Includes updated tests.

BCTokens: sync with PHPCS 4.0 [5] / namespaced name tokens

PHPCSStandards/PHP_CodeSniffer#1020 (namespaced names) adds a new Tokens::NAME_TOKENS token array and also adds these tokens to the Tokens::$functionNameTokens token array. This last bit was already handled previously.

This commit now adds the mirror function for the Tokens::NAME_TOKENS token array.

Includes updated tests.

UtilityMethodTestCase: make compatible with PHPCS 4.0

This commit introduces a test double for the PHPCS native Ruleset class.

PHPCS >= 4.0 is stricter about registering sniffs, so when "mocking" a registered sniff, it will throw a "No sniffs were registered." RuntimeException.

Most utility method tests don't need for any sniffs to be registered, they just need a Ruleset object to be available to allow for creating a File object. This test double accommodates that by catching the exception.

Tests: deal with difference in tokenization of PHP open tag in PHPCS 4.0

The whitespace which may be included in a long PHP open tag is tokenized as a separate T_WHITESPACE token as of PHPCS 4.0.

This commit updates token position expectations to take this into account.

Related to upstream:

Parentheses::getOwner(): T_USE is now a parenthesis owner

T_USE tokens used for closure use statements are parentheses owners as of PHPCS 4.0.0.

This commit updated the Parentheses::getOwner() method to treat closure T_USE tokens as parentheses owners, PHPCS cross-version.

Includes additional unit tests for this case.

Refs:

BCFile::getMethodParameters(): sync with PHPCS 4.0 / T_USE is parenthesis owner

This change was already previously handled in PHPCSUtils. This commit just syncs in the new tests as introduced upstream.

Ref: PHPCSStandards/PHP_CodeSniffer#1013

BCFile::getMemberProperties(): sync with PHPCS 4.0 / remove parse error warning + PHP 8.4 interface properties

As of PHPCS 4.0, the File::getMemberProperties() method:

  • ... will handle properties in interfaces to support PHP 8.4, in which this is now allowed.
  • ... will no longer throw a parse error warning.

This commit makes the necessary updates in PHPCSUtils for the same:

  • Adds T_INTERFACE to the Collections::ooPropertyScopes() for PHP 8.4 properties in interfaces support.
  • Updates the test expectations for the Scopes::isOOProperty() method to allow for PHP 8.4 properties in interfaces.
  • Updates the BCFile::getMemberProperties() polyfill to mirror the PHPCS 4.0 version of the method.
  • Updates various tests for both the BCFile::getMemberProperties() and the Variables::getMemberProperties() methods to be in line with the changes.

Ref: PHPCSStandards/PHP_CodeSniffer#991

BCFile::getDeclarationName(): sync with PHPCS 4.0 / stop accepting tokens for non-named structures

The [BC]File::getDeclarationName() method - for historic reasons - accepted the T_CLOSURE and T_ANON_CLASS tokens, even though these structures will never have a name, and returned null for those tokens.

This commit changes the BCFile::getDeclarationName() method to no longer accept those tokens and throw an exception if they are passed to the method instead.

As a secondary change, when the name of a valid structure cannot be determined, the method will now no longer return null, but will return an empty string.
This normalizes the return type of the method to always return a string (or throw an exception).

Includes updated unit tests to match.

This change mirrors the upstream change made to the File::getDeclarationName() method in PHPCS 4.0.

Note: this change is NOT mirrored in the ObjectDeclarations::getName() method, as changing it there would constitute a breaking change for PHPCSUtils, so that change needs to wait until PHPCSUtils 2.0.

Ref: PHPCSStandards/PHP_CodeSniffer#1007

BCFile::findExtendedClassName(): sync with PHPCS 4.0 / handling of namespace relative parent classes

The PHPCS File::findExtendedClassName() method did not handle namespace relative parent classes correctly prior to PHPCS 4.0.

The PHPCSUtils native ObjectDeclarations::findExtendedClassName() method did already handle this correctly.

This commit adds the PHPCS 4.x version of the findExtendedClassName() method to the BCFile class.

Includes moving related tests from the "Diff" test file to the "normal" test file and updating the docs to annotate this is no longer a difference between the PHPCS native and PHPCSUtils versions of the method.

BCFile::findImplementedInterfaceNames(): sync with PHPCS 4.0 / handling of namespace relative interfaces

The PHPCS File::findImplementedInterfaceNames() method did not handle namespace relative interfaces correctly prior to PHPCS 4.0.

The PHPCSUtils native ObjectDeclarations::findImplementedInterfaceNames() method did already handle this correctly.

This commit adds the PHPCS 4.x version of the findImplementedInterfaceNames() method to the BCFile class.

Includes moving related tests from the "Diff" test file to the "normal" test file and updating the docs to annotate this is no longer a difference between the PHPCS native and PHPCSUtils versions of the method.

jrfnl added 3 commits June 4, 2025 12:23
PHPCSStandards/PHP_CodeSniffer 983 (JS/CSS support drop) removes the `T_ZSR_EQUAL`, `T__PROPERTY` and `T_OBJECT` tokens, including removing them from the `Tokens::$assignmentTokens`, `Tokens::$blockOpeners` and `Tokens::$scopeOpeners` token arrays.

This commit aligns these token arrays in the `BCFile` class with their PHPCS 4.0 contents.

Includes updated tests.
PHPCSStandards/PHP_CodeSniffer 47 adds the `T_ANON_CLASS` token to the `Tokens::$functionNameTokens` array.

Includes updated tests.
Copy link

@rodrigoprimo rodrigoprimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I just left two minor remarks.

jrfnl added 11 commits June 4, 2025 22:02
PHPCSStandards/PHP_CodeSniffer 1013 made `T_USE` for closure use a parenthesis owner.

While the token will now be included in the `BCTokens::parenthesisOpeners()` return value, while on PHPCS 3.x, it will still not be a parentheses owner.
Use the methods in the `Parentheses` class to work out whether `T_USE` is actually a parenthesis owner in a PHPCS cross-version compatible manner (commit upcoming).

Includes updated tests.
PHPCSStandards/PHP_CodeSniffer 1014 made `T_ISSET`, `T_UNSET`, `T_EMPTY`, `T_EVAL` and `T_EXIT` parenthesis owners.

While the token will now be included in the `BCTokens::parenthesisOpeners()` return value, while on PHPCS 3.x, they will still not be parentheses owners.
Use the methods in the `Parentheses` class to work out whether any of these tokens is actually a parenthesis owner in a PHPCS cross-version compatible manner.

Note: The `Parentheses` methods have supported these tokens as parenthesis owners since PHPCSUtils 1.0.0.

Includes updated tests.
PHPCSStandards/PHP_CodeSniffer 1020 (namespaced names) adds a new `Tokens::NAME_TOKENS` token array and also adds these tokens to the `Tokens::$functionNameTokens` token array. This last bit was already handled previously.

This commit now adds the mirror function for the `Tokens::NAME_TOKENS` token array.

Includes updated tests.
This commit introduces a test double for the PHPCS native `Ruleset` class.

PHPCS >= 4.0 is stricter about registering sniffs, so when "mocking" a registered sniff, it will throw a "No sniffs were registered." `RuntimeException`.

Most utility method tests don't need for any sniffs to be registered, they just need a `Ruleset` object to be available to allow for creating a `File` object. This test double accommodates that by catching the exception.
The whitespace which may be included in a long PHP open tag is tokenized as a separate `T_WHITESPACE` token as of PHPCS 4.0.

This commit updates token position expectations to take this into account.

Related to upstream:
* PHPCSStandards/PHP_CodeSniffer 593
* PHPCSStandards/PHP_CodeSniffer 1015
`T_USE` tokens used for closure use statements are parentheses owners as of PHPCS 4.0.0.

This commit updated the `Parentheses::getOwner()` method to treat closure `T_USE` tokens as parentheses owners, PHPCS cross-version.

Includes additional unit tests for this case.

Refs:
* PHPCSStandards/PHP_CodeSniffer 1013
* squizlabs/PHP_CodeSniffer 2593
* squizlabs/PHP_CodeSniffer 3104
…esis owner

This change was already previously handled in PHPCSUtils. This commit just syncs in the new tests as introduced upstream.

Ref: PHPCSStandards/PHP_CodeSniffer 1013
…or warning + PHP 8.4 interface properties

As of PHPCS 4.0, the `File::getMemberProperties()` method:
* ... will handle properties in interfaces to support PHP 8.4, in which this is now allowed.
* ... will no longer throw a parse error warning.

This commit makes the necessary updates in PHPCSUtils for the same:
* Adds `T_INTERFACE` to the `Collections::ooPropertyScopes()` for PHP 8.4 properties in interfaces support.
* Updates the test expectations for the `Scopes::isOOProperty()` method to allow for PHP 8.4 properties in interfaces.
* Updates the `BCFile::getMemberProperties()` polyfill to mirror the PHPCS 4.0 version of the method.
* Updates various tests for both the `BCFile::getMemberProperties()` and the `Variables::getMemberProperties()` methods to be in line with the changes.

Ref: PHPCSStandards/PHP_CodeSniffer 991
…kens for non-named structures

The `[BC]File::getDeclarationName()` method - for historic reasons - accepted the `T_CLOSURE` and `T_ANON_CLASS` tokens, even though these structures will never have a name, and returned `null` for those tokens.

This commit changes the `BCFile::getDeclarationName()` method to no longer accept those tokens and throw an exception if they are passed to the method instead.

As a secondary change, when the name of a valid structure cannot be determined, the method will now no longer return `null`, but will return an empty string.
This normalizes the return type of the method to always return a string (or throw an exception).

Includes updated unit tests to match.

This change mirrors the upstream change made to the `File::getDeclarationName()` method in PHPCS 4.0.

Note: this change is NOT mirrored in the `ObjectDeclarations::getName()` method, as changing it there would constitute a breaking change for PHPCSUtils, so that change needs to wait until PHPCSUtils 2.0.

Ref: PHPCSStandards/PHP_CodeSniffer 1007
…mespace relative parent classes

The PHPCS `File::findExtendedClassName()` method did not handle namespace relative parent classes correctly prior to PHPCS 4.0.

The PHPCSUtils native `ObjectDeclarations::findExtendedClassName()` method **_did_** already handle this correctly.

This commit adds the PHPCS 4.x version of the `findExtendedClassName()` method to the `BCFile` class.

Includes moving related tests from the "Diff" test file to the "normal" test file and updating the docs to annotate this is no longer a difference between the PHPCS native and PHPCSUtils versions of the method.
…ng of namespace relative interfaces

The PHPCS `File::findImplementedInterfaceNames()` method did not handle namespace relative interfaces correctly prior to PHPCS 4.0.

The PHPCSUtils native `ObjectDeclarations::findImplementedInterfaceNames()` method **_did_** already handle this correctly.

This commit adds the PHPCS 4.x version of the `findImplementedInterfaceNames()` method to the `BCFile` class.

Includes moving related tests from the "Diff" test file to the "normal" test file and updating the docs to annotate this is no longer a difference between the PHPCS native and PHPCSUtils versions of the method.
@jrfnl jrfnl force-pushed the feature/update-for-new-upstream-4-branch branch from 578cf80 to 2adfb12 Compare June 4, 2025 20:02
@jrfnl jrfnl merged commit ff83d52 into develop Jun 5, 2025
59 checks passed
@jrfnl jrfnl deleted the feature/update-for-new-upstream-4-branch branch June 5, 2025 21:45
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.

2 participants