|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Dependency; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use function sprintf; |
| 7 | +use function strtolower; |
| 8 | + |
| 9 | +/** |
| 10 | + * Keeps the namespace and the use statements seen so far while walking a file's statements. |
| 11 | + * |
| 12 | + * Both places that build exported nodes drive one of these - ExportedNodeVisitor for a result |
| 13 | + * cache restore, DependencyResolver during the analysis - so that the PHPDoc scope recorded in |
| 14 | + * the cache and the one computed when checking the cache are produced by the same code. |
| 15 | + * |
| 16 | + * It mirrors what FileTypeMapper does while building a name scope map, except that the uses are |
| 17 | + * cleared when a namespace is entered rather than when it is left. Namespace blocks cover every |
| 18 | + * statement of a file, so nothing can observe the difference. |
| 19 | + */ |
| 20 | +final class ExportedNameScopeTracker |
| 21 | +{ |
| 22 | + |
| 23 | + /** @var non-empty-string|null */ |
| 24 | + private ?string $namespace = null; |
| 25 | + |
| 26 | + /** @var array<string, string> */ |
| 27 | + private array $uses = []; |
| 28 | + |
| 29 | + /** @var array<string, string> */ |
| 30 | + private array $constUses = []; |
| 31 | + |
| 32 | + private ?ExportedNameScope $nameScope = null; |
| 33 | + |
| 34 | + public function reset(): void |
| 35 | + { |
| 36 | + $this->namespace = null; |
| 37 | + $this->uses = []; |
| 38 | + $this->constUses = []; |
| 39 | + $this->nameScope = null; |
| 40 | + } |
| 41 | + |
| 42 | + public function enterNode(Node $node): void |
| 43 | + { |
| 44 | + if ($node instanceof Node\Stmt\Namespace_) { |
| 45 | + $this->namespace = $node->name !== null ? $node->name->toString() : null; |
| 46 | + $this->uses = []; |
| 47 | + $this->constUses = []; |
| 48 | + } elseif ($node instanceof Node\Stmt\Use_) { |
| 49 | + if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) { |
| 50 | + foreach ($node->uses as $use) { |
| 51 | + $this->uses[strtolower($use->getAlias()->name)] = $use->name->toString(); |
| 52 | + } |
| 53 | + } elseif ($node->type === Node\Stmt\Use_::TYPE_CONSTANT) { |
| 54 | + foreach ($node->uses as $use) { |
| 55 | + $this->constUses[strtolower($use->getAlias()->name)] = $use->name->toString(); |
| 56 | + } |
| 57 | + } else { |
| 58 | + return; |
| 59 | + } |
| 60 | + } elseif ($node instanceof Node\Stmt\GroupUse) { |
| 61 | + $prefix = $node->prefix->toString(); |
| 62 | + foreach ($node->uses as $use) { |
| 63 | + if ($node->type === Node\Stmt\Use_::TYPE_NORMAL || $use->type === Node\Stmt\Use_::TYPE_NORMAL) { |
| 64 | + $this->uses[strtolower($use->getAlias()->name)] = sprintf('%s\\%s', $prefix, $use->name->toString()); |
| 65 | + } elseif ($node->type === Node\Stmt\Use_::TYPE_CONSTANT || $use->type === Node\Stmt\Use_::TYPE_CONSTANT) { |
| 66 | + $this->constUses[strtolower($use->getAlias()->name)] = sprintf('%s\\%s', $prefix, $use->name->toString()); |
| 67 | + } |
| 68 | + } |
| 69 | + } else { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $this->nameScope = null; |
| 74 | + } |
| 75 | + |
| 76 | + public function getNameScope(): ExportedNameScope |
| 77 | + { |
| 78 | + return $this->nameScope ??= new ExportedNameScope($this->namespace, $this->uses, $this->constUses); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments