Skip to content

Commit e2e8e05

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Remove full head content in HTML to text converter apply the sort callback on the whole search result
2 parents 9e322de + 94a9b86 commit e2e8e05

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

Finder.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,13 @@ public function getIterator()
611611
}
612612

613613
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
614-
return $this->searchInDirectory($this->dirs[0]);
614+
$iterator = $this->searchInDirectory($this->dirs[0]);
615+
616+
if ($this->sort || $this->reverseSorting) {
617+
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
618+
}
619+
620+
return $iterator;
615621
}
616622

617623
$iterator = new \AppendIterator();
@@ -623,6 +629,10 @@ public function getIterator()
623629
$iterator->append($it);
624630
}
625631

632+
if ($this->sort || $this->reverseSorting) {
633+
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
634+
}
635+
626636
return $iterator;
627637
}
628638

@@ -767,11 +777,6 @@ private function searchInDirectory(string $dir): \Iterator
767777
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
768778
}
769779

770-
if ($this->sort || $this->reverseSorting) {
771-
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting);
772-
$iterator = $iteratorAggregate->getIterator();
773-
}
774-
775780
return $iterator;
776781
}
777782

Tests/FinderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,39 @@ public function testSort()
832832
]), $finder->in(self::$tmpDir)->getIterator());
833833
}
834834

835+
public function testSortAcrossDirectories()
836+
{
837+
$finder = $this->buildFinder()
838+
->in([
839+
self::$tmpDir,
840+
self::$tmpDir.'/qux',
841+
self::$tmpDir.'/foo',
842+
])
843+
->depth(0)
844+
->files()
845+
->filter(static function (\SplFileInfo $file): bool {
846+
return '' !== $file->getExtension();
847+
})
848+
->sort(static function (\SplFileInfo $a, \SplFileInfo $b): int {
849+
return strcmp($a->getExtension(), $b->getExtension()) ?: strcmp($a->getFilename(), $b->getFilename());
850+
})
851+
;
852+
853+
$this->assertOrderedIterator($this->toAbsolute([
854+
'qux_0_1.php',
855+
'qux_1000_1.php',
856+
'qux_1002_0.php',
857+
'qux_10_2.php',
858+
'qux_12_0.php',
859+
'qux_2_0.php',
860+
'test.php',
861+
'qux/baz_100_1.py',
862+
'qux/baz_1_2.py',
863+
'test.py',
864+
'foo/bar.tmp',
865+
]), $finder->getIterator());
866+
}
867+
835868
public function testFilter()
836869
{
837870
$finder = $this->buildFinder();

Tests/Iterator/IteratorTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function assertIterator($expected, \Traversable $iterator)
3131

3232
protected function assertOrderedIterator($expected, \Traversable $iterator)
3333
{
34-
$values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
34+
$values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', \DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator));
35+
$expected = array_map(function ($path) { return str_replace('/', \DIRECTORY_SEPARATOR, $path); }, $expected);
3536

3637
$this->assertEquals($expected, array_values($values));
3738
}

0 commit comments

Comments
 (0)