Repository navigation
Can sort locations - #7
Conversation
Make the class final to be able to use "new self" without unexpecting results in case of inheritance. Acceps any iterable instead of only array to be more compliant.
| /** | ||
| * @param iterable<Location> $locations | ||
| */ | ||
| public static function bySorting(iterable $locations, ?callable $sortStrategy = null): self |
There was a problem hiding this comment.
I think I would prefer:
$locations = (new Locations($locations))->sorted();
with no $sortStrategy - extension points are easy to add but hard to remove, and can be implemented in many different ways. So for now I think having a single, closed, sorted() factory method is enough, and probably nothing more would be needed?
| public static function bySorting(iterable $locations): self | ||
| { | ||
| return (new self($locations))->sorted(); | ||
| } |
There was a problem hiding this comment.
"by sorting" doesn't really make sense - Locations::bySorting: by makes sense to me for retrieving a set of things (e.g. usersByFirstName).
I think $locations->sorted() is fine.
There was a problem hiding this comment.
For me it's obvious with the static call that it is a static method factory, so it means create by sorting.
I could rename it like that if you prefer: Locations::createBySorting(iterable)
There is also the optionLocations::sorted(iterable) but this will require to rename the current sorted method back to sort.
At the end this is only a helper to avoid having ugly things like: return (new Locations($locations))->sorted();
After reading your first review I think you had that in mind from the start so I will remove it right away to speed up things.
| return $first->offset()->toInt() - $second->offset()->toInt(); | ||
| }); | ||
|
|
||
| return $this; |
There was a problem hiding this comment.
this needs to return a new instance. the locations should be immuatble.
$locations = $this->locations;
usort(...);
return new self($locations);
There was a problem hiding this comment.
My bad, just saw the append() method but didn't check the implementation.
I'm used to withXXX() method for immutable objects ><
a0c18fd to
ad5524b
Compare
|
Thanks, a static constructor would make it slightly less ugly in this particular use case, but equally also happy not to add one yet. |
The idea was to be able to sort locations directly from the list object.
From there I also replaced
arraybyiterablein the type hints so the code is more compliant.And I made the class final to avoid any surprises because of the
new self().This last point is a matter of taste, personally I don't like surprises and I don't see the point of extending it anyway, but if you prefer I can remove it quit easily.
If validated, the changes will be used to sort the result of the "find references" feature.