Skip to content

Commit

Permalink
add callable template default type support
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 25, 2024
1 parent ccd6e8c commit 0f2c97d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/DocBlock/TypeExpression.php
Expand Up @@ -78,6 +78,10 @@ final class TypeExpression
\h+(?i)(?<callable_template_inner_b_kw>of|as)(?-i)\h+
(?<callable_template_inner_b_types>(?&types_inner))
|)
(?<callable_template_inner_d>
\h*=\h*
(?<callable_template_inner_d_types>(?&types_inner))
|)
)
(?:
\h*,\h*
Expand Down Expand Up @@ -514,6 +518,14 @@ private function parseCallableTemplateInnerTypes(int $startIndex, string $value)
];
}

if ('' !== $matches['callable_template_inner_d'][0]) {
$this->innerTypeExpressions[] = [
'start_index' => $startIndex + $index + $matches['callable_template_inner_d_types'][1]
- \strlen($addedPrefix),
'expression' => $this->inner($matches['callable_template_inner_d_types'][0]),
];
}

$index += $consumedValueLength + $consumedCommaLength;
}
}
Expand Down
11 changes: 10 additions & 1 deletion tests/DocBlock/TypeExpressionTest.php
Expand Up @@ -219,6 +219,10 @@ public static function provideGetTypesCases(): iterable

yield ['Closure< T1 of Foo, T2 AS Foo >(T1): T2'];

yield ['Closure<T = Foo>(T): T'];

yield ['Closure<T1=int, T2 of Foo = Foo2>(T1): T2'];

yield ['Closure<Closure_can_be_regular_class>'];

yield ['Closure(int $a)'];
Expand Down Expand Up @@ -835,11 +839,16 @@ public static function provideSortTypesCases(): iterable
'Closure<B, A>(x|y, A|B|U<o|p>): (B|X|Y)',
];

yield 'generic Closure with narrowed template' => [
yield 'generic Closure with bound template' => [
'Closure<B of J|I, C, A of V|U, D of object>(B|A): array{B, A, B, C, D}',
'Closure<B of I|J, C, A of U|V, D of object>(A|B): array{B, A, B, C, D}',
];

yield 'generic Closure with template with default' => [
'Closure<T = B&A>(T): void',
'Closure<T = A&B>(T): void',
];

yield 'nullable generic' => [
'?array<Foo|Bar>',
'?array<Bar|Foo>',
Expand Down

0 comments on commit 0f2c97d

Please sign in to comment.