Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\StaticVar;
Expand All @@ -32,6 +33,11 @@ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector
*/
private $undefinedVariables = [];

/**
* @var string[]
*/
private $definedVariables = [];

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Adds default value for undefined variable', [
Expand Down Expand Up @@ -79,6 +85,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$this->definedVariables = [];
$this->undefinedVariables = [];

$this->traverseNodesWithCallable((array) $node->stmts, function (Node $node): ?int {
Expand All @@ -87,6 +94,8 @@ public function refactor(Node $node): ?Node
return NodeTraverser::STOP_TRAVERSAL;
}

$this->collectDefinedVariablesFromForeach($node);

if (! $node instanceof Variable) {
return null;
}
Expand All @@ -95,10 +104,8 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var string $variableName */
$variableName = $this->getName($node);
if ($variableName === null) {
return null;
}

// defined 100 %
/** @var Scope $nodeScope */
Expand All @@ -120,6 +127,10 @@ public function refactor(Node $node): ?Node

$variablesInitiation = [];
foreach ($this->undefinedVariables as $undefinedVariable) {
if (in_array($undefinedVariable, $this->definedVariables, true)) {
continue;
}

$variablesInitiation[] = new Expression(new Assign(new Variable($undefinedVariable), $this->createNull()));
}

Expand Down Expand Up @@ -165,6 +176,37 @@ private function shouldSkipVariable(Variable $variable): bool
/** @var Scope|null $nodeScope */
$nodeScope = $variable->getAttribute(AttributeKey::SCOPE);

return $nodeScope === null;
if ($nodeScope === null) {
return true;
}

$variableName = $this->getName($variable);
if ($variableName === null) {
return true;
}

return false;
}

private function collectDefinedVariablesFromForeach(Node $node): void
{
if (! $node instanceof Foreach_) {
return;
}

$this->traverseNodesWithCallable($node->stmts, function (Node $node): void {
if ($node instanceof Assign || $node instanceof Node\Expr\AssignRef) {
if (! $node->var instanceof Variable) {
return;
}

$variableName = $this->getName($node->var);
if ($variableName === null) {
return;
}

$this->definedVariables[] = $variableName;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test(): void
__DIR__ . '/Fixture/take_static_into_account.php.inc',
// @see https://3v4l.org/ObtMn
__DIR__ . '/Fixture/skip_list.php.inc',
__DIR__ . '/Fixture/skip_foreach_assign.php.inc',
]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Php\Tests\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

class SkipForeachAssign
{
public function run(array $nodes)
{
$abstracts = array();

// Abstract numbering definition
if (true) {
foreach ($nodes as $node) {
$abstract = &$abstracts[$node];
}
}
}
}