From 6a4abf447866731eed5d4a944686992366840cc3 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 4 Nov 2019 08:26:48 +1100 Subject: [PATCH] Fixed bug #2678 : PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself --- package.xml | 1 + .../Classes/AnonClassDeclarationUnitTest.inc | 3 ++ .../AnonClassDeclarationUnitTest.inc.fixed | 3 ++ .../Sniffs/Classes/ClassDeclarationSniff.php | 32 ++++++++++--------- .../Classes/ClassDeclarationUnitTest.inc | 3 ++ .../ClassDeclarationUnitTest.inc.fixed | 3 ++ 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/package.xml b/package.xml index f75080f30b..89ef9b3657 100644 --- a/package.xml +++ b/package.xml @@ -32,6 +32,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Thanks to Raphael Horber for the patch - Fixed bug #2674 : Squiz.Functions.FunctionDeclarationArgumentSpacing prints wrong argument name in error message - Fixed bug #2676 : PSR12.Files.FileHeader locks up when file ends with multiple inline comments + - Fixed bug #2678 : PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself diff --git a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc index 855b0c9a94..e132b54df1 100644 --- a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc @@ -79,3 +79,6 @@ $foo->bar( // ... }, ); + +foo(new class { +}); diff --git a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed index 78e4f025b8..921dcc02de 100644 --- a/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Classes/AnonClassDeclarationUnitTest.inc.fixed @@ -81,3 +81,6 @@ $foo->bar( // ... }, ); + +foo(new class { +}); diff --git a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php index 67d1a6e5b7..0d801fba82 100644 --- a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php @@ -503,21 +503,23 @@ public function processClose(File $phpcsFile, $stackPtr) } }//end if - // Check the closing brace is on it's own line, but allow - // for comments like "//end class". - $ignoreTokens = Tokens::$phpcsCommentTokens; - $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'] - ) { - $type = strtolower($tokens[$stackPtr]['content']); - $error = 'Closing %s brace must be on a line by itself'; - $data = [$type]; - $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data); + if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS) { + // Check the closing brace is on it's own line, but allow + // for comments like "//end class". + $ignoreTokens = Tokens::$phpcsCommentTokens; + $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'] + ) { + $type = strtolower($tokens[$stackPtr]['content']); + $error = 'Closing %s brace must be on a line by itself'; + $data = [$type]; + $phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data); + } } }//end processClose() diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc index 57c22ce4b2..13e596e17a 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc @@ -236,3 +236,6 @@ class C8 { } + +foo(new class { +}); diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed index 4b66e3a078..3ee394e789 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed @@ -229,3 +229,6 @@ class C7 extends \Foo\Bar implements \Baz\Bar // comment class C8 { } + +foo(new class { +});