Skip to content

Commit d6e90a3

Browse files
committed
Add callable parameter support
1 parent 50bf167 commit d6e90a3

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/DocBlock/Tags/Factory/TypeFactory.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpDocumentor\Reflection\TypeResolver;
1818
use phpDocumentor\Reflection\Types\Array_;
1919
use phpDocumentor\Reflection\Types\Callable_;
20+
use phpDocumentor\Reflection\Types\CallableParameter;
2021
use phpDocumentor\Reflection\Types\ClassString;
2122
use phpDocumentor\Reflection\Types\Collection;
2223
use phpDocumentor\Reflection\Types\Compound;
@@ -33,6 +34,7 @@
3334
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
3435
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
3536
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
37+
use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
3638
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode;
3739
use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode;
3840
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
@@ -189,7 +191,21 @@ private function createFromGeneric(GenericTypeNode $type, ?Context $context): Ty
189191

190192
private function createFromCallable(CallableTypeNode $type, ?Context $context): Callable_
191193
{
192-
return new Callable_();
194+
return new Callable_(
195+
array_map(
196+
function (CallableTypeParameterNode $param) use ($context) {
197+
return new CallableParameter(
198+
$param->parameterName !== '' ? trim($param->parameterName, '$') : null,
199+
$this->createType($param->type, $context),
200+
$param->isReference,
201+
$param->isVariadic,
202+
$param->isOptional
203+
);
204+
},
205+
$type->parameters
206+
),
207+
$this->createType($type->returnType, $context),
208+
);
193209
}
194210

195211
private function createFromConst(ConstTypeNode $type, ?Context $context): ?Type

tests/unit/DocBlock/Tags/Factory/TypeFactoryTest.php

+41-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use phpDocumentor\Reflection\Types\Array_;
2828
use phpDocumentor\Reflection\Types\ArrayKey;
2929
use phpDocumentor\Reflection\Types\Callable_;
30+
use phpDocumentor\Reflection\Types\CallableParameter;
3031
use phpDocumentor\Reflection\Types\ClassString;
3132
use phpDocumentor\Reflection\Types\Collection;
3233
use phpDocumentor\Reflection\Types\Compound;
@@ -49,9 +50,9 @@
4950
final class TypeFactoryTest extends TestCase
5051
{
5152
/**
52-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createType
53-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createFromGeneric
54-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createFromCallable
53+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createType
54+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createFromGeneric
55+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\TypeFactory::createFromCallable
5556
* @dataProvider typeProvider
5657
* @dataProvider genericsProvider
5758
* @dataProvider callableProvider
@@ -208,15 +209,49 @@ public function callableProvider(): array
208209
],
209210
[
210211
'callable(): Foo',
211-
new Callable_(),
212+
new Callable_([], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
212213
],
213214
[
214215
'callable(): (Foo&Bar)',
215-
new Callable_(),
216+
new Callable_(
217+
[],
218+
new Intersection(
219+
[
220+
new Object_(new Fqsen('\\phpDocumentor\\Foo')),
221+
new Object_(new Fqsen('\\phpDocumentor\\Bar'))
222+
]
223+
)
224+
),
216225
],
217226
[
218227
'callable(A&...$a=, B&...=, C): Foo',
219-
new Callable_(),
228+
new Callable_(
229+
[
230+
new CallableParameter(
231+
'a',
232+
new Object_(new Fqsen('\\phpDocumentor\\A')),
233+
true,
234+
true,
235+
true
236+
),
237+
new CallableParameter(
238+
null,
239+
new Object_(new Fqsen('\\phpDocumentor\\B')),
240+
true,
241+
true,
242+
true
243+
),
244+
new CallableParameter(
245+
null,
246+
new Object_(new Fqsen('\\phpDocumentor\\C')),
247+
false,
248+
false,
249+
false
250+
),
251+
],
252+
new Object_(new Fqsen('\\phpDocumentor\\Foo')
253+
)
254+
),
220255
],
221256
];
222257
}

0 commit comments

Comments
 (0)