Skip to content

Commit 6358ba0

Browse files
committed
Rewrite Context
1 parent e87cb6b commit 6358ba0

File tree

12 files changed

+674
-526
lines changed

12 files changed

+674
-526
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": "^8.2",
1818
"phpstan/phpdoc-parser": "^2.1",
1919
"symfony/polyfill-php84": "^1.33",
20-
"typhoon/type": "^0.6@dev"
20+
"typhoon/type": "^0.6"
2121
},
2222
"require-dev": {
2323
"bamarni/composer-bin-plugin": "^1.8.2",

composer.lock

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

src/Context.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,47 @@
44

55
namespace Typhoon\PHPStanTypeParser;
66

7-
use Typhoon\Type;
7+
use Typhoon\Type\TemplateT;
88

9-
/**
10-
* @api
11-
*/
12-
interface Context
9+
final class Context
1310
{
1411
/**
15-
* @param non-empty-string $unresolvedName
16-
* @return array{non-empty-string, ?non-empty-string}
12+
* @var array<non-empty-lowercase-string, Name>
1713
*/
18-
public function resolveConstantName(string $unresolvedName): array;
14+
private array $importTable = [];
1915

2016
/**
21-
* @param non-empty-string $unresolvedName
22-
* @return class-string
17+
* @var array<non-empty-string, TemplateT>
2318
*/
24-
public function resolveClassName(string $unresolvedName): string;
19+
private array $templates = [];
2520

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+
}
3150
}

src/CustomParser.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@
1212
*/
1313
interface CustomParser
1414
{
15-
/**
16-
* @param callable(TypeNode): Type $parse
17-
*/
18-
public function parse(TypeNode $node, callable $parse, Context $context): ?Type;
15+
public function parse(TypeNode $node, Parser $parser): ?Type;
1916
}

src/CustomParsers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function __construct(
1919
private iterable $customTypeParsers = [],
2020
) {}
2121

22-
public function parse(TypeNode $node, callable $parse, Context $context): ?Type
22+
public function parse(TypeNode $node, Parser $parser): ?Type
2323
{
2424
foreach ($this->customTypeParsers as $customTypeParser) {
25-
$type = $customTypeParser->parse($node, $parse, $context);
25+
$type = $customTypeParser->parse($node, $parser);
2626

2727
if ($type !== null) {
2828
return $type;

0 commit comments

Comments
 (0)