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

Skip to content

PhpStanExtractor doesn't support docblock from promoted properties #52682

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

Closed
LastDragon-ru opened this issue Nov 22, 2023 · 0 comments
Closed

Comments

@LastDragon-ru
Copy link
Contributor

LastDragon-ru commented Nov 22, 2023

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

nicolas-grekas added a commit that referenced this issue Nov 24, 2023
…`PhpStanExtractor` (LastDragon-ru)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[PropertyInfo] Fixed promoted property type detection for `PhpStanExtractor`

| Q             | A
| ------------- | ---
| Branch?       | 6.3 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #52682 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

Implementation aligned with actual phpstan behaviour, please see https://phpstan.org/r/8278f037-5a67-4e53-8352-a13585d2eca5

<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the documentation.

Additionally (see https://symfony.com/releases):
 - Always add tests and ensure they pass.
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the latest branch.
 - For new features, provide some code snippets to help understand usage.
 - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
 - Never break backward compatibility (see https://symfony.com/bc).
-->

Commits
-------

430ec79 [PropertyInfo] Fixed promoted property type detection for `PhpStanExtractor`
LastDragon-ru added a commit to LastDragon-ru/lara-asp that referenced this issue Nov 30, 2023
LastDragon-ru added a commit to LastDragon-ru/lara-asp-serializer that referenced this issue Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants