Skip to content

Commit

Permalink
[Php72] Remove parent attribute on AnonymousFunctionFactory (#4443)
Browse files Browse the repository at this point in the history
* [Php72] Remove parent attribute on AnonymousFunctionFactory

* check is param value

* check is param value

* check is param value

* Fix variable loop

* Fix variable loop

* Fix phpstan
  • Loading branch information
samsonasik committed Jul 9, 2023
1 parent c7a7046 commit 3b6084d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
10 changes: 10 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Expand Up @@ -232,6 +232,11 @@ final class AttributeKey
*/
public const IS_IN_LOOP = 'is_in_loop';

/**
* @var string
*/
public const IS_VARIABLE_LOOP = 'is_variable_loop';

/**
* @var string
*/
Expand Down Expand Up @@ -292,6 +297,11 @@ final class AttributeKey
*/
public const IS_ARG_VALUE = 'is_arg_value';

/**
* @var string
*/
public const IS_PARAM_VAR = 'IS_PARAM_VAR';

/**
* @var string
*/
Expand Down
Expand Up @@ -12,6 +12,8 @@
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
Expand Down Expand Up @@ -54,16 +56,8 @@ public function enterNode(Node $node): ?Node
}

if ($node instanceof Attribute) {
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$node->args,
static function (Node $subNode) {
if ($subNode instanceof Array_) {
$subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
}

return null;
}
);
$this->processContextInAttribute($node);
return null;
}

if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) {
Expand All @@ -79,9 +73,27 @@ static function (Node $subNode) {
$node->value->setAttribute(AttributeKey::IS_ARG_VALUE, true);
}

if ($node instanceof Param) {
$node->var->setAttribute(AttributeKey::IS_PARAM_VAR, true);
}

return null;
}

private function processContextInAttribute(Attribute $attribute): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$attribute->args,
static function (Node $subNode) {
if ($subNode instanceof Array_) {
$subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
}

return null;
}
);
}

private function processContextInIssetOrUnset(Isset_|Unset_ $node): void
{
if ($node instanceof Isset_) {
Expand All @@ -108,6 +120,14 @@ private function processContextInIf(If_|Else_|ElseIf_ $node): void

private function processContextInLoop(For_|Foreach_|While_|Do_ $node): void
{
if ($node instanceof Foreach_) {
if ($node->keyVar instanceof Variable) {
$node->keyVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}

$node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$node->stmts,
static function (Node $subNode): ?int {
Expand Down
11 changes: 5 additions & 6 deletions rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Expand Up @@ -229,12 +229,11 @@ private function createUseVariablesFromParams(array $nodes, array $params): arra
continue;
}

$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Node && in_array(
$parentNode::class,
[Assign::class, Foreach_::class, Param::class],
true
)) {
if (
$variable->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true
|| $variable->getAttribute(AttributeKey::IS_PARAM_VAR) === true
|| $variable->getAttribute(AttributeKey::IS_VARIABLE_LOOP) === true
) {
$alreadyAssignedVariables[] = $variableName;
}

Expand Down
3 changes: 3 additions & 0 deletions src/PhpParser/Parser/SimplePhpParser.php
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Rector\Core\PhpParser\NodeTraverser\NodeConnectingTraverser;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\AssignedToNodeVisitor;

final class SimplePhpParser
{
Expand All @@ -19,6 +20,8 @@ public function __construct(
) {
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7);

$this->nodeConnectingTraverser->addVisitor(new AssignedToNodeVisitor());
}

/**
Expand Down

0 comments on commit 3b6084d

Please sign in to comment.