Skip to content

Commit 98566f2

Browse files
committed
Add typeless parameter support.
1 parent 2b309a2 commit 98566f2

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

src/DocBlock/Tags/Factory/ParamFactory.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1010
use phpDocumentor\Reflection\TypeResolver;
1111
use phpDocumentor\Reflection\Types\Context;
12+
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
15+
use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode;
1416
use Webmozart\Assert\Assert;
1517

1618
use function trim;
@@ -32,7 +34,13 @@ public function __construct(TypeResolver $typeResolver, DescriptionFactory $desc
3234
public function create(PhpDocTagNode $node, Context $context): Tag
3335
{
3436
$tagValue = $node->value;
35-
Assert::isInstanceOf($tagValue, ParamTagValueNode::class);
37+
Assert::isInstanceOfAny(
38+
$tagValue,
39+
[
40+
ParamTagValueNode::class,
41+
TypelessParamTagValueNode::class
42+
]
43+
);
3644

3745
return new Param(
3846
trim($tagValue->parameterName, '$'),
@@ -45,6 +53,7 @@ public function create(PhpDocTagNode $node, Context $context): Tag
4553

4654
public function supports(PhpDocTagNode $node, Context $context): bool
4755
{
48-
return $node->value instanceof ParamTagValueNode;
56+
return $node->value instanceof ParamTagValueNode
57+
|| $node->value instanceof TypelessParamTagValueNode;
4958
}
5059
}

tests/integration/InterpretingDocBlocksTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,5 @@ public function testMethodRegression(): void
217217
],
218218
$docblock->getTags()
219219
);
220-
221220
}
222221
}

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

+65-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use phpDocumentor\Reflection\DocBlock\Description;
1717
use phpDocumentor\Reflection\DocBlock\Tags\Param;
1818
use phpDocumentor\Reflection\Types\Context;
19+
use phpDocumentor\Reflection\Types\Integer;
20+
use phpDocumentor\Reflection\Types\Mixed_;
1921
use phpDocumentor\Reflection\Types\String_;
2022

2123
final class ParamFactoryTest extends TagFactoryTestCase
@@ -24,23 +26,77 @@ final class ParamFactoryTest extends TagFactoryTestCase
2426
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct
2527
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create
2628
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports
29+
* @dataProvider paramInputProvider
2730
*/
28-
public function testParamIsCreated(): void
31+
public function testParamIsCreated(string $input, Param $expected): void
2932
{
30-
$ast = $this->parseTag('@param string $var');
33+
$ast = $this->parseTag($input);
3134
$factory = new ParamFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
3235
$context = new Context('global');
3336

3437
self::assertTrue($factory->supports($ast, $context));
3538
self::assertEquals(
36-
new Param(
37-
'var',
38-
new String_(),
39-
false,
40-
new Description(''),
41-
false
42-
),
39+
$expected,
4340
$factory->create($ast, $context)
4441
);
4542
}
43+
44+
/**
45+
* @return array<array-key, string|Param>
46+
*/
47+
public function paramInputProvider(): array
48+
{
49+
return [
50+
[
51+
'@param string $var',
52+
new Param(
53+
'var',
54+
new String_(),
55+
false,
56+
new Description(''),
57+
false
58+
),
59+
],
60+
[
61+
'@param $param8 Description 4',
62+
new Param(
63+
'param8',
64+
new Mixed_(),
65+
false,
66+
new Description('Description 4'),
67+
false
68+
),
69+
],
70+
[
71+
'@param $param9',
72+
new Param(
73+
'param9',
74+
new Mixed_(),
75+
false,
76+
new Description(''),
77+
false
78+
),
79+
],
80+
[
81+
'@param int My Description',
82+
new Param(
83+
null,
84+
new Integer(),
85+
false,
86+
new Description('My Description'),
87+
false
88+
),
89+
],
90+
[
91+
'@param foo',
92+
new Param(
93+
null,
94+
new Mixed_(),
95+
false,
96+
new Description(''),
97+
false
98+
),
99+
],
100+
];
101+
}
46102
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function giveTypeResolver(): TypeResolver
4545
public function givenDescriptionFactory(): DescriptionFactory
4646
{
4747
$factory = m::mock(DescriptionFactory::class);
48-
$factory->shouldReceive('create')->andReturn(new Description(''));
48+
$factory->shouldReceive('create')->andReturnUsing(static fn ($args) => new Description($args));
4949

5050
return $factory;
5151
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class VarFactoryTest extends TagFactoryTestCase
2525
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::create
2626
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::supports
2727
*/
28-
public function testParamIsCreated(): void
28+
public function testVarIsCreated(): void
2929
{
3030
$ast = $this->parseTag('@var string $var');
3131
$factory = new VarFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());

0 commit comments

Comments
 (0)