From 04da5586979df7d1b71371b81da7fe70a39a074a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 16 Jul 2024 01:09:06 +0200 Subject: [PATCH] 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/Sniffs/Classes/ClassDeclarationSniff.php | 1 - .../PSR2/Tests/Classes/ClassDeclarationUnitTest.inc | 7 +++++++ .../PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php index 3325459ae2..42cff38957 100644 --- a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php @@ -525,7 +525,6 @@ public function processClose(File $phpcsFile, $stackPtr) $ignoreTokens[] = T_WHITESPACE; $ignoreTokens[] = T_COMMENT; $ignoreTokens[] = T_SEMICOLON; - $ignoreTokens[] = T_COMMA; $nextContent = $phpcsFile->findNext($ignoreTokens, ($closeBrace + 1), null, true); if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line'] diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc index 4cc2d4fb87..c2695983ec 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc @@ -331,3 +331,10 @@ interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed { } // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed. + +// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678. +$foo->bar( + new class implements Bar { + // ... + }, +); diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed index 1e306e373b..7bd99d3a3c 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed @@ -320,3 +320,10 @@ interface ClassBraceNotOnLineByItselfTrailingCommentIsAllowed trait ClassBraceNotOnLineByItselfTrailingAnnotationIsAllowed { } // phpcs:ignore Stnd.Cat.Sniff -- this comment is also allowed. + +// Issue squizlabs/PHP_CodeSniffer#2621 - fix was superseded by fix for #2678. +$foo->bar( + new class implements Bar { + // ... + }, +);