Skip to content

Commit d6ad6ed

Browse files
[Finder] Fix recursive filter iterator
1 parent 70affaa commit d6ad6ed

10 files changed

+38
-16
lines changed

Iterator/ExcludeDirectoryFilterIterator.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
*
1717
* @author Fabien Potencier <[email protected]>
1818
*/
19-
class ExcludeDirectoryFilterIterator extends FilterIterator
19+
class ExcludeDirectoryFilterIterator extends FilterIterator implements \RecursiveIterator
2020
{
21+
private $isRecursive;
2122
private $patterns;
2223

2324
/**
@@ -28,6 +29,7 @@ class ExcludeDirectoryFilterIterator extends FilterIterator
2829
*/
2930
public function __construct(\Iterator $iterator, array $directories)
3031
{
32+
$this->isRecursive = $iterator instanceof \RecursiveIterator;
3133
$this->patterns = array();
3234
foreach ($directories as $directory) {
3335
$this->patterns[] = '#(^|/)'.preg_quote($directory, '#').'(/|$)#';
@@ -53,4 +55,17 @@ public function accept()
5355

5456
return true;
5557
}
58+
59+
public function hasChildren()
60+
{
61+
return $this->isRecursive && $this->getInnerIterator()->hasChildren();
62+
}
63+
64+
public function getChildren()
65+
{
66+
$children = new self($this->getInnerIterator()->getChildren(), array());
67+
$children->patterns = $this->patterns;
68+
69+
return $children;
70+
}
5671
}

Iterator/FilterIterator.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,8 @@
1818
*
1919
* @author Alex Bogomazov
2020
*/
21-
abstract class FilterIterator extends \FilterIterator implements \RecursiveIterator
21+
abstract class FilterIterator extends \FilterIterator
2222
{
23-
public function hasChildren()
24-
{
25-
return $this->getInnerIterator() instanceof \RecursiveIterator && $this->getInnerIterator()->hasChildren();
26-
}
27-
28-
public function getChildren()
29-
{
30-
return $this->getInnerIterator()->getChildren();
31-
}
32-
3323
/**
3424
* This is a workaround for the problem with \FilterIterator leaving inner \FilesystemIterator in wrong state after
3525
* rewind in some cases.

Tests/FinderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ public function testIgnoreVCS()
152152
{
153153
$finder = $this->buildFinder();
154154
$this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
155-
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
155+
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
156156

157157
$finder = $this->buildFinder();
158158
$finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
159-
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
159+
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
160160

161161
$finder = $this->buildFinder();
162162
$this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
@@ -167,11 +167,11 @@ public function testIgnoreDotFiles()
167167
{
168168
$finder = $this->buildFinder();
169169
$this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
170-
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
170+
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
171171

172172
$finder = $this->buildFinder();
173173
$finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
174-
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
174+
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
175175

176176
$finder = $this->buildFinder();
177177
$this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));

Tests/Iterator/DateRangeFilterIteratorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getAcceptData()
3737
'foo/bar.tmp',
3838
'test.php',
3939
'toto',
40+
'toto/.git',
4041
'.bar',
4142
'.foo',
4243
'.foo/.bar',
@@ -49,6 +50,7 @@ public function getAcceptData()
4950
'test.py',
5051
'foo',
5152
'toto',
53+
'toto/.git',
5254
'.bar',
5355
'.foo',
5456
'.foo/.bar',
@@ -62,6 +64,7 @@ public function getAcceptData()
6264
'foo/bar.tmp',
6365
'test.php',
6466
'toto',
67+
'toto/.git',
6568
'.foo',
6669
);
6770

Tests/Iterator/DepthRangeFilterIteratorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function getAcceptData()
5050
'foo/bar.tmp',
5151
'test.php',
5252
'toto',
53+
'toto/.git',
5354
'.foo',
5455
'.foo/.bar',
5556
'.bar',
@@ -58,12 +59,14 @@ public function getAcceptData()
5859
);
5960

6061
$graterThanOrEqualTo1 = array(
62+
'toto/.git',
6163
'foo/bar.tmp',
6264
'.foo/.bar',
6365
'.foo/bar',
6466
);
6567

6668
$equalTo1 = array(
69+
'toto/.git',
6770
'foo/bar.tmp',
6871
'.foo/.bar',
6972
'.foo/bar',

Tests/Iterator/ExcludeDirectoryFilterIteratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function getAcceptData()
3939
'test.py',
4040
'test.php',
4141
'toto',
42+
'toto/.git',
4243
'foo bar',
4344
);
4445

@@ -53,6 +54,7 @@ public function getAcceptData()
5354
'foo/bar.tmp',
5455
'test.php',
5556
'toto',
57+
'toto/.git',
5658
'foo bar',
5759
);
5860

Tests/Iterator/FileTypeFilterIteratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getAcceptData()
4343
'.git',
4444
'foo',
4545
'toto',
46+
'toto/.git',
4647
'.foo',
4748
);
4849

Tests/Iterator/RealIteratorTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static function setUpBeforeClass()
3131
'foo/bar.tmp',
3232
'test.php',
3333
'toto/',
34+
'toto/.git/',
3435
'foo bar',
3536
);
3637

Tests/Iterator/SizeRangeFilterIteratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function getAcceptData()
3636
'foo',
3737
'test.php',
3838
'toto',
39+
'toto/.git',
3940
);
4041

4142
return array(

Tests/Iterator/SortableIteratorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ public function getAcceptData()
8585
'test.php',
8686
'test.py',
8787
'toto',
88+
'toto/.git',
8889
);
8990

9091
$sortByType = array(
9192
'.foo',
9293
'.git',
9394
'foo',
9495
'toto',
96+
'toto/.git',
9597
'.bar',
9698
'.foo/.bar',
9799
'.foo/bar',
@@ -113,6 +115,7 @@ public function getAcceptData()
113115
'test.php',
114116
'test.py',
115117
'toto',
118+
'toto/.git',
116119
);
117120

118121
$sortByAccessedTime = array(
@@ -127,6 +130,7 @@ public function getAcceptData()
127130
'test.py',
128131
'foo',
129132
'toto',
133+
'toto/.git',
130134
'foo bar',
131135
),
132136
// This file was accessed after sleeping for 1 sec
@@ -143,6 +147,7 @@ public function getAcceptData()
143147
'foo',
144148
'foo/bar.tmp',
145149
'toto',
150+
'toto/.git',
146151
'foo bar',
147152
),
148153
array('test.php'),
@@ -159,6 +164,7 @@ public function getAcceptData()
159164
'foo',
160165
'foo/bar.tmp',
161166
'toto',
167+
'toto/.git',
162168
'foo bar',
163169
),
164170
array('test.php'),

0 commit comments

Comments
 (0)