Skip to content

Rename current AbstractCollection::pluck() to extract() since it keeps array keys, and make pluck() return only values - #728

Merged
giuscris merged 1 commit into
2.xfrom
rename-pluck-to-extract
Jul 12, 2025
Merged

Rename current AbstractCollection::pluck() to extract() since it keeps array keys, and make pluck() return only values#728
giuscris merged 1 commit into
2.xfrom
rename-pluck-to-extract

Conversation

@giuscris

Copy link
Copy Markdown
Member

This pull request refactors the AbstractCollection::pluck() method replacing it with a new extract() 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 in AbstractCollection to provide a variant of extract() that does not preserve keys.

The changes impact several classes and methods, ensuring consistency across the codebase.

Note: Arr::pluck() has been renamed to Arr::extract().

@giuscris giuscris added this to the 2.0.0 milestone Jul 12, 2025
@giuscris
giuscris requested a review from Copilot July 12, 2025 10:15
@giuscris giuscris self-assigned this Jul 12, 2025

Copilot AI left a comment

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.

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() to Arr::extract() and updated its docblock to mention key preservation.
  • Updated all internal calls from pluck() to extract().
  • In AbstractCollection, added a new pluck() alias that reindexes values, and updated related methods (filterBy, sortBy, groupBy) to use extract().

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::extract to 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 extract method in AbstractCollection to ensure it delegates correctly to Arr::extract and 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 pluck wrapper in AbstractCollection to confirm it returns a zero-based indexed array of values (i.e., array_values of extract).
    public function pluck(string $key, mixed $default = null): array

@getformwork getformwork deleted a comment from Copilot AI Jul 12, 2025
@giuscris
giuscris merged commit e0c4731 into 2.x Jul 12, 2025
1 check passed
@giuscris
giuscris deleted the rename-pluck-to-extract branch July 12, 2025 10:23
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