Skip to content

Commit 6b865db

Browse files
committed
Refactor ContextualTypeParser
1 parent 518a654 commit 6b865db

File tree

7 files changed

+21
-25
lines changed

7 files changed

+21
-25
lines changed

composer.lock

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

src/CustomTypeParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface CustomTypeParser
1313
{
1414
/**
1515
* @param non-empty-string $unresolvedName
16-
* @param list<Type> $typeArguments
16+
* @param list<Type> $templateArguments
1717
*/
18-
public function parseCustomType(string $unresolvedName, array $typeArguments, TypeContext $context): ?Type;
18+
public function parseCustomType(string $unresolvedName, array $templateArguments, TypeContext $context): ?Type;
1919
}

src/CustomTypeParsers.php

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

21-
public function parseCustomType(string $unresolvedName, array $typeArguments, TypeContext $context): ?Type
21+
public function parseCustomType(string $unresolvedName, array $templateArguments, TypeContext $context): ?Type
2222
{
2323
foreach ($this->customTypeParsers as $customTypeParser) {
24-
$type = $customTypeParser->parseCustomType($unresolvedName, $typeArguments, $context);
24+
$type = $customTypeParser->parseCustomType($unresolvedName, $templateArguments, $context);
2525

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

src/Internal/ContextualTypeParser.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,10 @@ private function parseIdentifier(string $name, array $genericNodes = []): Type
140140
return $type($genericNodes);
141141
}
142142

143-
$typeArguments = array_map($this->parseTypeNode(...), $genericNodes);
144-
$customType = $this->customTypeParser->parseCustomType($name, $typeArguments, $this->context);
143+
$templateArguments = array_map($this->parseTypeNode(...), $genericNodes);
145144

146-
if ($customType !== null) {
147-
return $customType;
148-
}
149-
150-
throw new \LogicException(\sprintf('Unsupported identifier `%s`', $name));
145+
return $this->customTypeParser->parseCustomType($name, $templateArguments, $this->context)
146+
?? $this->context->resolveNameAsType($name, $templateArguments);
151147
}
152148

153149
/**

src/RuntimeTypeContext.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Typhoon\PHPStanTypeParser;
66

7-
use Typhoon\Type\NamedObjectT;
87
use Typhoon\Type\Type;
8+
use function Typhoon\Type\objectT;
99

1010
/**
1111
* @api
@@ -23,19 +23,15 @@ public function resolveConstantName(string $unresolvedName): array
2323

2424
public function resolveClassName(string $unresolvedName): string
2525
{
26-
if (class_exists($unresolvedName)) {
26+
if (class_exists($unresolvedName) || interface_exists($unresolvedName)) {
2727
return $unresolvedName;
2828
}
2929

3030
throw new \LogicException(\sprintf('Class `%s` does not exist', $unresolvedName));
3131
}
3232

33-
public function resolveNameAsType(string $unresolvedName, array $typeArguments = []): Type
33+
public function resolveNameAsType(string $unresolvedName, array $templateArguments = []): Type
3434
{
35-
/**
36-
* @todo requires objectT() type constructor
37-
* @psalm-suppress InternalMethod
38-
*/
39-
return new NamedObjectT($this->resolveClassName($unresolvedName), $typeArguments);
35+
return objectT($this->resolveClassName($unresolvedName), $templateArguments);
4036
}
4137
}

src/TypeContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function resolveClassName(string $unresolvedName): string;
2525

2626
/**
2727
* @param non-empty-string $unresolvedName
28-
* @param list<Type> $typeArguments
28+
* @param list<Type> $templateArguments
2929
*/
30-
public function resolveNameAsType(string $unresolvedName, array $typeArguments = []): Type;
30+
public function resolveNameAsType(string $unresolvedName, array $templateArguments = []): Type;
3131
}

tests/PHPStanTypeParserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function Typhoon\Type\intRangeT;
1616
use function Typhoon\Type\intT;
1717
use function Typhoon\Type\nullOrT;
18+
use function Typhoon\Type\objectT;
1819
use function Typhoon\Type\orT;
1920
use function Typhoon\Type\stringT;
2021
use const Typhoon\Type\arrayKeyT;
@@ -89,6 +90,9 @@ private static function cases(): \Generator
8990
yield 'int&string' => andT(intT, stringT);
9091
yield '(int&string)&float' => andT(andT(intT, stringT), floatT);
9192
yield 'mixed' => mixedT;
93+
yield \stdClass::class => objectT(\stdClass::class);
94+
yield \Stringable::class => objectT(\Stringable::class);
95+
yield 'Traversable<int, string>' => objectT(\Traversable::class, [intT, stringT]);
9296
}
9397

9498
/**

0 commit comments

Comments
 (0)