Skip to content

Commit

Permalink
[CodingStyle] Handle FuncGetArgsToVariadicParamRector in Method/Stati…
Browse files Browse the repository at this point in the history
…c Call (#389)
  • Loading branch information
samsonasik authored Jul 6, 2021
1 parent dc78a3e commit 95e6a5e
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture;

use Webmozart\Assert\Assert;

final class InMethodOrStaticCall
{
public function run(): void
{
$this->count($args = func_get_args(), 1);
Assert::count($args = func_get_args(), 1);
}

private function count($args, $value)
{
Assert::count($args, $value);
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture;

use Webmozart\Assert\Assert;

final class InMethodOrStaticCall
{
public function run(...$args): void
{
$this->count($args, 1);
Assert::count($args, 1);
}

private function count($args, $value)
{
Assert::count($args, $value);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture;

use Webmozart\Assert\Assert;

final class InMethodOrStaticCall2
{
public function run(): void
{
function () {
$this->count($args = func_get_args(), 1);
Assert::count($args = func_get_args(), 1);
};
}

public function run2(): void
{
function innerFunc() {
Assert::count($args = func_get_args(), 1);
};
}

private function count($args, $value)
{
Assert::count($args, $value);
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector\Fixture;

use Webmozart\Assert\Assert;

final class InMethodOrStaticCall2
{
public function run(): void
{
function (...$args) {
$this->count($args, 1);
Assert::count($args, 1);
};
}

public function run2(): void
{
function innerFunc(...$args) {
Assert::count($args, 1);
};
}

private function count($args, $value)
{
Assert::count($args, $value);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
namespace Rector\CodingStyle\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -77,12 +82,16 @@ public function refactor(Node $node): ?Node
return null;
}

$this->removeNode($assign);
} else {
$variableName = 'args';
$assign->expr = new Variable('args');
return $this->removeOrChangeAssignToVariable($node, $assign, $variableName);
}

$variableName = 'args';
$assign->expr = new Variable('args');
return $this->applyVariadicParams($node, $assign, $variableName);
}

private function applyVariadicParams(ClassMethod|Function_|Closure $node, Assign $assign, string $variableName): ?Node
{
$param = $this->createVariadicParam($variableName);
$variableParam = $param->var;
if ($variableParam instanceof Variable && $this->hasFunctionOrClosureInside($node, $variableParam)) {
Expand All @@ -93,6 +102,31 @@ public function refactor(Node $node): ?Node
return $node;
}

private function removeOrChangeAssignToVariable(ClassMethod|Function_|Closure $node, Assign $assign, string $variableName): ?Node
{
$parent = $assign->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof Expression) {
$this->removeNode($assign);
return $this->applyVariadicParams($node, $assign, $variableName);
}

$variable = $assign->var;
/** @var ClassMethod|Function_|Closure $functionLike */
$functionLike = $this->betterNodeFinder->findParentType($parent, FunctionLike::class);
/** @var Stmt[] $stmts */
$stmts = $functionLike->getStmts();
$this->traverseNodesWithCallable($stmts, function (Node $node) use ($assign, $variable): ?Expr {
if (! $this->nodeComparator->areNodesEqual($node, $assign)) {
return null;
}

return $variable;
});

$this->applyVariadicParams($functionLike, $assign, $variableName);
return $node;
}

private function hasFunctionOrClosureInside(
ClassMethod | Function_ | Closure $functionLike,
Variable $variable
Expand Down

0 comments on commit 95e6a5e

Please sign in to comment.