8000 [PropertyInfo] Fix PHPStan properties type in trait by mtarld · Pull Request #54675 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[PropertyInfo] Fix PHPStan properties type in trait #54675

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

Merged
merged 1 commit into from
Apr 22, 2024

Conversation

mtarld
Copy link
Contributor
@mtarld mtarld commented Apr 19, 2024
Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #54569
License MIT

Handle traits properly in PhpStanExtractor.

@mtarld mtarld requested a review from dunglas as a code owner April 19, 2024 15:50
@carsonbot carsonbot added this to the 7.1 milestone Apr 19, 2024
@mtarld mtarld changed the base branch from 7.1 to 5.4 April 19, 2024 15:50
@mtarld mtarld force-pushed the fix/phpstan-trait branch from c628b73 to 0b1b275 Compare April 19, 2024 15:51
@nicolas-grekas nicolas-grekas modified the milestones: 7.1, 5.4 Apr 22, 2024
@nicolas-grekas
Copy link
Member

Thank you @mtarld.

@nicolas-grekas nicolas-grekas merged commit a4dc5f7 into symfony:5.4 Apr 22, 2024
7 of 12 checks passed
@mtarld mtarld deleted the fix/phpstan-trait branch April 22, 2024 14:19
This was referenced Apr 29, 2024
@themasch
Copy link
Contributor
themasch commented May 6, 2024

This change caused some breakage for us (that was quickly resolved), so maybe someone else runs into a similar issue and finds this helpful:

We had code that looked something like this:

trait Foo {
   public readonly array $items;
   // some methods that do stuff on items to implement \Countable, \ArrayAccess, etc
}

final readonly class SomeList implements \Countable, \ArrayAccess {
  use Foo;
  public function __construct( 
    /** @var SomeItem[] */
    #[SerializerName("SomeItem")]
    public array $items;
  ) { }
}

final readonly class OtherList implements \Countable, \ArrayAccess {
  use Foo;
  public function __construct( 
    /** @var OtherItem[] */
    #[SerializerName("OtherItemName")]
    public array $items;
  ) { }
}

I am unsure why the $items property was added to the trait as well, that was certainly unessesary and incorrect.
But it worked before this change ;)

Obviously this change is correct and we gladly only hat one case where we relied on the old behaviour (by accident), so fixing it was easy. We just remove the property from the trait, the Phpstan extractor looked at the metadata of the constructor param instead of the trait property again, and tests went green!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[PropertyInfo] PhpStanExtractor is not correctly resolving types declared in traits
6 participants
0