Skip to content

Commit ef5300d

Browse files
committed
merged branch jfsimon/finder-iterator-keys-fix (PR #6911)
This PR was merged into the master branch. Commits ------- ef593fa [Finder] Fixed iterator keys d16e28a [Finder] Added iterator keys test Discussion ---------- [Finder] Fixes iterator keys | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6891 | License | MIT Previous PR I did on finder introduced a BR break (finder iterator keys must be the pathname of the file, not an incremented index). This PR adds a test to ensure this wont be broken again and fixes the BC break.
2 parents 4ecaa10 + ef593fa commit ef5300d

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class FilePathsIterator extends \ArrayIterator
4040
*/
4141
private $subPathname;
4242

43+
/**
44+
* @var SplFileInfo
45+
*/
46+
private $current;
47+
4348
/**
4449
* @param array $paths List of paths returned by shell command
4550
* @param string $baseDir Base dir for relative path building
@@ -70,21 +75,27 @@ public function __call($name, array $arguments)
7075
*/
7176
public function current()
7277
{
73-
return new SplFileInfo(parent::current(), $this->subPath, $this->subPathname);
78+
return $this->current;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function key()
85+
{
86+
return $this->current->getPathname();
7487
}
7588

7689
public function next()
7790
{
7891
parent::next();
79-
80-
$this->buildSubPath();
92+
$this->buildProperties();
8193
}
8294

8395
public function rewind()
8496
{
8597
parent::rewind();
86-
87-
$this->buildSubPath();
98+
$this->buildProperties();
8899
}
89100

90101
/**
@@ -103,7 +114,7 @@ public function getSubPathname()
103114
return $this->subPathname;
104115
}
105116

106-
private function buildSubPath()
117+
private function buildProperties()
107118
{
108119
$absolutePath = parent::current();
109120

@@ -114,5 +125,7 @@ private function buildSubPath()
114125
} else {
115126
$this->subPath = $this->subPathname = '';
116127
}
128+
129+
$this->current = new SplFileInfo(parent::current(), $this->subPath, $this->subPathname);
117130
}
118131
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,19 @@ public function testMultipleLocations(Adapter\AdapterInterface $adapter)
576576
$this->assertEquals(1, count($finder));
577577
}
578578

579+
/**
580+
* Iterator keys must be the file pathname.
581+
*
582+
* @dataProvider getAdaptersTestData
583+
*/
584+
public function testIteratorKeys(Adapter\AdapterInterface $adapter)
585+
{
586+
$finder = $this->buildFinder($adapter)->in(self::$tmpDir);
587+
foreach ($finder as $key => $file) {
588+
$this->assertEquals($file->getPathname(), $key);
589+
}
590+
}
591+
579592
public function testAdaptersOrdering()
580593
{
581594
$finder = Finder::create()

0 commit comments

Comments
 (0)