| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Generic; |
| 4: | |
| 5: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 6: | use function sprintf; |
| 7: | |
| 8: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateTypeScope.cpp')] |
| 9: | final class TemplateTypeScope |
| 10: | { |
| 11: | |
| 12: | public static function createWithAnonymousFunction(): self |
| 13: | { |
| 14: | return new self(null, null); |
| 15: | } |
| 16: | |
| 17: | public static function createWithFunction(string $functionName): self |
| 18: | { |
| 19: | return new self(null, $functionName); |
| 20: | } |
| 21: | |
| 22: | public static function createWithMethod(string $className, string $functionName): self |
| 23: | { |
| 24: | return new self($className, $functionName); |
| 25: | } |
| 26: | |
| 27: | public static function createWithClass(string $className): self |
| 28: | { |
| 29: | return new self($className, null); |
| 30: | } |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | public static function createWithType(string $typeDescription): self |
| 40: | { |
| 41: | return new self(null, null, $typeDescription); |
| 42: | } |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | public static function createWithArrayKey(string $typeDescription): self |
| 53: | { |
| 54: | return new self(null, null, $typeDescription, true); |
| 55: | } |
| 56: | |
| 57: | private function __construct(private ?string $className, private ?string $functionName, private ?string $typeDescription = null, private bool $arrayKey = false) |
| 58: | { |
| 59: | } |
| 60: | |
| 61: | |
| 62: | public function getClassName(): ?string |
| 63: | { |
| 64: | return $this->className; |
| 65: | } |
| 66: | |
| 67: | |
| 68: | public function getFunctionName(): ?string |
| 69: | { |
| 70: | return $this->functionName; |
| 71: | } |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | public function getTypeDescription(): ?string |
| 79: | { |
| 80: | return $this->typeDescription; |
| 81: | } |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | public function isArrayKey(): bool |
| 89: | { |
| 90: | return $this->arrayKey; |
| 91: | } |
| 92: | |
| 93: | |
| 94: | public function equals(self $other): bool |
| 95: | { |
| 96: | return $this->className === $other->className |
| 97: | && $this->functionName === $other->functionName |
| 98: | && $this->typeDescription === $other->typeDescription |
| 99: | && $this->arrayKey === $other->arrayKey; |
| 100: | } |
| 101: | |
| 102: | |
| 103: | public function describe(): string |
| 104: | { |
| 105: | if ($this->typeDescription !== null) { |
| 106: | return sprintf('type %s', $this->typeDescription); |
| 107: | } |
| 108: | |
| 109: | if ($this->className === null && $this->functionName === null) { |
| 110: | return 'anonymous function'; |
| 111: | } |
| 112: | |
| 113: | if ($this->className === null) { |
| 114: | return sprintf('function %s()', $this->functionName); |
| 115: | } |
| 116: | |
| 117: | if ($this->functionName === null) { |
| 118: | return sprintf('class %s', $this->className); |
| 119: | } |
| 120: | |
| 121: | return sprintf('method %s::%s()', $this->className, $this->functionName); |
| 122: | } |
| 123: | |
| 124: | } |
| 125: | |