-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Finder] Simplify SortableIterator #42111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Alexander M. Turek <me@derrabus.de>
@@ -83,15 +89,8 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder = | |||
*/ | |||
public function getIterator() | |||
{ | |||
if (1 === $this->sort) { | |||
return $this->iterator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this early return prevented the iterator_to_array
to array below, saving some memory in the process
} elseif (\is_callable($sort)) { | ||
$this->sort = $reverseOrder ? static function ($a, $b) use ($sort) { return -$sort($a, $b); } : $sort; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this adds a call that the previous logic skipped
@@ -70,9 +74,11 @@ public function __construct(\Traversable $iterator, $sort, bool $reverseOrder = | |||
return $order * ($a->getMTime() - $b->getMTime()); | |||
}; | |||
} elseif (self::SORT_BY_NONE === $sort) { | |||
$this->sort = $order; | |||
$this->sort = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not preserve the order, that was stored before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I was probably a bit too eager here.
@@ -27,6 +27,10 @@ class SortableIterator implements \IteratorAggregate | |||
public const SORT_BY_NAME_NATURAL = 6; | |||
|
|||
private $iterator; | |||
|
|||
/** | |||
* @var \Closure|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing type is \Closure|int|null
(\Closure|1|-1|null
to be precise with literal types)
I think, I misread the code while fixing the types. I'm closing this PR and will fix my PR against 6.0 accordingly. |
Spotted while working on #42106. The sort property is either a callable or zero. This means that
SortableIterator::getIterator()
currently handles impossible cases.