Skip to content

Commit

Permalink
[Performance] Only set Attribute on context inside ArrayDimFetch and …
Browse files Browse the repository at this point in the history
…ArrayItem on specific node used (#4493)

* [Performance] Only set Attribute on context inside ArrayDimFetch and ArrayItem on specific node needed

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 12, 2023
1 parent 706be90 commit 2939136
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
Expand Down Expand Up @@ -41,6 +44,20 @@ public function __construct(
) {
}

private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void
{
if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {
$arrayDimFetch->var->setAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, true);
}
}

private function processInsideArrayItem(ArrayItem $arrayItem): void
{
if ($arrayItem->value instanceof Match_) {
$arrayItem->value->setAttribute(AttributeKey::INSIDE_ARRAY_ITEM, true);
}
}

public function enterNode(Node $node): ?Node
{
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_) {
Expand All @@ -49,11 +66,13 @@ public function enterNode(Node $node): ?Node
}

if ($node instanceof ArrayDimFetch) {
$node->var->setAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, true);
$this->processInsideArrayDimFetch($node);
return null;
}

if ($node instanceof ArrayItem) {
$node->value->setAttribute(AttributeKey::INSIDE_ARRAY_ITEM, true);
if ($node instanceof ArrayItem && $node->value instanceof Match_) {
$this->processInsideArrayItem($node);
return null;
}

if ($node instanceof Isset_ || $node instanceof Unset_) {
Expand All @@ -73,14 +92,17 @@ public function enterNode(Node $node): ?Node

if ($node instanceof Return_ && $node->expr instanceof Expr) {
$node->expr->setAttribute(AttributeKey::IS_RETURN_EXPR, true);
return null;
}

if ($node instanceof Arg) {
$node->value->setAttribute(AttributeKey::IS_ARG_VALUE, true);
return null;
}

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

$this->processContextInClass($node);
Expand Down

0 comments on commit 2939136

Please sign in to comment.