Skip to content

Finder iterators declare wrong SplFileInfo iteration item type (using @extends) #45672

Closed
@InvisibleSmiley

Description

@InvisibleSmiley

Symfony version(s) affected

5.4

Description

symfony/finder iterators pretend to act on items of type \SplFileInfo when in fact (at least some of the iterators) act upon items of type \Symfony\Component\Finder\SplFileInfo which includes methods getRelativePath and getRelativePathname.

Example of affected iterator: PathFilterIterator

How to reproduce

Example showing actual behavior and PHPStan type analysis (either execute script or pass it to PHPStan):

<?php

declare(strict_types=1);

use Symfony\Component\Finder\Iterator\PathFilterIterator;

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

$it = new PathFilterIterator(new ArrayIterator([new \SplFileInfo(__FILE__)]), [], []);

if (class_exists(\PHPStan::class)) {
    \PHPStan\dumpType($it->current());
    $example = new \Symfony\Component\Finder\SplFileInfo(__FILE__, '', '');
    \PHPStan\dumpType($example);
}

// The following will throw 'Call to undefined method SplFileInfo::getRelativePathname()'
foreach ($it as $item) {
    var_dump($item);
}

Possible Solution

Fix item type in @extends of all iterators that actually require the Symfony variant of SplFileInfo (or maybe all iterators?)

Additional Context

No response

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