From 15f93f358c7f725a5e701914f12654392a2b5bea Mon Sep 17 00:00:00 2001 From: Alex Bogomazov Date: Thu, 24 Dec 2015 01:45:26 +0300 Subject: [PATCH 1/4] [Finder] Check PHP version before applying a workaround for a pre PHP 5.5.23/5.6.7 bug. --- .../Finder/Iterator/FilterIterator.php | 31 ++++++++++--------- .../Iterator/RecursiveDirectoryIterator.php | 7 +++-- .../Tests/Iterator/FilterIteratorTest.php | 5 +-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Component/Finder/Iterator/FilterIterator.php b/src/Symfony/Component/Finder/Iterator/FilterIterator.php index f4da44c4cdd59..d4a3ee71a2f89 100644 --- a/src/Symfony/Component/Finder/Iterator/FilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/FilterIterator.php @@ -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/bug.php?id=68557 * * @author Alex Bogomazov */ @@ -28,22 +29,24 @@ abstract class FilterIterator extends \FilterIterator */ public function rewind() { - $iterator = $this; - while ($iterator instanceof \OuterIterator) { - $innerIterator = $iterator->getInnerIterator(); + if (version_compare(PHP_VERSION, '5.5.23', '<') + or (version_compare(PHP_VERSION, '5.6.0', '>=') and version_compare(PHP_VERSION, '5.6.7', '<'))) { + $iterator = $this; + while ($iterator instanceof \OuterIterator) { + $innerIterator = $iterator->getInnerIterator(); - if ($innerIterator instanceof RecursiveDirectoryIterator) { - if ($innerIterator->isRewindable()) { - $innerIterator->next(); - $innerIterator->rewind(); + if ($innerIterator instanceof RecursiveDirectoryIterator) { + if ($innerIterator->isRewindable()) { + $innerIterator->next(); + $innerIterator->rewind(); + } + } elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) { + $iterator->getInnerIterator()->next(); + $iterator->getInnerIterator()->rewind(); } - } elseif ($iterator->getInnerIterator() instanceof \FilesystemIterator) { - $iterator->getInnerIterator()->next(); - $iterator->getInnerIterator()->rewind(); + $iterator = $iterator->getInnerIterator(); } - $iterator = $iterator->getInnerIterator(); } - parent::rewind(); } } diff --git a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php index 3f65d69269756..e868206ca040a 100644 --- a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php +++ b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php @@ -118,8 +118,11 @@ public function rewind() return; } - // @see https://bugs.php.net/bug.php?id=49104 - parent::next(); + // @see https://bugs.php.net/bug.php?id=68557 + if (version_compare(PHP_VERSION, '5.5.23', '<') + or (version_compare(PHP_VERSION, '5.6.0', '>=') and version_compare(PHP_VERSION, '5.6.7', '<'))) { + parent::next(); + } parent::rewind(); } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php index 41e05a0a7b390..8c8aed85f5555 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php @@ -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/bug.php?id=68557 $this->assertEquals(1, $c); } } From f2e47df24f9d9e82a2fbc29c80b24c53865d89b7 Mon Sep 17 00:00:00 2001 From: Alex Bogomazov Date: Fri, 25 Dec 2015 22:14:14 +0300 Subject: [PATCH 2/4] Corrected version check method. --- src/Symfony/Component/Finder/Iterator/FilterIterator.php | 3 +-- .../Component/Finder/Iterator/RecursiveDirectoryIterator.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Finder/Iterator/FilterIterator.php b/src/Symfony/Component/Finder/Iterator/FilterIterator.php index d4a3ee71a2f89..93abcef664f91 100644 --- a/src/Symfony/Component/Finder/Iterator/FilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/FilterIterator.php @@ -29,8 +29,7 @@ abstract class FilterIterator extends \FilterIterator */ public function rewind() { - if (version_compare(PHP_VERSION, '5.5.23', '<') - or (version_compare(PHP_VERSION, '5.6.0', '>=') and version_compare(PHP_VERSION, '5.6.7', '<'))) { + if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) { $iterator = $this; while ($iterator instanceof \OuterIterator) { $innerIterator = $iterator->getInnerIterator(); diff --git a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php index e868206ca040a..c8fce577ab7f3 100644 --- a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php +++ b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php @@ -119,8 +119,7 @@ public function rewind() } // @see https://bugs.php.net/bug.php?id=68557 - if (version_compare(PHP_VERSION, '5.5.23', '<') - or (version_compare(PHP_VERSION, '5.6.0', '>=') and version_compare(PHP_VERSION, '5.6.7', '<'))) { + if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) { parent::next(); } From 4d97385a927b9255892967408ff638ea0f716b6a Mon Sep 17 00:00:00 2001 From: Alex Bogomazov Date: Mon, 11 Jan 2016 00:50:41 +0300 Subject: [PATCH 3/4] Minor updates and some cleanup. --- .../Finder/Iterator/FilterIterator.php | 29 +++++++++---------- .../Iterator/RecursiveDirectoryIterator.php | 2 +- .../Component/Finder/Tests/FinderTest.php | 12 +++++--- .../Tests/Iterator/FilterIteratorTest.php | 2 +- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Finder/Iterator/FilterIterator.php b/src/Symfony/Component/Finder/Iterator/FilterIterator.php index 93abcef664f91..d004003b91766 100644 --- a/src/Symfony/Component/Finder/Iterator/FilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/FilterIterator.php @@ -15,7 +15,7 @@ * 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=68557 + * @see https://bugs.php.net/68557 * * @author Alex Bogomazov */ @@ -29,23 +29,22 @@ abstract class FilterIterator extends \FilterIterator */ public function rewind() { - if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) { - $iterator = $this; - while ($iterator instanceof \OuterIterator) { - $innerIterator = $iterator->getInnerIterator(); + 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(); - } - $iterator = $iterator->getInnerIterator(); + if ($innerIterator instanceof \FilesystemIterator) { + $innerIterator->next(); + $innerIterator->rewind(); } + $iterator = $iterator->getInnerIterator(); } + parent::rewind(); } } diff --git a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php index c8fce577ab7f3..402033a5c2816 100644 --- a/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php +++ b/src/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php @@ -118,7 +118,7 @@ public function rewind() return; } - // @see https://bugs.php.net/bug.php?id=68557 + // @see https://bugs.php.net/68557 if (PHP_VERSION_ID < 50523 || PHP_VERSION_ID >= 50600 && PHP_VERSION_ID < 50607) { parent::next(); } diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index cee8bd7530767..624d4547e519e 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -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() { @@ -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); } @@ -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() { diff --git a/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php index 8c8aed85f5555..4f12d142e7f27 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/FilterIteratorTest.php @@ -45,7 +45,7 @@ public function testFilterFilesystemIterators() // 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/bug.php?id=68557 + // see https://bugs.php.net/68557 $this->assertEquals(1, $c); } } From 69f21e3cd227611d453f864602d7b06d9981fbb5 Mon Sep 17 00:00:00 2001 From: Alex Bogomazov Date: Mon, 11 Jan 2016 00:59:12 +0300 Subject: [PATCH 4/4] Code style fix. --- src/Symfony/Component/Finder/Iterator/FilterIterator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Finder/Iterator/FilterIterator.php b/src/Symfony/Component/Finder/Iterator/FilterIterator.php index d004003b91766..24adeb68f9040 100644 --- a/src/Symfony/Component/Finder/Iterator/FilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/FilterIterator.php @@ -31,6 +31,7 @@ public function rewind() { if (PHP_VERSION_ID > 50607 || (PHP_VERSION_ID > 50523 && PHP_VERSION_ID < 50600)) { parent::rewind(); + return; }