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