Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

derrabus
Copy link
Member

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets N/A
License MIT
Doc PR N/A

Spotted while working on #42106. The sort property is either a callable or zero. This means that SortableIterator::getIterator() currently handles impossible cases.

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;
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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.

Copy link
Member Author

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
Copy link
Member

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)

@derrabus
Copy link
Member Author

I think, I misread the code while fixing the types. I'm closing this PR and will fix my PR against 6.0 accordingly.

@derrabus derrabus closed this Jul 15, 2021
@derrabus derrabus deleted the bugfix/sortable-iterator branch July 15, 2021 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants