Skip to content

Commit

Permalink
Merge pull request #4 from PedroTroller/fix/assign-and-return-fixer
Browse files Browse the repository at this point in the history
Fix variable assign and return fixer for default arguments
  • Loading branch information
PedroTroller committed Apr 7, 2016
2 parents dd78bcf + 30110b8 commit 4172201
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 23 deletions.
33 changes: 33 additions & 0 deletions spec/PedroTroller/CS/Fixer/Contrib/AssignAndReturnFixerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$expect = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$this->fix($spl, $class)->shouldReturn($expect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$expect = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$this->fix($spl, $class)->shouldReturn($expect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$expect = <<<PHP
<?php
class TheClass
{
/**
* {@inheritdoc}
*/
public function start(Request \$request, AuthenticationException \$authException = null)
{
}
}
PHP;

$this->fix($spl, $class)->shouldReturn($expect);
Expand Down
51 changes: 28 additions & 23 deletions src/PedroTroller/CS/Fixer/Contrib/VariableAssignAndReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 4172201

Please sign in to comment.