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

PSR2/ClassDeclaration: add tests and remove some redundant code #556

Merged
merged 4 commits into from
Jul 20, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 16, 2024

Description

PSR2/ClassDeclaration: add tests covering the CloseBraceSameLine error

Previously, there were no dedicated tests for the CloseBraceSameLine error.

PSR2/ClassDeclaration: remove some redundant code [1]

Issue squizlabs/PHP_CodeSniffer#2621 added the T_COMMA token to the $ignoreTokens (without adding a test) to prevent a false positive for a anonymous class declaration nested within a function call.
That fix was later superseded by another fix for basically the same issue via squizlabs/PHP_CodeSniffer#2678, which excluded anonymous classes completely from the CloseBraceSameLine check.

This commit adds the test case from squizlabs/PHP_CodeSniffer#2621 and removed the redundant T_COMMA token as the test now confirms it is no longer needed.

PSR2/ClassDeclaration: remove some redundant code [2]

In the original version of the sniff, only comments tokens after the close brace were ignored for the CloseBraceSameLine check.

Since then the sniff has received numerous changes improving on that code to prevent false positives.

Once such change was made in response to issue squizlabs/PHP_CodeSniffer#689, the fix adding ignoring for whitespace tokens to the code block.

This makes the $tokens[$nextContent]['content'] !== $phpcsFile->eolChar check redundant as that condition can now never be true anymore (as it could only match on T_WHITESPACE tokens and those are now ignored).

This change is covered by the tests previously added.

PSR2/ClassDeclaration: add test with close brace followed by PHP close tag

Related to #552

This adds a test to the sniff documenting that when a PHP close tag is on the same line as the OO close brace, the sniff will throw an error.

In my opinion, this is the correct and expected behaviour.

Suggested changelog entry

N/A

jrfnl added 4 commits July 20, 2024 06:14
Previously, there were no dedicated tests for the `CloseBraceSameLine` error.
Issue squizlabs/PHP_CodeSniffer 2621 added the `T_COMMA` token to the `$ignoreTokens` (without adding a test) to prevent a false positive for a anonymous class declaration nested within a function call.
That fix was later superseded by another fix for basically the same issue via squizlabs/PHP_CodeSniffer#2678, which excluded anonymous classes completely from the `CloseBraceSameLine` check.

This commit adds the test case from squizlabs/PHP_CodeSniffer 2621 and removed the redundant `T_COMMA` token as the test now confirms it is no longer needed.
In the original version of the sniff, only comments tokens after the close brace were ignored for the `CloseBraceSameLine` check.

Since then the sniff has received numerous changes improving on that code to prevent false positives.

Once such change was made in response to issue squizlabs/PHP_CodeSniffer 689, the fix adding ignoring for whitespace tokens to the code block.

This makes the `$tokens[$nextContent]['content'] !== $phpcsFile->eolChar` check redundant as that condition can now never be true anymore (as it could only match on `T_WHITESPACE` tokens and those are now ignored).

This change is covered by the tests previously added.
…e tag

Related to 552

This adds a test to the sniff documenting that when a PHP close tag is on the same line as the OO close brace, the sniff will throw an error.

In my opinion, this is the correct and expected behaviour.
@jrfnl jrfnl force-pushed the feature/psr2-classdeclaration-add-tests-close-tag branch from ff9ea41 to 8303a3f Compare July 20, 2024 04:14
@jrfnl
Copy link
Member Author

jrfnl commented Jul 20, 2024

Rebased without changes, merging once the build has passed.

@jrfnl jrfnl merged commit 85318b7 into master Jul 20, 2024
48 checks passed
@jrfnl jrfnl deleted the feature/psr2-classdeclaration-add-tests-close-tag branch July 20, 2024 04:44
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