diff --git a/composer.json b/composer.json index 5ad1c5cc8322..84692253ea65 100644 --- a/composer.json +++ b/composer.json @@ -121,7 +121,7 @@ "egulias/email-validator": "~1.2,>=1.2.8|~2.0", "symfony/phpunit-bridge": "^5.0.8", "symfony/security-acl": "~2.8|~3.0", - "phpdocumentor/reflection-docblock": "^3.0|^4.0", + "phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0", "twig/cssinliner-extra": "^2.12", "twig/inky-extra": "^2.12", "twig/markdown-extra": "^2.12" diff --git a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php index 0ae762bc0b6f..cbabce9eb6c2 100644 --- a/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php @@ -12,6 +12,7 @@ namespace Symfony\Component\PropertyInfo\Extractor; use phpDocumentor\Reflection\DocBlock; +use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag; use phpDocumentor\Reflection\DocBlockFactory; use phpDocumentor\Reflection\DocBlockFactoryInterface; use phpDocumentor\Reflection\Types\Context; @@ -88,10 +89,12 @@ public function getShortDescription($class, $property, array $context = []): ?st } foreach ($docBlock->getTagsByName('var') as $var) { - $varDescription = $var->getDescription()->render(); + if ($var && !$var instanceof InvalidTag) { + $varDescription = $var->getDescription()->render(); - if (!empty($varDescription)) { - return $varDescription; + if (!empty($varDescription)) { + return $varDescription; + } } } @@ -142,7 +145,7 @@ public function getTypes($class, $property, array $context = []): ?array $types = []; /** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */ foreach ($docBlock->getTagsByName($tag) as $tag) { - if ($tag && null !== $tag->getType()) { + if ($tag && !$tag instanceof InvalidTag && null !== $tag->getType()) { $types = array_merge($types, $this->phpDocTypeHelper->getTypes($tag->getType())); } } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php index 0d3c32206786..d352fa12b61f 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php @@ -11,6 +11,8 @@ namespace Symfony\Component\PropertyInfo\Tests\Extractor; +use phpDocumentor\Reflection\DocBlock\StandardTagFactory; +use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag; use phpDocumentor\Reflection\Types\Collection; use PHPUnit\Framework\TestCase; use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; @@ -46,6 +48,26 @@ public function testParamTagTypeIsOmitted() $this->assertNull($this->extractor->getTypes(OmittedParamTagTypeDocBlock::class, 'omittedType')); } + public function invalidTypesProvider() + { + return [ + 'pub' => ['pub', null, null], + 'stat' => ['stat', null, null], + 'foo' => ['foo', $this->isPhpDocumentorV5() ? 'Foo.' : null, null], + 'bar' => ['bar', $this->isPhpDocumentorV5() ? 'Bar.' : null, null], + ]; + } + + /** + * @dataProvider invalidTypesProvider + */ + public function testInvalid($property, $shortDescription, $longDescription) + { + $this->assertNull($this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\InvalidDummy', $property)); + $this->assertSame($shortDescription, $this->extractor->getShortDescription('Symfony\Component\PropertyInfo\Tests\Fixtures\InvalidDummy', $property)); + $this->assertSame($longDescription, $this->extractor->getLongDescription('Symfony\Component\PropertyInfo\Tests\Fixtures\InvalidDummy', $property)); + } + /** * @dataProvider typesWithNoPrefixesProvider */ @@ -94,7 +116,7 @@ public function typesProvider() ['donotexist', null, null, null], ['staticGetter', null, null, null], ['staticSetter', null, null, null], - ['emptyVar', null, null, null], + ['emptyVar', null, $this->isPhpDocumentorV5() ? 'This should not be removed.' : null, null], ]; } @@ -250,6 +272,16 @@ public function testDocBlockFallback($property, $types) { $this->assertEquals($types, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\DockBlockFallback', $property)); } + + protected function isPhpDocumentorV5() + { + if (class_exists(InvalidTag::class)) { + return true; + } + + return (new \ReflectionMethod(StandardTagFactory::class, 'create')) + ->hasReturnType(); + } } class EmptyDocBlock diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/InvalidDummy.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/InvalidDummy.php new file mode 100644 index 000000000000..0a4cc81f36be --- /dev/null +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/InvalidDummy.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\PropertyInfo\Tests\Fixtures; + +/** + * @author Martin Rademacher + */ +class InvalidDummy +{ + /** + * @var + */ + public $pub; + + /** + * @return + */ + public static function getStat() + { + return 'stat'; + } + + /** + * Foo. + * + * @param + */ + public function setFoo($foo) + { + } + + /** + * Bar. + * + * @return + */ + public function getBar() + { + return 'bar'; + } +} diff --git a/src/Symfony/Component/PropertyInfo/composer.json b/src/Symfony/Component/PropertyInfo/composer.json index fa24113005ea..f9f97e84a863 100644 --- a/src/Symfony/Component/PropertyInfo/composer.json +++ b/src/Symfony/Component/PropertyInfo/composer.json @@ -30,7 +30,7 @@ "symfony/serializer": "^3.4|^4.0|^5.0", "symfony/cache": "^3.4|^4.0|^5.0", "symfony/dependency-injection": "^3.4|^4.0|^5.0", - "phpdocumentor/reflection-docblock": "^3.0|^4.0", + "phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0", "doctrine/annotations": "~1.7" }, "conflict": {