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

[property-info] Detect invalid tags and throw error accordingly #36850

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -100,7 +100,7 @@
"egulias/email-validator": "~1.2,>=1.2.8|~2.0",
"symfony/phpunit-bridge": "^3.4.31|^4.3.4|~5.0",
"symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0"
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0"
},
"conflict": {
"monolog/monolog": ">=2",
Expand Down
Expand Up @@ -83,6 +83,10 @@ public function getShortDescription($class, $property, array $context = [])
}

foreach ($docBlock->getTagsByName('var') as $var) {
if (is_a($var, 'phpDocumentor\Reflection\DocBlock\Tags\InvalidTag')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is_a($var, 'phpDocumentor\Reflection\DocBlock\Tags\InvalidTag')) {
if (is_a($var, InvalidTag::class)) {

(add the appropriate use too)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InvalidTag is introduced in v5 of phpdocumentor/reflection-docblock. A use statement would be a BC break with v3 and v4 since those are autoloaded?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. Use statements don't imply that a class exist. And ::class does not either (it is not an actual constant access on the class, but a compile-time thing)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha I did't know that.

throw new \InvalidArgumentException(sprintf('Failed to get the description of the @var tag "%s" for class "%s". Please check that the @var tag is correctly defined.', $property, $class));
}

$varDescription = $var->getDescription()->render();

if (!empty($varDescription)) {
Expand Down Expand Up @@ -137,6 +141,10 @@ public function getTypes($class, $property, array $context = [])
$types = [];
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
foreach ($docBlock->getTagsByName($tag) as $tag) {
if (is_a($tag, 'phpDocumentor\Reflection\DocBlock\Tags\InvalidTag')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is_a($tag, 'phpDocumentor\Reflection\DocBlock\Tags\InvalidTag')) {
if (is_a($tag, InvalidTag::class)) {

return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we throw here too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing here would break applications with invalid docblocks.

}

if ($tag && null !== $tag->getType()) {
$types = array_merge($types, $this->phpDocTypeHelper->getTypes($tag->getType()));
}
Expand Down
Expand Up @@ -102,7 +102,7 @@ public function typesProvider()
['donotexist', null, null, null],
['staticGetter', null, null, null],
['staticSetter', null, null, null],
['emptyVar', null, null, null],
['emptyVar', null, 'This should not be removed.', null],
];
}

Expand Down Expand Up @@ -191,6 +191,14 @@ public function testReturnNullOnEmptyDocBlock()
$this->assertNull($this->extractor->getShortDescription(EmptyDocBlock::class, 'foo'));
}

public function testReturnNullOnIncompleteDocBlock()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Failed to get the description of the @var tag "foo" for class "Symfony\Component\PropertyInfo\Tests\Extractor\IncompleteDocBlock". Please check that the @var tag is correctly defined.');

$this->extractor->getShortDescription(IncompleteDocBlock::class, 'foo');
}

public function dockBlockFallbackTypesProvider()
{
return [
Expand All @@ -215,6 +223,16 @@ public function testDocBlockFallback($property, $types)
}
}

class IncompleteDocBlock
{
/**
* @var
* @ORM\Id
* @ORM\Column(name="FOO", type="integer")
*/
public $foo;
}

class EmptyDocBlock
{
public $foo;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/PropertyInfo/composer.json
Expand Up @@ -30,7 +30,7 @@
"symfony/serializer": "~2.8|~3.0|~4.0",
"symfony/cache": "~3.1|~4.0",
"symfony/dependency-injection": "~3.3|~4.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
"doctrine/annotations": "~1.7"
},
"conflict": {
Expand Down