|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection\ReflectionProvider; |
| 4 | + |
| 5 | +use PHPStan\Analyser\Scope; |
| 6 | +use PHPStan\Reflection\ClassReflection; |
| 7 | +use PHPStan\Reflection\FunctionReflection; |
| 8 | +use PHPStan\Reflection\GlobalConstantReflection; |
| 9 | +use PHPStan\Reflection\ReflectionProvider; |
| 10 | + |
| 11 | +class MemoizingReflectionProvider implements ReflectionProvider |
| 12 | +{ |
| 13 | + |
| 14 | + /** @var \PHPStan\Reflection\ReflectionProvider */ |
| 15 | + private $provider; |
| 16 | + |
| 17 | + /** @var array<string, bool> */ |
| 18 | + private $hasClasses = []; |
| 19 | + |
| 20 | + /** @var array<string, \PHPStan\Reflection\ClassReflection> */ |
| 21 | + private $classes = []; |
| 22 | + |
| 23 | + /** @var array<string, string> */ |
| 24 | + private $classNames = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param \PHPStan\Reflection\ReflectionProvider $provider |
| 28 | + */ |
| 29 | + public function __construct(ReflectionProvider $provider) |
| 30 | + { |
| 31 | + $this->provider = $provider; |
| 32 | + } |
| 33 | + |
| 34 | + public function hasClass(string $className): bool |
| 35 | + { |
| 36 | + if (isset($this->hasClasses[$className])) { |
| 37 | + return $this->hasClasses[$className]; |
| 38 | + } |
| 39 | + |
| 40 | + return $this->hasClasses[$className] = $this->provider->hasClass($className); |
| 41 | + } |
| 42 | + |
| 43 | + public function getClass(string $className): ClassReflection |
| 44 | + { |
| 45 | + if (isset($this->classes[$className])) { |
| 46 | + return $this->classes[$className]; |
| 47 | + } |
| 48 | + |
| 49 | + return $this->classes[$className] = $this->provider->getClass($className); |
| 50 | + } |
| 51 | + |
| 52 | + public function getClassName(string $className): string |
| 53 | + { |
| 54 | + if (isset($this->classNames[$className])) { |
| 55 | + return $this->classNames[$className]; |
| 56 | + } |
| 57 | + |
| 58 | + return $this->classNames[$className] = $this->provider->getClassName($className); |
| 59 | + } |
| 60 | + |
| 61 | + public function supportsAnonymousClasses(): bool |
| 62 | + { |
| 63 | + return $this->provider->supportsAnonymousClasses(); |
| 64 | + } |
| 65 | + |
| 66 | + public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNode, Scope $scope): ClassReflection |
| 67 | + { |
| 68 | + return $this->provider->getAnonymousClassReflection($classNode, $scope); |
| 69 | + } |
| 70 | + |
| 71 | + public function hasFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool |
| 72 | + { |
| 73 | + return $this->provider->hasFunction($nameNode, $scope); |
| 74 | + } |
| 75 | + |
| 76 | + public function getFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): FunctionReflection |
| 77 | + { |
| 78 | + return $this->provider->getFunction($nameNode, $scope); |
| 79 | + } |
| 80 | + |
| 81 | + public function resolveFunctionName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string |
| 82 | + { |
| 83 | + return $this->provider->resolveFunctionName($nameNode, $scope); |
| 84 | + } |
| 85 | + |
| 86 | + public function hasConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool |
| 87 | + { |
| 88 | + return $this->provider->hasConstant($nameNode, $scope); |
| 89 | + } |
| 90 | + |
| 91 | + public function getConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): GlobalConstantReflection |
| 92 | + { |
| 93 | + return $this->provider->getConstant($nameNode, $scope); |
| 94 | + } |
| 95 | + |
| 96 | + public function resolveConstantName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string |
| 97 | + { |
| 98 | + return $this->provider->resolveConstantName($nameNode, $scope); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments