vimeo / psalm

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

Possibly `&` types operator was broken in 4.24.0 release #8215

Open zerkms opened 2 years ago

zerkms commented 2 years ago

This code was working fine for years until 4.24.0 was released

        $files = $this->fetch($this->parameters['dir']);
        foreach ($files as $file) {
    /**
     * @psalm-return Finder&IteratorAggregate<array-key, SplFileInfo>
     */
    private function fetch(string $dir): IteratorAggregate
    {

It now returns the following warning:

ERROR: MixedAssignment - <redacted>/Directory.php:38:28 - Unable to determine the type that $file is being assigned to (see https://psalm.dev/032)
        foreach ($files as $file) {

ERROR: MixedArgument - <redacted>/Directory.php:39:32 - Argument 1 of <redacted>\Directory::filter cannot be mixed, expecting Symfony\Component\Finder\SplFileInfo (see https://psalm.dev/030)
            if (!$this->filter($file)) {

  The type of $file is sourced from here - <redacted>/Directory.php:38:28
        foreach ($files as $file) {

Trace returns type as expected:

ERROR: Trace - <redacted>/Directory.php:41:9 - $files: Symfony\Component\Finder\Finder&IteratorAggregate<array-key, Symfony\Component\Finder\SplFileInfo> (see https://psalm.dev/224)
        /** @psalm-trace $files */

It's a bit tricky to reproduce on psalm.dev because it requires a lot of types/code to be defined, but I may try do that if it's still not enough details here.

AndrolGenhald commented 2 years ago

A reproducer would be very helpful.

zerkms commented 2 years ago

I created a repro repository here: https://github.com/zerkms/psalm-8215-repro

I cannot create one in a single file or on psalm.dev unfortunately