Skip to content

Commit

Permalink
[PropertyInfo] Get short description from promoted properties in PhpD…
Browse files Browse the repository at this point in the history
…ocExtractor
  • Loading branch information
wuchen90 committed Jan 31, 2025
1 parent eff9b52 commit 9141b68
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/PropertyInfo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add support for `non-positive-int`, `non-negative-int` and `non-zero-int` PHPStan types to `PhpStanExtractor`
* Add `PropertyDescriptionExtractorInterface` to `PhpStanExtractor`
* Add support for getting description from constructor with promoted arguments to `PhpDocExtractor`

7.1
---
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ public function getShortDescription(string $class, string $property, array $cont
}
}

foreach ($docBlock->getTagsByName('param') as $param) {
if (!$param instanceof DocBlock\Tags\Param) {
continue;
}

$paramDescription = $param->getDescription()?->render();

if (null !== $paramDescription && '' !== $paramDescription) {
return $paramDescription;
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyCollection;
use Symfony\Component\PropertyInfo\Tests\Fixtures\InvalidDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Extractor\PromotedPropertiesWithDocBlock;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\PseudoTypeDummy;
Expand Down Expand Up @@ -889,6 +890,23 @@ public static function promotedPropertyProvider(): iterable
yield ['promoted', null];
yield ['promotedAndMutated', Type::string()];
}

/**
* @dataProvider provideGetShortDescriptionOnPromotedPropertyTestCases
*/
public function testGetShortDescriptionOnPromotedProperty(string $class, string $property, ?string $description)
{
$this->assertSame($description, $this->extractor->getShortDescription($class, $property));
}

public static function provideGetShortDescriptionOnPromotedPropertyTestCases(): array
{
return [
[PromotedPropertiesWithDocBlock::class, 'foo', 'Just a foo property'],
[PromotedPropertiesWithDocBlock::class, 'bar', 'A phpdoc without type'],
'Inlined phpdoc with @var is not compatible with the one above constructor' => [PromotedPropertiesWithDocBlock::class, 'baz', null],
];
}
}

class EmptyDocBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Extractor;

class PromotedPropertiesWithDocBlock
{
/**
* @param string $foo Just a foo property
* @param $bar A phpdoc without type
*/
public function __construct(
public string $foo,
public int $bar,
/** @var string $baz This phpdoc isn't compatible with the one above this method, thus it won't appear */
public string $baz,
) {
}
}

0 comments on commit 9141b68

Please sign in to comment.