Skip to content

Commit 15db8d8

Browse files
committed
Update typhoon/type, change templates' parsing
1 parent 2e11a47 commit 15db8d8

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
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/Internal/ContextualParser.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -349,21 +349,28 @@ private function const(array $genericNodes): ConstantT
349349

350350
private function callable(CallableTypeNode $node): CallableT
351351
{
352-
$templates = [];
353-
354-
foreach ($node->templateTypes as $templateNode) {
355-
$template = new Template($templateNode->name);
356-
$templates[$templateNode->name] = $template;
357-
$this->templateTypes[$templateNode->name] = $template->type;
358-
}
359-
360352
return new CallableT(
361353
templates: array_map(
362-
fn(TemplateTagValueNode $node): Template => $templates[$node->name]
363-
->withLowerBound($node->lowerBound === null ? neverT : $this->parse($node->lowerBound))
364-
->withUpperBound($node->bound === null ? mixedT : $this->parse($node->bound))
365-
->withDefault($node->default === null ? null : $this->parse($node->default)),
366-
$node->templateTypes,
354+
// 4. resolve templates
355+
static fn(\Closure $lazyTemplate): Template => $lazyTemplate(),
356+
array_map(
357+
function (TemplateTagValueNode $node): \Closure {
358+
// 2. create template factory
359+
$factory = Template::factory(
360+
name: $node->name,
361+
// 1. assign template type by reference
362+
type: $this->templateTypes[$node->name],
363+
);
364+
365+
// 3. apply the rest of the template's properties to the factory
366+
return fn(): Template => $factory(
367+
lowerBound: $node->lowerBound === null ? neverT : $this->parse($node->lowerBound),
368+
upperBound: $node->bound === null ? mixedT : $this->parse($node->bound),
369+
default: $node->default === null ? null : $this->parse($node->default),
370+
);
371+
},
372+
$node->templateTypes,
373+
),
367374
),
368375
parameters: array_map(
369376
fn(CallableTypeParameterNode $node): Parameter => new Parameter(

tests/ParserTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ private static function cases(): \Generator
146146
// todo yield 'stdClass|Iterator&Throwable' https://github.com/phpstan/phpdoc-parser/issues/271
147147
$T = template('T', upperBound: scalarT, lowerBound: stringT, default: arrayKeyT);
148148
yield 'callable<T of scalar super string = array-key>(T): ?T' => callableT([$T], [$T->type], nullOrT($T->type));
149+
$T2 = template('T2');
150+
$T = template('T', $T2->type);
151+
yield 'callable<T of T2, T2>(): mixed' => callableT([$T, $T2]);
149152
}
150153

151154
private ?Parser $parser = null;

0 commit comments

Comments
 (0)