Browse Source

fix: SpacesInsideParenthesesFixer - handle class instantiation parentheses (#7531)

Kuba Werłos 1 year ago
parent
commit
986f18b272

+ 7 - 5
src/Fixer/Whitespace/SpacesInsideParenthesesFixer.php

@@ -76,14 +76,14 @@ function foo(\$bar, \$baz)
 
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isTokenKindFound('(');
+        return $tokens->isAnyTokenKindsFound(['(', CT::T_BRACE_CLASS_INSTANTIATION_OPEN]);
     }
 
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         if ('none' === $this->configuration['space']) {
             foreach ($tokens as $index => $token) {
-                if (!$token->equals('(')) {
+                if (!$token->equalsAny(['(', [CT::T_BRACE_CLASS_INSTANTIATION_OPEN]])) {
                     continue;
                 }
 
@@ -94,7 +94,8 @@ function foo(\$bar, \$baz)
                     continue;
                 }
 
-                $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
+                $blockType = Tokens::detectBlockType($tokens[$index]);
+                $endIndex = $tokens->findBlockEnd($blockType['type'], $index);
 
                 // remove space after opening `(`
                 if (!$tokens[$tokens->getNextNonWhitespace($index)]->isComment()) {
@@ -110,11 +111,12 @@ function foo(\$bar, \$baz)
 
         if ('single' === $this->configuration['space']) {
             foreach ($tokens as $index => $token) {
-                if (!$token->equals('(')) {
+                if (!$token->equalsAny(['(', [CT::T_BRACE_CLASS_INSTANTIATION_OPEN]])) {
                     continue;
                 }
 
-                $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
+                $blockType = Tokens::detectBlockType($tokens[$index]);
+                $endParenthesisIndex = $tokens->findBlockEnd($blockType['type'], $index);
 
                 // if not other content than spaces in block remove spaces
                 $blockContent = $this->getBlockContent($index, $endParenthesisIndex, $tokens);

+ 15 - 0
tests/Fixer/Whitespace/SpacesInsideParenthesesFixerTest.php

@@ -241,6 +241,16 @@ multiply((2 + 3) * 4);
 multiply( (    2 + 3  ) * 4    );
 ',
         ];
+
+        yield [
+            '<?php $x = (new Foo())->bar();',
+            '<?php $x = ( new Foo() )->bar();',
+        ];
+
+        yield [
+            '<?php $x = (new Foo)->bar;',
+            '<?php $x = ( new Foo )->bar;',
+        ];
     }
 
     /**
@@ -423,6 +433,11 @@ multiply( ( 2 + 3 ) * 4 );
 multiply((2 + 3) * 4);
 ',
         ];
+
+        yield [
+            '<?php $x = ( new Foo() )->bar();',
+            '<?php $x = (new Foo())->bar();',
+        ];
     }
 
     /**