vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.54k stars 660 forks source link

No more errors detected when a duplicate class is found #10871

Open Spea opened 5 months ago

Spea commented 5 months ago

Whenever a DuplicateClass error is detected, psalm will stop analysing any child classes which leads to potential undetected issues.

This can be replicated via the following snippet: https://psalm.dev/r/5506f97530 (new Foo() should also raise a UndefinedClass error)

When looking at the code in the ClassLikeNodeScanner, stopping analysis for any potential child classes seems to be intentional whenever a DuplicateClass error was detected.

However, while this might not necessarily be an issue when you have complete control over the code, it becomes quite critical when the DuplicateClass error is coming from a vendor directory, as this leads to psalm not detecting any issues in your own files.

I also created a test that replicates this behaviour: https://github.com/vimeo/psalm/compare/master...rebuy-de:psalm:issues-ignored-on-duplicate-class

Side note: In our case we extended the AbstractFOSRestController from the FOSRestBundle and our class had some minor issues but psalm did not raise any errors.

psalm-github-bot[bot] commented 5 months ago

I found these snippets:

https://psalm.dev/r/5506f97530 ```php '7.0') { abstract class A { public static function testFoo(): array { return []; } } } else { abstract class A { public static function testFoo() { return []; } } } class B extends A { public function action(): void { new Foo(); } } ``` ``` Psalm output (using commit ef3b018): ERROR: DuplicateClass - 12:20 - Class A has already been defined in /var/www/vhosts/psalm.dev/httpdocs/src/somefile.php ```
j6s commented 3 weeks ago

I can confirm, that this also leads to psalm not detecting issues in classes extending Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository of doctrine/doctrine-bundle, which has a backwards-compatible Definition: https://github.com/doctrine/DoctrineBundle/blob/2.12.x/src/Repository/ServiceEntityRepository.php that is detected as duplicate.

j6s commented 3 weeks ago

Seems like this is the same issue (or related) as #9216