Skip to content

Commit 974172d

Browse files
committed
Add Context::useAlias()
1 parent 637ed5f commit 974172d

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"brick/math": "^0.14.0",
1919
"phpstan/phpdoc-parser": "^2.1",
2020
"symfony/polyfill-php84": "^1.33",
21-
"typhoon/type": "^0.6"
21+
"typhoon/type": "^0.6@dev"
2222
},
2323
"require-dev": {
2424
"bamarni/composer-bin-plugin": "^1.8.2",

composer.lock

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Context.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Typhoon\PHPStanTypeParser;
66

7+
use Typhoon\Type\AliasT;
78
use Typhoon\Type\TemplateT;
89

910
final class Context
@@ -18,6 +19,11 @@ final class Context
1819
*/
1920
private array $templates = [];
2021

22+
/**
23+
* @var array<non-empty-string, AliasT>
24+
*/
25+
private array $aliases = [];
26+
2127
public function __construct(
2228
public readonly ?Name $namespace = null,
2329
) {}
@@ -30,21 +36,32 @@ public function use(Name $name, ?Name $as = null): self
3036
return $context;
3137
}
3238

33-
public function template(TemplateT $template): self
39+
public function useTemplate(TemplateT $template): self
3440
{
3541
$context = clone $this;
3642
$context->templates[$template->name] = $template;
3743

3844
return $context;
3945
}
4046

47+
/**
48+
* @param ?non-empty-string $as
49+
*/
50+
public function useAlias(AliasT $alias, ?string $as = null): self
51+
{
52+
$context = clone $this;
53+
$context->aliases[$as ?? $alias->name] = $alias;
54+
55+
return $context;
56+
}
57+
4158
public function resolveAsClass(string $name): Name
4259
{
4360
return Name::parse($name)->resolveClass($this->namespace, $this->importTable);
4461
}
4562

46-
public function resolve(string $name): TemplateT|Name
63+
public function resolve(string $name): Name|TemplateT|AliasT
4764
{
48-
return $this->templates[$name] ?? $this->resolveAsClass($name);
65+
return $this->templates[$name] ?? $this->aliases[$name] ?? $this->resolveAsClass($name);
4966
}
5067
}

src/Parser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPStan\PhpDocParser\Parser\TypeParser;
3232
use PHPStan\PhpDocParser\ParserConfig;
3333
use Typhoon\Type;
34+
use Typhoon\Type\AliasT;
3435
use Typhoon\Type\ArrayBareT;
3536
use Typhoon\Type\ArrayT;
3637
use Typhoon\Type\CallableT;
@@ -277,6 +278,14 @@ private function identifier(string $name, array $genericNodes = []): Type
277278
return $resolved;
278279
}
279280

281+
if ($resolved instanceof AliasT) {
282+
return new AliasT(
283+
class: $resolved->class,
284+
name: $resolved->name,
285+
templateArguments: $templateArguments,
286+
);
287+
}
288+
280289
/** @var class-string */
281290
$class = $resolved->toString();
282291

@@ -404,7 +413,7 @@ private function callable(CallableTypeNode $node): CallableT
404413

405414
foreach ($node->templateTypes as $templateNode) {
406415
$templateFactories[] = Template::factory($templateNode->name, type: $type);
407-
$context = $context->template($type);
416+
$context = $context->useTemplate($type);
408417
}
409418

410419
$parser = $this->withContext($context);

0 commit comments

Comments
 (0)