Skip to content

Commit

Permalink
[PropertyInfo] Fix PhpStanExtractor when constructor has no docblock
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed May 15, 2023
1 parent c198074 commit d43b85b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Extractor/PhpStanExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ private function getDocBlockFromConstructor(string $class, string $property): ?P
return null;
}

$rawDocNode = $reflectionConstructor->getDocComment();
if (!$rawDocNode = $reflectionConstructor->getDocComment()) {
return null;
}

$tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
$phpDocNode = $this->phpDocParser->parse($tokens);
$tokens->consumeTokenType(Lexer::TOKEN_END);
Expand Down
9 changes: 9 additions & 0 deletions Tests/Extractor/PhpStanExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
Expand Down Expand Up @@ -353,6 +354,14 @@ public function testExtractConstructorTypes($property, array $type = null)
$this->assertEquals($type, $this->extractor->getTypesFromConstructor('Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummy', $property));
}

/**
* @dataProvider constructorTypesProvider
*/
public function testExtractConstructorTypesReturnNullOnEmptyDocBlock($property)
{
$this->assertNull($this->extractor->getTypesFromConstructor(ConstructorDummyWithoutDocBlock::class, $property));
}

public static function constructorTypesProvider()
{
return [
Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/ConstructorDummyWithoutDocBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;

class ConstructorDummyWithoutDocBlock
{
public function __construct(\DateTimeZone $timezone, $date, $dateObject, \DateTimeImmutable $dateTime, $mixed)
{
}
}

0 comments on commit d43b85b

Please sign in to comment.