Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cba206a

Browse files
alebofabpot
authored andcommitted
[Finder] Check PHP version before applying a workaround for a PHP bug
1 parent 5d63c55 commit cba206a

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/Symfony/Component/Finder/Iterator/FilterIterator.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Symfony\Component\Finder\Iterator;
1313

1414
/**
15-
* This iterator just overrides the rewind method in order to correct a PHP bug.
15+
* This iterator just overrides the rewind method in order to correct a PHP bug,
16+
* which existed before version 5.5.23/5.6.7.
1617
*
17-
* @see https://bugs.php.net/bug.php?id=49104
18+
* @see https://bugs.php.net/68557
1819
*
1920
* @author Alex Bogomazov
2021
*/
@@ -28,18 +29,19 @@ abstract class FilterIterator extends \FilterIterator
2829
*/
2930
public function rewind()
3031
{
32+
if (PHP_VERSION_ID > 50607 || (PHP_VERSION_ID > 50523 && PHP_VERSION_ID < 50600)) {
33+
parent::rewind();
34+
35+
return;
36+
}
37+
3138
$iterator = $this;
3239
while ($iterator instanceof \OuterIterator) {
3340
$innerIterator = $iterator->getInnerIterator();
3441

35-
if ($innerIterator instanceof RecursiveDirectoryIterator) {
36-
if ($innerIterator->isRewindable()) {
37-
$innerIterator->next();
38-
$innerIterator->rewind();
39-
}
40-
} elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) {
41-
$iterator->getInnerIterator()->next();
42-
$iterator->getInnerIterator()->rewind();
42+
if ($innerIterator instanceof \FilesystemIterator) {
43+
$innerIterator->next();
44+
$innerIterator->rewind();
4345
}
4446
$iterator = $iterator->getInnerIterator();
4547
}

src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ public function rewind()
118118
return;
119119
}
120120

121-
// @see https://bugs.php.net/bug.php?id=49104
122-
parent::next();
121+
// @see https://bugs.php.net/68557
122+
if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) {
123+
parent::next();
124+
}
123125

124126
parent::rewind();
125127
}

src/Symfony/Component/Finder/Tests/FinderTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public function testNotContainsOnDirectory()
458458
* Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
459459
* with inner FilesystemIterator in an invalid state.
460460
*
461-
* @see https://bugs.php.net/bug.php?id=49104
461+
* @see https://bugs.php.net/68557
462462
*/
463463
public function testMultipleLocations()
464464
{
@@ -468,8 +468,12 @@ public function testMultipleLocations()
468468
);
469469

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

474478
$this->assertCount(1, $finder);
475479
}
@@ -479,7 +483,7 @@ public function testMultipleLocations()
479483
* AppendIterator which does an unnecessary rewind which leaves
480484
* FilterIterator with inner FilesystemIterator in an invalid state.
481485
*
482-
* @see https://bugs.php.net/bug.php?id=49104
486+
* @see https://bugs.php.net/68557
483487
*/
484488
public function testMultipleLocationsWithSubDirectories()
485489
{

src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public function testFilterFilesystemIterators()
4343
++$c;
4444
}
4545

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

0 commit comments

Comments
 (0)