1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Analyser;
4:
5: use PHPStan\PhpDoc\Tag\TemplateTag;
6: use PHPStan\PhpDoc\Tag\TypeAliasImportTag;
7: use PHPStan\Type\Generic\TemplateTypeMap;
8: use PHPStan\Type\Generic\TemplateTypeScope;
9: use PHPStan\Type\Type;
10: use PHPStan\Type\TypeAlias;
11: use function array_key_exists;
12: use function array_merge;
13: use function array_shift;
14: use function count;
15: use function explode;
16: use function implode;
17: use function ltrim;
18: use function sprintf;
19: use function str_starts_with;
20: use function strtolower;
21:
22: /**
23: * @api
24: */
25: final class NameScope
26: {
27:
28: private TemplateTypeMap $templateTypeMap;
29:
30: /**
31: * @api
32: * @param non-empty-string|null $namespace
33: * @param array<string, string> $uses alias(string) => fullName(string)
34: * @param array<string, string> $constUses alias(string) => fullName(string)
35: * @param array<string, TemplateTag> $templateTags
36: * @param array<string, true> $typeAliasesMap
37: * @param array<string, array{TypeAlias|TypeAliasImportTag, string}> $functionTypeAliases aliases declared or imported above the enclosing functions and methods, with the key they are cached under
38: */
39: public function __construct(
40: private ?string $namespace,
41: private array $uses,
42: private ?string $className = null,
43: private ?string $functionName = null,
44: ?TemplateTypeMap $templateTypeMap = null,
45: private array $templateTags = [],
46: private array $typeAliasesMap = [],
47: private bool $bypassTypeAliases = false,
48: private array $constUses = [],
49: private ?string $typeAliasClassName = null,
50: private ?string $fileName = null,
51: private array $functionTypeAliases = [],
52: )
53: {
54: $this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty();
55: }
56:
57: /**
58: * The file the PHPDoc resolved in this scope is in, if it is in one.
59: */
60: public function getFileName(): ?string
61: {
62: return $this->fileName;
63: }
64:
65: public function getNamespace(): ?string
66: {
67: return $this->namespace;
68: }
69:
70: /**
71: * @return array<string, string>
72: */
73: public function getUses(): array
74: {
75: return $this->uses;
76: }
77:
78: public function hasUseAlias(string $name): bool
79: {
80: return isset($this->uses[strtolower($name)]);
81: }
82:
83: /**
84: * @return array<string, string>
85: */
86: public function getConstUses(): array
87: {
88: return $this->constUses;
89: }
90:
91: public function getClassName(): ?string
92: {
93: return $this->className;
94: }
95:
96: public function getFunctionName(): ?string
97: {
98: return $this->functionName;
99: }
100:
101: public function getClassNameForTypeAlias(): ?string
102: {
103: return $this->typeAliasClassName ?? $this->className;
104: }
105:
106: public function resolveStringName(string $name): string
107: {
108: if (str_starts_with($name, '\\')) {
109: return ltrim($name, '\\');
110: }
111:
112: $nameParts = explode('\\', $name);
113: $firstNamePart = strtolower($nameParts[0]);
114: if (isset($this->uses[$firstNamePart])) {
115: if (count($nameParts) === 1) {
116: return $this->uses[$firstNamePart];
117: }
118: array_shift($nameParts);
119: return sprintf('%s\\%s', $this->uses[$firstNamePart], implode('\\', $nameParts));
120: }
121:
122: if ($this->namespace !== null) {
123: return sprintf('%s\\%s', $this->namespace, $name);
124: }
125:
126: return $name;
127: }
128:
129: /**
130: * @return non-empty-list<string>
131: */
132: public function resolveConstantNames(string $name): array
133: {
134: if (str_starts_with($name, '\\')) {
135: return [ltrim($name, '\\')];
136: }
137:
138: $nameParts = explode('\\', $name);
139: $firstNamePart = strtolower($nameParts[0]);
140:
141: if (count($nameParts) > 1) {
142: if (isset($this->uses[$firstNamePart])) {
143: array_shift($nameParts);
144: return [sprintf('%s\\%s', $this->uses[$firstNamePart], implode('\\', $nameParts))];
145: }
146: } elseif (isset($this->constUses[$firstNamePart])) {
147: return [$this->constUses[$firstNamePart]];
148: }
149:
150: if ($this->namespace !== null) {
151: return [
152: sprintf('%s\\%s', $this->namespace, $name),
153: $name,
154: ];
155: }
156:
157: return [$name];
158: }
159:
160: public function getTemplateTypeScope(): ?TemplateTypeScope
161: {
162: if ($this->className !== null) {
163: if ($this->functionName !== null) {
164: return TemplateTypeScope::createWithMethod($this->className, $this->functionName);
165: }
166:
167: return TemplateTypeScope::createWithClass($this->className);
168: }
169:
170: if ($this->functionName !== null) {
171: return TemplateTypeScope::createWithFunction($this->functionName);
172: }
173:
174: return null;
175: }
176:
177: public function getTemplateTypeMap(): TemplateTypeMap
178: {
179: return $this->templateTypeMap;
180: }
181:
182: /**
183: * @return array<string, TemplateTag>
184: */
185: public function getTemplateTags(): array
186: {
187: return $this->templateTags;
188: }
189:
190: public function resolveTemplateTypeName(string $name): ?Type
191: {
192: return $this->templateTypeMap->getType($name);
193: }
194:
195: /**
196: * @param array<string, TemplateTag> $templateTags
197: */
198: public function withTemplateTypeMap(TemplateTypeMap $map, array $templateTags): self
199: {
200: if ($map->isEmpty() && $this->templateTypeMap->isEmpty()) {
201: return $this;
202: }
203:
204: return new self(
205: $this->namespace,
206: $this->uses,
207: $this->className,
208: $this->functionName,
209: new TemplateTypeMap(array_merge(
210: $this->templateTypeMap->getTypes(),
211: $map->getTypes(),
212: )),
213: $templateTags,
214: $this->typeAliasesMap,
215: $this->bypassTypeAliases,
216: $this->constUses,
217: fileName: $this->fileName,
218: functionTypeAliases: $this->functionTypeAliases,
219: );
220: }
221:
222: public function withoutNamespaceAndUses(): self
223: {
224: return new self(
225: null,
226: [],
227: $this->className,
228: $this->functionName,
229: $this->templateTypeMap,
230: $this->templateTags,
231: $this->typeAliasesMap,
232: $this->bypassTypeAliases,
233: $this->constUses,
234: fileName: $this->fileName,
235: functionTypeAliases: $this->functionTypeAliases,
236: );
237: }
238:
239: public function withClassName(string $className): self
240: {
241: return new self(
242: $this->namespace,
243: $this->uses,
244: $className,
245: $this->functionName,
246: $this->templateTypeMap,
247: $this->templateTags,
248: $this->typeAliasesMap,
249: $this->bypassTypeAliases,
250: $this->constUses,
251: fileName: $this->fileName,
252: functionTypeAliases: $this->functionTypeAliases,
253: );
254: }
255:
256: public function unsetTemplateType(string $name): self
257: {
258: $map = $this->templateTypeMap;
259: if (!$map->hasType($name)) {
260: return $this;
261: }
262:
263: return new self(
264: $this->namespace,
265: $this->uses,
266: $this->className,
267: $this->functionName,
268: $this->templateTypeMap->unsetType($name),
269: $this->templateTags,
270: $this->typeAliasesMap,
271: $this->bypassTypeAliases,
272: $this->constUses,
273: fileName: $this->fileName,
274: functionTypeAliases: $this->functionTypeAliases,
275: );
276: }
277:
278: public function bypassTypeAliases(): self
279: {
280: return new self($this->namespace, $this->uses, $this->className, $this->functionName, $this->templateTypeMap, $this->templateTags, $this->typeAliasesMap, true, $this->constUses, fileName: $this->fileName, functionTypeAliases: $this->functionTypeAliases);
281: }
282:
283: public function shouldBypassTypeAliases(): bool
284: {
285: return $this->bypassTypeAliases;
286: }
287:
288: public function hasTypeAlias(string $alias): bool
289: {
290: return array_key_exists($alias, $this->typeAliasesMap) || array_key_exists($alias, $this->functionTypeAliases);
291: }
292:
293: /**
294: * An alias declared or imported above an enclosing function or method, with the key it is cached under.
295: * It takes precedence over an alias of the class.
296: *
297: * @return array{TypeAlias|TypeAliasImportTag, string}|null
298: */
299: public function getFunctionTypeAlias(string $alias): ?array
300: {
301: return $this->functionTypeAliases[$alias] ?? null;
302: }
303:
304: /**
305: * @return array<string, array{TypeAlias|TypeAliasImportTag, string}>
306: */
307: public function getFunctionTypeAliases(): array
308: {
309: return $this->functionTypeAliases;
310: }
311:
312: /**
313: * @param callable(self): array<string, array{TypeAlias|TypeAliasImportTag, string}> $createFunctionTypeAliases gets the new name scope,
314: * which the aliases declared in it resolve in
315: */
316: public function withFunctionTypeAliases(callable $createFunctionTypeAliases): self
317: {
318: $self = clone $this;
319: $self->functionTypeAliases = $createFunctionTypeAliases($self);
320:
321: return $self;
322: }
323:
324: }
325: