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: * For templates a callable type declares for itself, like the T of callable<T>(T): T
34: * or of Closure<T>(T): T.
35: *
36: * The scope names the type that declares the template, as it is written. A template
37: * that shadows another one already in scope is reported, see NestedTemplateTypeRuleHelper.
38: */
39: public static function createWithType(string $typeDescription): self
40: {
41: return new self(null, null, $typeDescription);
42: }
43:
44: /**
45: * For a template an array key declares, like the T of class-string-map<T of Foo, list<T>>:
46: * the value at each key is the item type with T standing for what that key is.
47: *
48: * A key only declares a template with `of` (or `as`), except in class-string-map. A key that refers
49: * to a template declared elsewhere, like array<class-string<T>, T> in callable<T>(...),
50: * keeps the meaning of an ordinary key.
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: /** @api */
62: public function getClassName(): ?string
63: {
64: return $this->className;
65: }
66:
67: /** @api */
68: public function getFunctionName(): ?string
69: {
70: return $this->functionName;
71: }
72:
73: /**
74: * The type declaring the templates, as written, for a scope created with createWithType().
75: *
76: * @api
77: */
78: public function getTypeDescription(): ?string
79: {
80: return $this->typeDescription;
81: }
82:
83: /**
84: * Whether the scope was created with createWithArrayKey().
85: *
86: * @api
87: */
88: public function isArrayKey(): bool
89: {
90: return $this->arrayKey;
91: }
92:
93: /** @api */
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: /** @api */
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: