Skip to content

Commit

Permalink
Add typeless parameter support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed Mar 12, 2024
1 parent 2b309a2 commit 98566f2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
13 changes: 11 additions & 2 deletions src/DocBlock/Tags/Factory/ParamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode;
use Webmozart\Assert\Assert;

use function trim;
Expand All @@ -32,7 +34,13 @@ public function __construct(TypeResolver $typeResolver, DescriptionFactory $desc
public function create(PhpDocTagNode $node, Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, ParamTagValueNode::class);
Assert::isInstanceOfAny(
$tagValue,
[
ParamTagValueNode::class,
TypelessParamTagValueNode::class
]
);

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

public function supports(PhpDocTagNode $node, Context $context): bool
{
return $node->value instanceof ParamTagValueNode;
return $node->value instanceof ParamTagValueNode
|| $node->value instanceof TypelessParamTagValueNode;
}
}
1 change: 0 additions & 1 deletion tests/integration/InterpretingDocBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,5 @@ public function testMethodRegression(): void
],
$docblock->getTags()
);

}
}
74 changes: 65 additions & 9 deletions tests/unit/DocBlock/Tags/Factory/ParamFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\String_;

final class ParamFactoryTest extends TagFactoryTestCase
Expand All @@ -24,23 +26,77 @@ final class ParamFactoryTest extends TagFactoryTestCase
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::__construct
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports
* @dataProvider paramInputProvider
*/
public function testParamIsCreated(): void
public function testParamIsCreated(string $input, Param $expected): void
{
$ast = $this->parseTag('@param string $var');
$ast = $this->parseTag($input);
$factory = new ParamFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
$context = new Context('global');

self::assertTrue($factory->supports($ast, $context));
self::assertEquals(
new Param(
'var',
new String_(),
false,
new Description(''),
false
),
$expected,
$factory->create($ast, $context)
);
}

/**
* @return array<array-key, string|Param>
*/
public function paramInputProvider(): array
{
return [
[
'@param string $var',
new Param(
'var',
new String_(),
false,
new Description(''),
false
),
],
[
'@param $param8 Description 4',
new Param(
'param8',
new Mixed_(),
false,
new Description('Description 4'),
false
),
],
[
'@param $param9',
new Param(
'param9',
new Mixed_(),
false,
new Description(''),
false
),
],
[
'@param int My Description',
new Param(
null,
new Integer(),
false,
new Description('My Description'),
false
),
],
[
'@param foo',
new Param(
null,
new Mixed_(),
false,
new Description(''),
false
),
],
];
}
}
2 changes: 1 addition & 1 deletion tests/unit/DocBlock/Tags/Factory/TagFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function giveTypeResolver(): TypeResolver
public function givenDescriptionFactory(): DescriptionFactory
{
$factory = m::mock(DescriptionFactory::class);
$factory->shouldReceive('create')->andReturn(new Description(''));
$factory->shouldReceive('create')->andReturnUsing(static fn ($args) => new Description($args));

return $factory;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/DocBlock/Tags/Factory/VarFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class VarFactoryTest extends TagFactoryTestCase
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::create
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\VarFactory::supports
*/
public function testParamIsCreated(): void
public function testVarIsCreated(): void
{
$ast = $this->parseTag('@var string $var');
$factory = new VarFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
Expand Down

0 comments on commit 98566f2

Please sign in to comment.