Rename current AbstractCollection::pluck() to extract() since it keeps array keys, and make pluck() return only values - #728
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR renames the existing pluck() method to extract() to preserve array keys when pulling values, and reintroduces pluck() as a values-only wrapper. Key changes:
- Renamed
Arr::pluck()toArr::extract()and updated its docblock to mention key preservation. - Updated all internal calls from
pluck()toextract(). - In
AbstractCollection, added a newpluck()alias that reindexes values, and updated related methods (filterBy,sortBy,groupBy) to useextract().
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| formwork/src/Utils/Arr.php | Renamed pluck() to extract(), updated doc comment |
| formwork/src/Panel/Controllers/FilesController.php | Switched fields()->pluck('default') to fields()->extract('default') |
| formwork/src/Pages/PageCollection.php | Renamed pluck() method to extract() |
| formwork/src/Pages/Page.php | Updated fields()->pluck('default') to fields()->extract('default') |
| formwork/src/Fields/FieldCollection.php | Renamed pluck() method to extract() |
| formwork/src/Data/AbstractCollection.php | Renamed pluck() → extract(), added new pluck() wrapper, and replaced internal pluck() calls with extract() |
Comments suppressed due to low confidence (3)
formwork/src/Utils/Arr.php:553
- Add unit tests for
Arr::extractto verify that it preserves the original array keys and behaves as expected when the default value is used.
}
formwork/src/Data/AbstractCollection.php:390
- Include tests for the new
extractmethod inAbstractCollectionto ensure it delegates correctly toArr::extractand preserves keys for various collection types.
public function extract(string $key, mixed $default = null): array
formwork/src/Data/AbstractCollection.php:403
- Add unit tests for the newly introduced
pluckwrapper inAbstractCollectionto confirm it returns a zero-based indexed array of values (i.e.,array_valuesofextract).
public function pluck(string $key, mixed $default = null): array
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request refactors the
AbstractCollection::pluck()method replacing it with a newextract()method to improve clarity and maintainability.In fact in many other projects, like notably Laravel,
pluck()extracts only the values from the collection items, without preserving the keys, unlike the former Formwork equivalent of the method.By doing this we ensure the collection API is more predictable.
pluck()is still kept inAbstractCollectionto provide a variant ofextract()that does not preserve keys.The changes impact several classes and methods, ensuring consistency across the codebase.
Note:
Arr::pluck()has been renamed toArr::extract().