Skip to content

Commit

Permalink
Remove removeNode() from FuncGetArgsToVariadicParamRector (#4021)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed May 29, 2023
1 parent 0ea7b49 commit 5979f30
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
20 changes: 4 additions & 16 deletions packages/PostRector/Collector/NodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function isActive(): bool
}

/**
* @deprecated
* @internal Return created nodes right in refactor() method to keep context instead.
*/
public function addNodeBeforeNode(Node $addedNode, Node $positionNode): void
Expand All @@ -67,22 +68,8 @@ public function addNodeBeforeNode(Node $addedNode, Node $positionNode): void
}

/**
* @api
* @param Node[] $addedNodes
* @internal Return created nodes right in refactor() method to keep context instead.
*/
public function addNodesAfterNode(array $addedNodes, Node $positionNode): void
{
foreach ($addedNodes as $addedNode) {
// prevent fluent method weird indent
$addedNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->addNodeAfterNode($addedNode, $positionNode);
}

$this->rectorChangeCollector->notifyNodeFileInfo($positionNode);
}

/**
* @api Used in downgrade still
* @deprecated
* Better return created nodes right in refactor() method to keep context
* @internal
*/
Expand Down Expand Up @@ -135,6 +122,7 @@ public function clearNodesToAddBefore(Node $node): void
}

/**
* @deprecated
* @api downgrade
* @deprecated Return created nodes right in refactor() method to keep context instead.
* @param Node[] $newNodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class SkipMethodCallRenameBelow
$assign = $this->getAssign();

$jsonDataAssign = new Assign(1, 2);
$this->addNodeBeforeNode($jsonDataAssign, $assign);
$this->someMethod($jsonDataAssign, $assign);
}

private function getAssign()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ final class SkipParamRenameBelow
public function run($assign)
{
$jsonDataAssign = new Assign(1, 2);
$this->addNodeBeforeNode($jsonDataAssign, $assign);
$this->someMethod($jsonDataAssign, $assign);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->params !== []) {
if ($node->params !== [] || $node->stmts === null) {
return null;
}

Expand All @@ -70,21 +71,33 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var Assign $assign */
$assign = $expression->expr;
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;
if (! $this->isFuncGetArgsFuncCall($assign->expr)) {
continue;
}

if ($assign->var instanceof Variable) {
/** @var string $variableName */
$variableName = $this->getName($assign->var);
unset($node->stmts[$key]);

if ($assign->var instanceof Variable) {
$variableName = $this->getName($assign->var);
if ($variableName === null) {
return null;
return $this->applyVariadicParams($node, $variableName);
}

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

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

public function provideMinPhpVersion(): int
Expand All @@ -97,8 +110,8 @@ private function applyVariadicParams(
string $variableName
): ClassMethod | Function_ | Closure | null {
$param = $this->createVariadicParam($variableName);
$variableParam = $param->var;
if ($variableParam instanceof Variable && $this->hasFunctionOrClosureInside($node, $variableParam)) {

if ($param->var instanceof Variable && $this->hasFunctionOrClosureInside($node, $param->var)) {
return null;
}

Expand Down Expand Up @@ -169,4 +182,13 @@ private function createVariadicParam(string $variableName): Param
$variable = new Variable($variableName);
return new Param($variable, null, null, false, true);
}

private function isFuncGetArgsFuncCall(Expr $expr): bool
{
if (! $expr instanceof FuncCall) {
return false;
}

return $this->isName($expr, 'func_get_args');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class AddBeforeInlineHTMLRector extends AbstractRector
final class AddBeforeInlineHTMLRector extends AbstractRector
{
private array $justAdded = [];

public function __construct(private readonly NodesToAddCollector $nodesToAddCollector)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('before html', []);
Expand All @@ -45,8 +41,6 @@ public function refactor(Node $node)
return null;
}

$firstStmt = current($node->stmts);

$expression1 = new Expression(
new Assign(
new Variable('test'), new String_('test')
Expand All @@ -62,11 +56,6 @@ public function refactor(Node $node)
)
);

$this->nodesToAddCollector->addNodeBeforeNode(
$expression1,
$firstStmt
);

$expression2 = new Expression(
new Assign(
new Variable('test2'), new String_('test2')
Expand All @@ -82,10 +71,7 @@ public function refactor(Node $node)
)
);

$this->nodesToAddCollector->addNodeBeforeNode(
$expression2,
$firstStmt
);
$node->stmts = array_merge([$expression1, $expression2], $node->stmts);

$this->justAdded[$this->file->getFilePath()] = true;

Expand Down

0 comments on commit 5979f30

Please sign in to comment.