From a83130e25c515f2b52c753dda415c2e831b4b842 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 Jun 2023 23:29:12 +0700 Subject: [PATCH 1/3] [NodeTypeResolver] Remove callable traverse on ContextNodeVisitor --- .../Scope/NodeVisitor/ContextNodeVisitor.php | 59 +++++-------------- 1 file changed, 14 insertions(+), 45 deletions(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index 2dd4c03cf74..49700c624a0 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -7,32 +7,22 @@ use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Stmt\Break_; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\ElseIf_; use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; -use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Unset_; use PhpParser\Node\Stmt\While_; -use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; -use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; final class ContextNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface { - public function __construct( - private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser - ) { - } - public function enterNode(Node $node): ?Node { if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_) { @@ -46,16 +36,13 @@ 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; + foreach ($node->args as $arg) { + if ($arg->value instanceof Array_) { + $arg->value->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true); } - ); + } + + return null; } if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) { @@ -83,37 +70,19 @@ private function processContextInIssetOrUnset(Isset_|Unset_ $node): void private function processContextInIf(If_|Else_|ElseIf_ $node): void { - $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - $node->stmts, - static function (Node $subNode): ?int { - if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; - } - - if ($subNode instanceof Break_) { - $subNode->setAttribute(AttributeKey::IS_IN_IF, true); - } - - return null; + foreach ($node->stmts as $stmt) { + if ($stmt instanceof Break_) { + $stmt->setAttribute(AttributeKey::IS_IN_IF, true); } - ); + } } private function processContextInLoop(For_|Foreach_|While_|Do_ $node): void { - $this->simpleCallableNodeTraverser->traverseNodesWithCallable( - $node->stmts, - static function (Node $subNode): ?int { - if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { - return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; - } - - if ($subNode instanceof If_ || $subNode instanceof Break_) { - $subNode->setAttribute(AttributeKey::IS_IN_LOOP, true); - } - - return null; + foreach ($node->stmts as $stmt) { + if ($stmt instanceof If_ || $stmt instanceof Break_) { + $stmt->setAttribute(AttributeKey::IS_IN_LOOP, true); } - ); + } } } From e38312288866aedcba5914ee9f8993edfbb356ae Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 Jun 2023 23:37:29 +0700 Subject: [PATCH 2/3] reduce instead --- .../Scope/NodeVisitor/ContextNodeVisitor.php | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index 49700c624a0..78ad595ddd9 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -7,22 +7,32 @@ use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Stmt\Break_; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\ElseIf_; use PhpParser\Node\Stmt\For_; use PhpParser\Node\Stmt\Foreach_; +use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Unset_; use PhpParser\Node\Stmt\While_; +use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; +use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; final class ContextNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface { + public function __construct( + private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser + ) { + } + public function enterNode(Node $node): ?Node { if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_) { @@ -36,13 +46,16 @@ public function enterNode(Node $node): ?Node } if ($node instanceof Attribute) { - foreach ($node->args as $arg) { - if ($arg->value instanceof Array_) { - $arg->value->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true); - } - } + $this->simpleCallableNodeTraverser->traverseNodesWithCallable( + $node->args, + static function (Node $subNode) { + if ($subNode instanceof Array_) { + $subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true); + } - return null; + return null; + } + ); } if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) { @@ -71,18 +84,25 @@ private function processContextInIssetOrUnset(Isset_|Unset_ $node): void private function processContextInIf(If_|Else_|ElseIf_ $node): void { foreach ($node->stmts as $stmt) { - if ($stmt instanceof Break_) { - $stmt->setAttribute(AttributeKey::IS_IN_IF, true); - } + $stmt->setAttribute(AttributeKey::IS_IN_IF, true); } } private function processContextInLoop(For_|Foreach_|While_|Do_ $node): void { - foreach ($node->stmts as $stmt) { - if ($stmt instanceof If_ || $stmt instanceof Break_) { - $stmt->setAttribute(AttributeKey::IS_IN_LOOP, true); + $this->simpleCallableNodeTraverser->traverseNodesWithCallable( + $node->stmts, + static function (Node $subNode): ?int { + if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + + if ($subNode instanceof If_ || $subNode instanceof Break_) { + $subNode->setAttribute(AttributeKey::IS_IN_LOOP, true); + } + + return null; } - } + ); } } From d1e7bb5de6e6ee1515d5a54726f98b46c2c26665 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 22 Jun 2023 23:39:23 +0700 Subject: [PATCH 3/3] reduce instead --- .../PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index 78ad595ddd9..c3b2fd58f3f 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -84,7 +84,9 @@ private function processContextInIssetOrUnset(Isset_|Unset_ $node): void private function processContextInIf(If_|Else_|ElseIf_ $node): void { foreach ($node->stmts as $stmt) { - $stmt->setAttribute(AttributeKey::IS_IN_IF, true); + if ($stmt instanceof Break_) { + $stmt->setAttribute(AttributeKey::IS_IN_IF, true); + } } }