Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more strict about type definitions on param #363

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/DocBlock/Tags/Factory/ParamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Deprecations\Deprecation;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Context;
Expand All @@ -15,6 +16,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use Webmozart\Assert\Assert;

use function sprintf;
Expand Down Expand Up @@ -59,6 +61,13 @@ public function create(PhpDocTagNode $node, Context $context): Tag
]
);

if (($tagValue->type ?? null) instanceof OffsetAccessTypeNode) {
return InvalidTag::create(
(string) $tagValue,
'param'
);
}

return new Param(
trim($tagValue->parameterName, '$'),
$this->typeResolver->createType($tagValue->type ?? new IdentifierTypeNode('mixed'), $context),
Expand Down
8 changes: 8 additions & 0 deletions src/DocBlock/Tags/TagWithType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

namespace phpDocumentor\Reflection\DocBlock\Tags;

use InvalidArgumentException;
use phpDocumentor\Reflection\Type;

use function in_array;
use function sprintf;
use function strlen;
use function substr;
use function trim;
Expand Down Expand Up @@ -59,6 +61,12 @@ protected static function extractTypeFromBody(string $body): array
}
}

if ($nestingLevel < 0 || $nestingLevel > 0) {
throw new InvalidArgumentException(
sprintf('Could not find type in %s, please check for malformed notations', $body)
);
}

$description = trim(substr($body, strlen($type)));

return [$type, $description];
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/InterpretingDocBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Method;
use phpDocumentor\Reflection\DocBlock\Tags\MethodParameter;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
Expand Down Expand Up @@ -218,4 +219,30 @@ public function testMethodRegression(): void
$docblock->getTags()
);
}

public function testInvalidTypeParamResultsInInvalidTag(): void
{
$docCommment = '
/**
* This is an example of a summary.
*
* @param array\Foo> $test
*/
';
$factory = DocBlockFactory::createInstance();
$docblock = $factory->create($docCommment);

self::assertEquals(
[
InvalidTag::create(
'array\Foo> $test',
'param',
)->withError(
new \InvalidArgumentException(
'Could not find type in array\Foo> $test, please check for malformed notations')
),
],
$docblock->getTags()
);
}
}
8 changes: 7 additions & 1 deletion tests/unit/DocBlock/Tags/Factory/ParamFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\PseudoTypes\IntegerValue;
Expand All @@ -33,7 +35,7 @@ final class ParamFactoryTest extends TagFactoryTestCase
* @covers \phpDocumentor\Reflection\DocBlock\Tags\Factory\ParamFactory::supports
* @dataProvider paramInputProvider
*/
public function testParamIsCreated(string $input, Param $expected): void
public function testParamIsCreated(string $input, Tag $expected): void
{
$ast = $this->parseTag($input);
$factory = new ParamFactory($this->giveTypeResolver(), $this->givenDescriptionFactory());
Expand Down Expand Up @@ -122,6 +124,10 @@ public function paramInputProvider(): array
false
),
],
[
'@param array[\Illuminate\Notifications\Channels\Notification] $notification',
InvalidTag::create('array[\Illuminate\Notifications\Channels\Notification] $notification', 'param'),
],
];
}
}
Loading