|
2 | 2 |
|
3 | 3 | namespace PHPStan\Analyser; |
4 | 4 |
|
5 | | -use Override; |
6 | 5 | use PhpParser\Node; |
7 | 6 | use PhpParser\Node\Expr; |
8 | | -use PhpParser\NodeTraverser; |
9 | | -use PhpParser\NodeVisitor; |
10 | | -use PhpParser\NodeVisitorAbstract; |
11 | 7 | use PHPStan\Analyser\Traverser\VoidToNullTraverser; |
12 | 8 | use PHPStan\DependencyInjection\AutowiredExtensions; |
13 | 9 | use PHPStan\DependencyInjection\ExtensionsCollection; |
|
19 | 15 | use PHPStan\Type\TypeUtils; |
20 | 16 | use PHPStan\Type\UnionType; |
21 | 17 | use function array_keys; |
| 18 | +use function is_array; |
22 | 19 | use function is_string; |
23 | 20 | use function spl_object_id; |
24 | 21 |
|
@@ -769,44 +766,71 @@ public function onNonNullabilityDevicedScopes(MutatingScope $beforeScope, Mutati |
769 | 766 | */ |
770 | 767 | private function getReadVariableNames(): array |
771 | 768 | { |
772 | | - if ($this->readVariableNames !== null) { |
773 | | - return $this->readVariableNames; |
774 | | - } |
| 769 | + return $this->readVariableNames ??= self::collectReadVariableNames($this->expr); |
| 770 | + } |
775 | 771 |
|
776 | | - $visitor = new class extends NodeVisitorAbstract { |
| 772 | + /** |
| 773 | + * The names are pure syntax, so they cache on the AST node itself as an |
| 774 | + * attribute (sharing the node's own lifetime) - a deep fetch/call chain |
| 775 | + * composes each link's set from its child's cached set in O(1) amortized |
| 776 | + * instead of re-traversing the whole subtree per link (and per |
| 777 | + * loop-convergence pass, which recreates the results). |
| 778 | + */ |
| 779 | + private const READ_VARIABLE_NAMES_ATTRIBUTE = 'readVariableNames'; |
777 | 780 |
|
778 | | - /** @var array<string, true> */ |
779 | | - public array $names = []; |
| 781 | + /** |
| 782 | + * @return list<string> |
| 783 | + */ |
| 784 | + private static function collectReadVariableNames(Node $node): array |
| 785 | + { |
| 786 | + if ($node instanceof Expr) { |
| 787 | + /** @var list<string>|null $cached */ |
| 788 | + $cached = $node->getAttribute(self::READ_VARIABLE_NAMES_ATTRIBUTE); |
| 789 | + if ($cached !== null) { |
| 790 | + return $cached; |
| 791 | + } |
| 792 | + } |
780 | 793 |
|
781 | | - #[Override] |
782 | | - public function enterNode(Node $node): ?int |
783 | | - { |
784 | | - if ($node instanceof Expr\Variable && is_string($node->name) && $node->name !== 'this') { |
785 | | - $this->names[$node->name] = true; |
| 794 | + $names = []; |
| 795 | + if ($node instanceof Expr\Variable && is_string($node->name) && $node->name !== 'this') { |
| 796 | + $names[$node->name] = true; |
| 797 | + } |
| 798 | + if ($node instanceof Expr\Closure) { |
| 799 | + // a closure body's variables live in its own scope - only the |
| 800 | + // use() clause reads the enclosing position. Arrow functions |
| 801 | + // capture implicitly and are traversed. |
| 802 | + foreach ($node->uses as $use) { |
| 803 | + if (!is_string($use->var->name)) { |
| 804 | + continue; |
786 | 805 | } |
787 | | - // a closure body's variables live in its own scope - only the |
788 | | - // use() clause reads the enclosing position. Arrow functions |
789 | | - // capture implicitly and are traversed. |
790 | | - if ($node instanceof Expr\Closure) { |
791 | | - foreach ($node->uses as $use) { |
792 | | - if (!is_string($use->var->name)) { |
| 806 | + $names[$use->var->name] = true; |
| 807 | + } |
| 808 | + } else { |
| 809 | + foreach ($node->getSubNodeNames() as $subNodeName) { |
| 810 | + $subNode = $node->$subNodeName; |
| 811 | + if ($subNode instanceof Node) { |
| 812 | + foreach (self::collectReadVariableNames($subNode) as $name) { |
| 813 | + $names[$name] = true; |
| 814 | + } |
| 815 | + } elseif (is_array($subNode)) { |
| 816 | + foreach ($subNode as $item) { |
| 817 | + if (!$item instanceof Node) { |
793 | 818 | continue; |
794 | 819 | } |
795 | | - $this->names[$use->var->name] = true; |
| 820 | + foreach (self::collectReadVariableNames($item) as $name) { |
| 821 | + $names[$name] = true; |
| 822 | + } |
796 | 823 | } |
797 | | - |
798 | | - return NodeVisitor::DONT_TRAVERSE_CHILDREN; |
799 | 824 | } |
800 | | - |
801 | | - return null; |
802 | 825 | } |
| 826 | + } |
803 | 827 |
|
804 | | - }; |
805 | | - $traverser = new NodeTraverser(); |
806 | | - $traverser->addVisitor($visitor); |
807 | | - $traverser->traverse([$this->expr]); |
| 828 | + $result = array_keys($names); |
| 829 | + if ($node instanceof Expr) { |
| 830 | + $node->setAttribute(self::READ_VARIABLE_NAMES_ATTRIBUTE, $result); |
| 831 | + } |
808 | 832 |
|
809 | | - return $this->readVariableNames = array_keys($visitor->names); |
| 833 | + return $result; |
810 | 834 | } |
811 | 835 |
|
812 | 836 | } |
0 commit comments