|
4 | 4 |
|
5 | 5 | namespace Typhoon\PHPStanTypeParser; |
6 | 6 |
|
7 | | -use Typhoon\Type; |
| 7 | +use Typhoon\Type\TemplateT; |
8 | 8 |
|
9 | | -/** |
10 | | - * @api |
11 | | - */ |
12 | | -interface Context |
| 9 | +final class Context |
13 | 10 | { |
14 | 11 | /** |
15 | | - * @param non-empty-string $unresolvedName |
16 | | - * @return array{non-empty-string, ?non-empty-string} |
| 12 | + * @var array<non-empty-lowercase-string, Name> |
17 | 13 | */ |
18 | | - public function resolveConstantName(string $unresolvedName): array; |
| 14 | + private array $importTable = []; |
19 | 15 |
|
20 | 16 | /** |
21 | | - * @param non-empty-string $unresolvedName |
22 | | - * @return class-string |
| 17 | + * @var array<non-empty-string, TemplateT> |
23 | 18 | */ |
24 | | - public function resolveClassName(string $unresolvedName): string; |
| 19 | + private array $templates = []; |
25 | 20 |
|
26 | | - /** |
27 | | - * @param non-empty-string $unresolvedName |
28 | | - * @param list<Type> $templateArguments |
29 | | - */ |
30 | | - public function resolveNameAsType(string $unresolvedName, array $templateArguments = []): Type; |
| 21 | + public function __construct( |
| 22 | + public readonly ?Name $namespace = null, |
| 23 | + ) {} |
| 24 | + |
| 25 | + public function use(Name $name, ?Name $as = null): self |
| 26 | + { |
| 27 | + $context = clone $this; |
| 28 | + $context->importTable[($as ?? $name)->toLowercaseString()] = $name; |
| 29 | + |
| 30 | + return $context; |
| 31 | + } |
| 32 | + |
| 33 | + public function template(TemplateT $template): self |
| 34 | + { |
| 35 | + $context = clone $this; |
| 36 | + $context->templates[$template->name] = $template; |
| 37 | + |
| 38 | + return $context; |
| 39 | + } |
| 40 | + |
| 41 | + public function resolveAsClass(string $name): Name |
| 42 | + { |
| 43 | + return Name::parse($name)->resolveClass($this->namespace, $this->importTable); |
| 44 | + } |
| 45 | + |
| 46 | + public function resolve(string $name): TemplateT|Name |
| 47 | + { |
| 48 | + return $this->templates[$name] ?? $this->resolveAsClass($name); |
| 49 | + } |
31 | 50 | } |
0 commit comments