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

CodeAnalysis/AssignmentInCondition: defer to upstream, implement PHPCSUtils & support modern PHP #2198

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 12, 2023

CodeAnalysis/AssignmentInCondition: defer to upstream sniff

Upstream PR squizlabs/PHP_CodeSniffer#1594 pulled largely the same sniff to PHPCS itself and was merged in PHPCS 3.1.0.

The only part of the sniff I held back at the time, was the "is this an assignment in a ternary condition" part of the sniff as that was (and still is) more prone to false positives/negatives due to the difficulty in determining the start and end of the condition.

As the minimum supported PHPCS version for WPCS is now well beyond PHPCS 3.1.0, I'd like to propose to defer to the upstream sniff for the control structure condition checking and to only keep the code related to checking for assignments in ternary conditions in WPCS.

To prevent confusion between the two sniffs (the upstream one and this one) and to make it clear which is checking what, I also propose to rename the sniff to WordPress.CodeAnalysis.AssignmentInTernaryCondition.

Includes bringing the WPCS native PHPCS ruleset inline with this change.

CodeAnalysis/AssignmentInTernaryCondition: remove some redundant code

There will always be non-empty tokens before a ternary, if nothing else, a PHP open tag, so this condition is redundant.

CodeAnalysis/AssignmentInTernaryCondition: add some extra tests

... to cover code (which is bowing out of the sniff) previously uncovered.

CodeAnalysis/AssignmentInTernaryCondition: implement PHPCSUtils

CodeAnalysis/AssignmentInTernaryCondition: add test with PHP 7.4+ null coalesce equals

... to ensure it is recognized as an assignment.

Upstream PR squizlabs/PHP_CodeSniffer 1594 pulled largely the same sniff to PHPCS itself and was merged in PHPCS 3.1.0.

The only part of the sniff I held back at the time, was the "is this an assignment in a ternary condition" part of the sniff as that was (and still is) more prone to false positives/negatives due to the difficulty in determining the start and end of the condition.

As the minimum supported PHPCS version for WPCS is now well beyond PHPCS 3.1.0, I'd like to propose to defer to the upstream sniff for the control structure condition checking and to only keep the code related to checking for assignments in ternary conditions in WPCS.

To prevent confusion between the two sniffs (the upstream one and this one) and to make it clear which is checking what, I also propose to rename the sniff to `WordPress.CodeAnalysis.AssignmentInTernaryCondition`.

Includes bringing the WPCS native PHPCS ruleset inline with this change.
There will always be non-empty tokens before a ternary, if nothing else, a PHP open tag, so this condition is redundant.
... to cover code (which is bowing out of the sniff) previously uncovered.
…l coalesce equals

... to ensure it is recognized as an assignment.
Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

@dingo-d dingo-d merged commit 485975e into develop Jan 12, 2023
@dingo-d dingo-d deleted the feature/assignment-in-condition-phpcsutils-modern-php branch January 12, 2023 20:35
@jrfnl jrfnl mentioned this pull request Jun 8, 2023
19 tasks
GaryJones added a commit to Automattic/VIP-Coding-Standards that referenced this pull request Jun 20, 2023
GaryJones added a commit to Automattic/VIP-Coding-Standards that referenced this pull request Jun 22, 2023
GaryJones added a commit to Automattic/VIP-Coding-Standards that referenced this pull request Jun 22, 2023
GaryJones added a commit to Automattic/VIP-Coding-Standards that referenced this pull request Jun 22, 2023
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.

3 participants