Skip to content
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

[5.4] Always return a collection when calling Collection::random with a parameter #16865

Merged
merged 1 commit into from
Dec 20, 2016
Merged

[5.4] Always return a collection when calling Collection::random with a parameter #16865

merged 1 commit into from
Dec 20, 2016

Conversation

sebastiandedeyne
Copy link
Contributor

See laravel/ideas#304

Currently, if you call $collection->random(1), you'll get a single item back. This makes sense if there isn't a parameter set, but if 1 is explicitly passed through, it should return a new collection instance. This keeps it consistent with passing in any other integer.

@GrahamCampbell
Copy link
Member

👎 from me. Very confusing behaviour to have passing null behaving differently to if you don't pass anything, even though the default for arg 1 is null.

@sebastiandedeyne
Copy link
Contributor Author

Hmm that's actually a mistake in my PR, not in the underlying idea. If the default parameter would be null instead of 1, that wouldn't be an issue, right? Then ->random(null) would behave the same as ->random().

@GrahamCampbell
Copy link
Member

I don't like the premise of the idea entirely anyway.

@sebastiandedeyne
Copy link
Contributor Author

Doesn't random() behaving differently than random(int) make more sense that random(int = 1) behaving differently than random(int = >1)?

@GrahamCampbell
Copy link
Member

Doesn't random() behaving differently than random(int) make more sense that random(int = 1) behaving differently than random(int = >1)?

No, because PHP doesn't have a concept of multiple function definitions like Java.

@GrahamCampbell
Copy link
Member

I think the current behaviour is fine, though if we are going to change it, we should just break it to always return a collection, instead of the confusing mess of maybe returning one.

@sebastiandedeyne sebastiandedeyne deleted the collection-random-method-improvements branch December 19, 2016 16:25
@sebastiandedeyne sebastiandedeyne restored the collection-random-method-improvements branch December 19, 2016 16:26
@taylorotwell
Copy link
Member

I think this makes fairly good sense... if I'm passing an argument to that method, that argument could be a variable. If it's a variable it could be 1, in which in my own code I would be forced to have an if statement to know whether I have a collection or a single item. With this PR I can know for sure which one I will have just by asking if I am passing an argument to the method.

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.

3 participants