-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[8.x] Collection prevent null being added to resulting collection with pop and shift #38265
Closed
craig-sen
wants to merge
9
commits into
laravel:8.x
from
craig-sen:prevent_null_being_added_to_pop_and_shift
Closed
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
946a4a4
pop olny return collection items
craig-sen faa5076
add test - pop only return collection items
craig-sen a60645f
shift only return collection items
craig-sen 2c6b9fe
add test - shift only return collection items
craig-sen b9d9e23
if collection is empty or less than one return null
craig-sen f60ced8
add test return null when no items or less than zero
craig-sen 40ccf2b
whitespace
craig-sen c907736
return an empty collection when less than 1 or no items
craig-sen 29c6046
return collection when parameter supplied
craig-sen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should probably be a new empty collection instead of null to allow for chaining?
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.
I think you can just use
return;
, there are 2 more spots in this file related to the functionsmedian()
andmode()
which also have a null return. I'm not sure if returning a new collection is appropriate here when comparing it to the phparray_pop()
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.
as mentioned by @bert-w with php the functions
array_pop
andarray_shift
returningnull
in the case of an empty array, when the $count parameter is1
this implementation seems appropriate.However, in the case that there is a user supplied parameter which is not
1
things get a little bit more difficult...Considering the current behaviour of the function it may be better to apply something similar to the following logic
The more I think about it, it is probally more appropriate to seperate the funtionality into classic
pop
functionality returningobject | null
, andpopMultiple
which takes a parameter and returns a collection. (As this is already in production it would be a breaking change?)