Skip to content

Commit

Permalink
[Php56] Clean up AddDefaultValueForUndefinedVariableRector: remove un…
Browse files Browse the repository at this point in the history
…necessary repetitive apply and used from params (#1778)
  • Loading branch information
samsonasik committed Feb 8, 2022
1 parent 43c6725 commit 31cffa8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 33 deletions.
23 changes: 1 addition & 22 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
{
$undefinedVariables = [];

$variableNamesFromParams = $this->collectVariableNamesFromParams($node);
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (Node $node) use (
&$undefinedVariables,
$variableNamesFromParams
&$undefinedVariables
): ?int {
// entering new scope - break!
if ($node instanceof FunctionLike && ! $node instanceof ArrowFunction) {
Expand Down Expand Up @@ -84,10 +82,6 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return null;
}

if (in_array($variableName, $variableNamesFromParams, true)) {
return null;
}

$undefinedVariables[] = $variableName;

return null;
Expand All @@ -96,21 +90,6 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return array_unique($undefinedVariables);
}

/**
* @return string[]
*/
private function collectVariableNamesFromParams(ClassMethod | Function_ | Closure $node): array
{
$variableNames = [];
foreach ($node->getParams() as $param) {
if ($param->var instanceof Variable) {
$variableNames[] = (string) $this->nodeNameResolver->getName($param->var);
}
}

return $variableNames;
}

private function issetOrUnsetOrEmptyParent(Node $parentNode): bool
{
return in_array($parentNode::class, [Unset_::class, UnsetCast::class, Isset_::class, Empty_::class], true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@
*/
final class AddDefaultValueForUndefinedVariableRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var string
*/
private const ALREADY_ADDED_VARIABLE_NAMES = 'already_added_variable_names';

public function __construct(
private readonly UndefinedVariableResolver $undefinedVariableResolver,
private readonly InlineHTMLAnalyzer $inlineHTMLAnalyzer
Expand Down Expand Up @@ -99,10 +94,6 @@ public function refactor(Node $node): ?Node

$undefinedVariableNames = $this->undefinedVariableResolver->resolve($node);

// avoids adding same variable multiple tiemes
$alreadyAddedVariableNames = (array) $node->getAttribute(self::ALREADY_ADDED_VARIABLE_NAMES);
$undefinedVariableNames = array_diff($undefinedVariableNames, $alreadyAddedVariableNames);

if ($undefinedVariableNames === []) {
return null;
}
Expand All @@ -117,8 +108,6 @@ public function refactor(Node $node): ?Node
$variablesInitiation[] = new Expression($assign);
}

$node->setAttribute(self::ALREADY_ADDED_VARIABLE_NAMES, $undefinedVariableNames);

$node->stmts = array_merge($variablesInitiation, (array) $node->stmts);

return $node;
Expand Down

0 comments on commit 31cffa8

Please sign in to comment.