diff --git a/spec/PedroTroller/CS/Fixer/Contrib/AssignAndReturnFixerSpec.php b/spec/PedroTroller/CS/Fixer/Contrib/AssignAndReturnFixerSpec.php index e9d8a9e..cd7287b 100644 --- a/spec/PedroTroller/CS/Fixer/Contrib/AssignAndReturnFixerSpec.php +++ b/spec/PedroTroller/CS/Fixer/Contrib/AssignAndReturnFixerSpec.php @@ -78,6 +78,39 @@ public function theLastFunction() return \$this->useless2 = true; } } +PHP; + + $this->fix($spl, $class)->shouldReturn($expect); + } + + function it_supports_functions_with_default_argument(\SplFileInfo $spl) + { + $class = <<fix($spl, $class)->shouldReturn($expect); diff --git a/spec/PedroTroller/CS/Fixer/Contrib/PropertyAssignAndReturnFixerSpec.php b/spec/PedroTroller/CS/Fixer/Contrib/PropertyAssignAndReturnFixerSpec.php index d96a342..483a486 100644 --- a/spec/PedroTroller/CS/Fixer/Contrib/PropertyAssignAndReturnFixerSpec.php +++ b/spec/PedroTroller/CS/Fixer/Contrib/PropertyAssignAndReturnFixerSpec.php @@ -80,6 +80,39 @@ public function theLastFunction() return \$this->useless2 = true; } } +PHP; + + $this->fix($spl, $class)->shouldReturn($expect); + } + + function it_supports_functions_with_default_argument(\SplFileInfo $spl) + { + $class = <<fix($spl, $class)->shouldReturn($expect); diff --git a/spec/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixerSpec.php b/spec/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixerSpec.php index 40cf792..fe248e6 100644 --- a/spec/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixerSpec.php +++ b/spec/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixerSpec.php @@ -82,6 +82,39 @@ public function theLastFunction() return \$this->useless2; } } +PHP; + + $this->fix($spl, $class)->shouldReturn($expect); + } + + function it_supports_functions_with_default_argument(\SplFileInfo $spl) + { + $class = <<fix($spl, $class)->shouldReturn($expect); diff --git a/src/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixer.php b/src/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixer.php index 10217b2..d4b806c 100644 --- a/src/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixer.php +++ b/src/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixer.php @@ -24,30 +24,35 @@ public function fix(\SplFileInfo $file, $content) ), $index); if (null !== $assign) { - $keys = array_keys($assign); - $equals = end($keys); - $endline = $tokens->getNextTokenOfKind($equals, array(';')); - $valueTokens = array(); - - for ($i = $tokens->getNextMeaningfulToken($equals); $i <= $endline; ++$i) { - $valueTokens[$i] = $tokens[$i]; - } - - $variable = $tokens[$keys[0]]; - $next = $tokens->getNextMeaningfulToken($endline); - - if (T_RETURN === $tokens[$next]->getId()) { - $all[] = array( - 'assign' => $assign, - 'value' => $valueTokens, - 'return' => $tokens->findSequence(array( - array(T_VARIABLE, $variable->getContent()), - ';', - ), $next + 2), - ); + $keys = array_keys($assign); + list($variableIndex, $equalsIndex) = $keys; + $endline = $tokens->getNextTokenOfKind($equalsIndex, array(';')); + $variable = $tokens[$variableIndex]; + + if (null !== $endline) { + $valueTokens = array(); + + for ($i = $tokens->getNextMeaningfulToken($equalsIndex); $i <= $endline; ++$i) { + $valueTokens[$i] = $tokens[$i]; + } + + $next = $tokens->getNextMeaningfulToken($endline); + + if (T_RETURN === $tokens[$next]->getId()) { + $all[] = array( + 'assign' => $assign, + 'value' => $valueTokens, + 'return' => $tokens->findSequence(array( + array(T_VARIABLE, $variable->getContent()), + ';', + ), $next + 2), + ); + } + + $index = $next + 6; + } else { + $assign = null; } - - $index = $next + 6; } } while (null !== $assign);