Skip to content

Commit 2723e44

Browse files
committed
bug symfony#10849 [WIP][Finder] Fix wrong implementation on sortable callback comparator (ProPheT777)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes symfony#10849). Discussion ---------- [WIP][Finder] Fix wrong implementation on sortable callback comparator | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | no Working on this issue, i find SORT_BY_ACCESSED_TIME, SORT_BY_CHANGED_TIME, SORT_BY_MODIFIED_TIME was not tested. - [x] Add test for SORT_BY_ACCESSED_TIME case - [x] Add test for SORT_BY_CHANGED_TIME case - [x] add test for SORT_BY_MODIFIED_TIME case Commits ------- b965fa2 [WIP][Finder] Fix wrong implementation on sortable callback comparator
2 parents c8476ee + b965fa2 commit 2723e44

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class SortableIterator implements \IteratorAggregate
3030
/**
3131
* Constructor.
3232
*
33-
* @param \Traversable $iterator The Iterator to filter
34-
* @param int|callback $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
33+
* @param \Traversable $iterator The Iterator to filter
34+
* @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
3535
*
3636
* @throws \InvalidArgumentException
3737
*/
@@ -55,20 +55,20 @@ public function __construct(\Traversable $iterator, $sort)
5555
};
5656
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
5757
$this->sort = function ($a, $b) {
58-
return ($a->getATime() > $b->getATime());
58+
return ($a->getATime() - $b->getATime());
5959
};
6060
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
6161
$this->sort = function ($a, $b) {
62-
return ($a->getCTime() > $b->getCTime());
62+
return ($a->getCTime() - $b->getCTime());
6363
};
6464
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
6565
$this->sort = function ($a, $b) {
66-
return ($a->getMTime() > $b->getMTime());
66+
return ($a->getMTime() - $b->getMTime());
6767
};
6868
} elseif (is_callable($sort)) {
6969
$this->sort = $sort;
7070
} else {
71-
throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.');
71+
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
7272
}
7373
}
7474

src/Symfony/Component/Finder/Tests/Iterator/SortableIteratorTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ public function testConstructor()
3030
*/
3131
public function testAccept($mode, $expected)
3232
{
33+
if (!is_callable($mode)) {
34+
switch ($mode) {
35+
case SortableIterator::SORT_BY_ACCESSED_TIME :
36+
file_get_contents(self::toAbsolute('.git'));
37+
sleep(1);
38+
file_get_contents(self::toAbsolute('.bar'));
39+
break;
40+
case SortableIterator::SORT_BY_CHANGED_TIME :
41+
file_put_contents(self::toAbsolute('test.php'), 'foo');
42+
sleep(1);
43+
file_put_contents(self::toAbsolute('test.py'), 'foo');
44+
break;
45+
case SortableIterator::SORT_BY_MODIFIED_TIME :
46+
file_put_contents(self::toAbsolute('test.php'), 'foo');
47+
sleep(1);
48+
file_put_contents(self::toAbsolute('test.py'), 'foo');
49+
break;
50+
}
51+
}
52+
3353
$inner = new Iterator(self::$files);
3454

3555
$iterator = new SortableIterator($inner, $mode);
@@ -82,9 +102,54 @@ public function getAcceptData()
82102
'toto',
83103
);
84104

105+
$sortByAccessedTime = array(
106+
'foo/bar.tmp',
107+
'test.php',
108+
'toto',
109+
'foo bar',
110+
'foo',
111+
'test.py',
112+
'.foo',
113+
'.foo/.bar',
114+
'.foo/bar',
115+
'.git',
116+
'.bar'
117+
);
118+
119+
$sortByChangedTime = array(
120+
'foo',
121+
'foo/bar.tmp',
122+
'toto',
123+
'.git',
124+
'.bar',
125+
'.foo',
126+
'foo bar',
127+
'.foo/.bar',
128+
'.foo/bar',
129+
'test.php',
130+
'test.py'
131+
);
132+
133+
$sortByModifiedTime = array(
134+
'foo/bar.tmp',
135+
'foo',
136+
'toto',
137+
'.git',
138+
'.bar',
139+
'.foo',
140+
'foo bar',
141+
'.foo/.bar',
142+
'.foo/bar',
143+
'test.php',
144+
'test.py'
145+
);
146+
85147
return array(
86148
array(SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)),
87149
array(SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)),
150+
array(SortableIterator::SORT_BY_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)),
151+
array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)),
152+
array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)),
88153
array(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }, $this->toAbsolute($customComparison)),
89154
);
90155
}

0 commit comments

Comments
 (0)