Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PhpStanExtractor doesn't support docblock from promoted properties #52682

Closed
@LastDragon-ru

Description

@LastDragon-ru

Symfony version(s) affected

6.1+

Description

Promoted properties may have a doc comment, so the following code (see https://3v4l.org/csbKl):

<?php

class A {
    public function __construct(
        /**
         * @var class-string
         */
        public string $promoted,
    ) {
        // empty
    }
}

$class    = new ReflectionClass(A::class);
$property = $class->getProperty('promoted');

var_dump($property->getDocComment());

will dump:

string(44) "/**
         * @var class-string
         */"

Phpstan itself also reads the type from the promoted property correctly (see https://phpstan.org/r/6de7935b-1d48-49bc-afd2-5748ec5523ac):

image

But the PhpStanExtractor ignores the property comment and always tried to use __construct comment:

if ($reflectionProperty->isPromoted()) {
$constructor = new \ReflectionMethod($class, '__construct');
$rawDocNode = $constructor->getDocComment();
$source = self::MUTATOR;
} else {

How to reproduce

// First, run "composer require symfony/property-info"
// Then, execute this file:

<?php

use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;

require_once __DIR__.'/vendor/autoload.php';

class A {
    public function __construct(
        /**
         * @var class-string
         */
        public string $promoted,
    ) {
        // empty
    }
}

$extractor = new PhpStanExtractor();
$types     = $extractor->getTypes(A::class, 'promoted');

var_dump($types); // NULL

Possible Solution

#52684

Additional Context

#46056

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions