Skip to content

Commit

Permalink
apply the sort callback on the whole search result
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jan 4, 2021
1 parent fbd34cd commit 94a9b86
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
17 changes: 11 additions & 6 deletions Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,13 @@ public function getIterator()
}

if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
return $this->searchInDirectory($this->dirs[0]);
$iterator = $this->searchInDirectory($this->dirs[0]);

if ($this->sort || $this->reverseSorting) {
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
}

return $iterator;
}

$iterator = new \AppendIterator();
Expand All @@ -636,6 +642,10 @@ public function getIterator()
$iterator->append($it);
}

if ($this->sort || $this->reverseSorting) {
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
}

return $iterator;
}

Expand Down Expand Up @@ -782,11 +792,6 @@ private function searchInDirectory(string $dir): \Iterator
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
}

if ($this->sort || $this->reverseSorting) {
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting);
$iterator = $iteratorAggregate->getIterator();
}

return $iterator;
}

Expand Down
33 changes: 33 additions & 0 deletions Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,39 @@ public function testSort()
]), $finder->in(self::$tmpDir)->getIterator());
}

public function testSortAcrossDirectories()
{
$finder = $this->buildFinder()
->in([
self::$tmpDir,
self::$tmpDir.'/qux',
self::$tmpDir.'/foo',
])
->depth(0)
->files()
->filter(static function (\SplFileInfo $file): bool {
return '' !== $file->getExtension();
})
->sort(static function (\SplFileInfo $a, \SplFileInfo $b): int {
return strcmp($a->getExtension(), $b->getExtension()) ?: strcmp($a->getFilename(), $b->getFilename());
})
;

$this->assertOrderedIterator($this->toAbsolute([
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'test.py',
'foo/bar.tmp',
]), $finder->getIterator());
}

public function testFilter()
{
$finder = $this->buildFinder();
Expand Down
3 changes: 2 additions & 1 deletion Tests/Iterator/IteratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected function assertIterator($expected, \Traversable $iterator)

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

$this->assertEquals($expected, array_values($values));
}
Expand Down

2 comments on commit 94a9b86

@fliespl
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this commit introduced regression to for example translations.

It changed the way they get sorted causing /translations/ directory to be processed before /vendor/ one which in the end cause impossibility to override security transations.

@xabbuh
Copy link
Member Author

@xabbuh xabbuh commented on 94a9b86 Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see symfony/symfony#40116 which will be part of the next release

Please sign in to comment.