From a2b325ff40721a1df12daa21e13c3ba3a1572081 Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Fri, 4 Feb 2022 15:20:25 +0100 Subject: [PATCH] Tweaks --- src/Fixer/Operator/NewWithBracesFixer.php | 18 ++++---- .../Fixer/Operator/NewWithBracesFixerTest.php | 42 ++++++++++++++++++- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/Fixer/Operator/NewWithBracesFixer.php b/src/Fixer/Operator/NewWithBracesFixer.php index 186c4f4d1b7..a685fe1863c 100644 --- a/src/Fixer/Operator/NewWithBracesFixer.php +++ b/src/Fixer/Operator/NewWithBracesFixer.php @@ -122,6 +122,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void [CT::T_BRACE_CLASS_INSTANTIATION_OPEN], [CT::T_BRACE_CLASS_INSTANTIATION_CLOSE], ]; + if (\defined('T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG')) { // @TODO: drop condition when PHP 8.1+ is required $nextTokenKinds[] = [T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG]; $nextTokenKinds[] = [T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG]; @@ -134,10 +135,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } $nextIndex = $tokens->getNextTokenOfKind($index, $nextTokenKinds); - $nextToken = $tokens[$nextIndex]; // new anonymous class definition - if ($nextToken->isGivenKind(T_CLASS)) { + if ($tokens[$nextIndex]->isGivenKind(T_CLASS)) { $nextIndex = $tokens->getNextMeaningfulToken($nextIndex); if ($this->configuration['anonymous_class']) { @@ -150,14 +150,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void } // entrance into array index syntax - need to look for exit - while ($nextToken->equals('[') || $nextToken->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) { - $nextIndex = $tokens->findBlockEnd(Tokens::detectBlockType($nextToken)['type'], $nextIndex) + 1; - $nextToken = $tokens[$nextIndex]; - } - // new statement has a gap in it - advance to the next token - if ($nextToken->isWhitespace()) { - $nextIndex = $tokens->getNextNonWhitespace($nextIndex); + while ($tokens[$nextIndex]->equals('[') || $tokens[$nextIndex]->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) { + $nextIndex = $tokens->findBlockEnd(Tokens::detectBlockType($tokens[$nextIndex])['type'], $nextIndex); + $nextIndex = $tokens->getNextMeaningfulToken($nextIndex); } if ($this->configuration['named_class']) { @@ -206,11 +202,11 @@ private function ensureNoBracesAt(Tokens $tokens, int $index): void $closingIndex = $tokens->getNextMeaningfulToken($index); // constructor has arguments - braces can not be removed - if (null === $closingIndex || !$tokens[$closingIndex]->equals(')')) { + if (!$tokens[$closingIndex]->equals(')')) { return; } - $tokens->clearAt($closingIndex); + $tokens->clearTokenAndMergeSurroundingWhitespace($closingIndex); $tokens->clearTokenAndMergeSurroundingWhitespace($index); } } diff --git a/tests/Fixer/Operator/NewWithBracesFixerTest.php b/tests/Fixer/Operator/NewWithBracesFixerTest.php index f6ebf216d6d..3d436fa3fdb 100644 --- a/tests/Fixer/Operator/NewWithBracesFixerTest.php +++ b/tests/Fixer/Operator/NewWithBracesFixerTest.php @@ -256,6 +256,24 @@ public function provideNamedWithDefaultConfigurationCases(): \Generator ', ], ]; + + yield [ + " + $a = new class {}?> ', ' + $a = new class ( ) {}?> ', ], [