Skip to content

[Finder] Check PHP version before applying a workaround for a PHP bug #17134

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/Symfony/Component/Finder/Iterator/FilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Symfony\Component\Finder\Iterator;

/**
* This iterator just overrides the rewind method in order to correct a PHP bug.
* This iterator just overrides the rewind method in order to correct a PHP bug,
* which existed before version 5.5.23/5.6.7.
*
* @see https://bugs.php.net/bug.php?id=49104
* @see https://bugs.php.net/68557
*
* @author Alex Bogomazov
*/
Expand All @@ -28,18 +29,19 @@ abstract class FilterIterator extends \FilterIterator
*/
public function rewind()
{
if (PHP_VERSION_ID > 50607 || (PHP_VERSION_ID > 50523 && PHP_VERSION_ID < 50600)) {
parent::rewind();

return;
}

$iterator = $this;
while ($iterator instanceof \OuterIterator) {
$innerIterator = $iterator->getInnerIterator();

if ($innerIterator instanceof RecursiveDirectoryIterator) {
if ($innerIterator->isRewindable()) {
$innerIterator->next();
$innerIterator->rewind();
}
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
$iterator->getInnerIterator()->next();
$iterator->getInnerIterator()->rewind();
if ($innerIterator instanceof \FilesystemIterator) {
$innerIterator->next();
$innerIterator->rewind();
}
$iterator = $iterator->getInnerIterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ public function rewind()
return;
}

// @see https://bugs.php.net/bug.php?id=49104
parent::next();
// @see https://bugs.php.net/68557
if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas, Do you think this one also needs to be changed to a guard clause? I think that the same condition should be used in both cases, but here duplication of the parent::rewind call in the guard clause and in the normal flow would look weird.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks fine to me.

parent::next();
}

parent::rewind();
}
Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public function testNotContainsOnDirectory()
* Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
* with inner FilesystemIterator in an invalid state.
*
* @see https://bugs.php.net/bug.php?id=49104
* @see https://bugs.php.net/68557
*/
public function testMultipleLocations()
{
Expand All @@ -468,8 +468,12 @@ public function testMultipleLocations()
);

// it is expected that there are test.py test.php in the tmpDir
$finder = $this->buildFinder();
$finder->in($locations)->depth('< 1')->name('test.php');
$finder = new Finder();
$finder->in($locations)
// the default flag IGNORE_DOT_FILES fixes the problem indirectly
// so we set it to false for better isolation
->ignoreDotFiles(false)
->depth('< 1')->name('test.php');

$this->assertCount(1, $finder);
}
Expand All @@ -479,7 +483,7 @@ public function testMultipleLocations()
* AppendIterator which does an unnecessary rewind which leaves
* FilterIterator with inner FilesystemIterator in an invalid state.
*
* @see https://bugs.php.net/bug.php?id=49104
* @see https://bugs.php.net/68557
*/
public function testMultipleLocationsWithSubDirectories()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public function testFilterFilesystemIterators()
++$c;
}

// This would fail with \FilterIterator but works with Symfony\Component\Finder\Iterator\FilterIterator
// see https://bugs.php.net/bug.php?id=49104
// This would fail in php older than 5.5.23/5.6.7 with \FilterIterator
// but works with Symfony\Component\Finder\Iterator\FilterIterator
// see https://bugs.php.net/68557
$this->assertEquals(1, $c);
}
}