Skip to content

Can sort locations - #7

Merged
dantleech merged 6 commits into
phpactor:masterfrom
camilledejoye:can-sort-locations
Oct 18, 2020
Merged

dantleech merged 6 commits into
phpactor:masterfrom
camilledejoye:can-sort-locations

Conversation

@camilledejoye

Copy link
Copy Markdown
Contributor

The idea was to be able to sort locations directly from the list object.
From there I also replaced array by iterable in 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.

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.
Comment thread lib/Locations.php Outdated
/**
* @param iterable<Location> $locations
*/
public static function bySorting(iterable $locations, ?callable $sortStrategy = null): self

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread lib/Locations.php Outdated
public static function bySorting(iterable $locations): self
{
return (new self($locations))->sorted();
}

@dantleech dantleech Oct 18, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/Locations.php Outdated
return $first->offset()->toInt() - $second->offset()->toInt();
});

return $this;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this needs to return a new instance. the locations should be immuatble.

$locations = $this->locations;
usort(...);
return new self($locations);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My bad, just saw the append() method but didn't check the implementation.
I'm used to withXXX() method for immutable objects ><

@dantleech
dantleech merged commit fa2e1f7 into phpactor:master Oct 18, 2020
@dantleech

Copy link
Copy Markdown
Contributor

Thanks, a static constructor would make it slightly less ugly in this particular use case, but equally also happy not to add one yet.

@camilledejoye
camilledejoye deleted the can-sort-locations branch October 18, 2020 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants