Browse Source

bug #2230 FunctionDeclarationFixer - Fix T_USE case (SpacePossum)

This PR was merged into the 1.12 branch.

Discussion
----------

FunctionDeclarationFixer - Fix T_USE case

closes https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/2216

Commits
-------

779b3e0 Fix T_USE case.
Dariusz Ruminski 8 years ago
parent
commit
97b6fb5843

+ 3 - 3
Symfony/CS/Fixer/PSR2/FunctionDeclarationFixer.php

@@ -64,12 +64,12 @@ class FunctionDeclarationFixer extends AbstractFixer
             $afterParenthesisToken = $tokens[$afterParenthesisIndex];
 
             if ($afterParenthesisToken->isGivenKind(T_USE)) {
+                // fix whitespace after T_USE (we might add a token, so do this before determining start and end parenthesis)
+                $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
+
                 $useStartParenthesisIndex = $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('));
                 $useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
 
-                // fix whitespace after T_USE
-                $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
-
                 // remove single-line edge whitespaces inside use parentheses
                 $this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
 

+ 12 - 0
Symfony/CS/Tests/Fixer/PSR2/FunctionDeclarationFixerTest.php

@@ -76,6 +76,18 @@ foo () {}',
                 '<?php function ($a) use ($b) {};',
                 '<?php function ($a) use ($b)     {};',
             ),
+            array(
+                '<?php $foo = function ($foo) use ($bar, $baz) {};',
+                '<?php $foo = function ($foo) use($bar, $baz) {};',
+            ),
+            array(
+                '<?php $foo = function ($foo) use ($bar, $baz) {};',
+                '<?php $foo = function ($foo)use ($bar, $baz) {};',
+            ),
+            array(
+                '<?php $foo = function ($foo) use ($bar, $baz) {};',
+                '<?php $foo = function ($foo)use($bar, $baz) {};',
+            ),
             array(
                 '<?php function &foo($a) {}',
                 '<?php function &foo( $a ) {}',