From c4fba27e0e9750e792d85dfc8958b7495d9e6f6a Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Mon, 21 Feb 2022 09:06:11 +0100 Subject: [PATCH] tweaks --- .../ClassNotation/ClassDefinitionFixer.php | 18 ++++++++++++------ .../ClassNotation/ClassDefinitionFixerTest.php | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Fixer/ClassNotation/ClassDefinitionFixer.php b/src/Fixer/ClassNotation/ClassDefinitionFixer.php index 936cb00873a..233027e887d 100644 --- a/src/Fixer/ClassNotation/ClassDefinitionFixer.php +++ b/src/Fixer/ClassNotation/ClassDefinitionFixer.php @@ -201,6 +201,18 @@ private function fixClassyDefinition(Tokens $tokens, int $classyIndex): void $end = $tokens->getPrevNonWhitespace($classDefInfo['open']); } + if ($classDefInfo['anonymousClass'] && !$this->configuration['inline_constructor_arguments']) { + if (!$tokens[$end]->equals(')')) { // anonymous class with `extends` and/or `implements` + $start = $tokens->getPrevMeaningfulToken($end); + $this->makeClassyDefinitionSingleLine($tokens, $start, $end); + $end = $start; + } + + if ($tokens[$end]->equals(')')) { // skip constructor arguments of anonymous class + $end = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $end); + } + } + // 4.1 The extends and implements keywords MUST be declared on the same line as the class name. $this->makeClassyDefinitionSingleLine($tokens, $classDefInfo['start'], $end); } @@ -335,12 +347,6 @@ private function getClassyInheritanceInfo(Tokens $tokens, int $startIndex, strin private function makeClassyDefinitionSingleLine(Tokens $tokens, int $startIndex, int $endIndex): void { for ($i = $endIndex; $i >= $startIndex; --$i) { - if (!$this->configuration['inline_constructor_arguments'] && $tokens[$i]->equals(')')) { - $i = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $i); - - continue; - } - if ($tokens[$i]->isWhitespace()) { if ($tokens[$i - 1]->isComment() || $tokens[$i + 1]->isComment()) { $content = $tokens[$i - 1]->getContent(); diff --git a/tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php b/tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php index 294ae6c853e..16d4ee1609f 100644 --- a/tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php +++ b/tests/Fixer/ClassNotation/ClassDefinitionFixerTest.php @@ -159,6 +159,11 @@ public function provideAnonymousClassesCases(): array " false], ], + [ + ' false], + ], [ 'foo() , bar ( $a) ) {};', "foo() , bar ( \$a) ){};", @@ -186,6 +191,16 @@ public function provideAnonymousClassesCases(): array 'prop ){};', ['inline_constructor_arguments' => false], ], + [ + " false], + ], + [ + " false], + ], [ 'prop, $v[3], 4) {};', 'prop,$v[3], 4) {};', @@ -198,7 +213,7 @@ public function provideAnonymousClassesCases(): array \Serializable {};', ' [