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

Lazy collection and collections update #610

Open
wants to merge 18 commits into
base: 1.x
Choose a base branch
from
Open
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Add Lazy Collection and bring Collection/Enumerable Values trait up-to-date with Laravel.

## Unreleased

📢 Minimum PHP version is now 8.2. The framework supports 8.2 - 8.4.

### Added

- ✨ Experimental feature ✨: Use the home URL as the base URL for testing rather
than `WP_TESTS_DOMAIN`. This can be enabled by calling the
`with_experimental_testing_url_host()` method of the installation manager or
by setting the `MANTLE_EXPERIMENTAL_TESTING_USE_HOME_URL_HOST` environment
variable.

Once enabled, the home URL will be used as the base URL for testing rather
the hard-coded `WP_TESTS_DOMAIN`. It will also infer the HTTPS status from
the home URL.
- Added `with_option()`/`with_home_url()`/`with_site_url()` methods to the installation manager.
- Add a `without_local_object_cache()` method to prevent the `object-cache.php` drop-in from being loaded locally.
- Added a better `dump()` method to the response object when testing HTTP
requests that will dump the request/response to the console.
Expand Down Expand Up @@ -42,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Allow `Filter`/`Action` attributes to be used multiple times on the same method.
>>>>>>> @{-1}

## v1.3.2 - 2024-12-17

Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<exclude name="Squiz.Commenting.VariableComment.MissingVar" />
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.Found" />
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.Found" />
<exclude name="WordPress.PHP.YodaConditions.NotYoda" />
<exclude name="WordPress.WP.AlternativeFunctions.json_encode_json_encode" />
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax" />
Expand Down
691 changes: 691 additions & 0 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

77 changes: 68 additions & 9 deletions src/mantle/support/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
* Collection
*
* @template TKey of array-key
* @template TValue
*
* @template-covariant TValue
*
* @implements \ArrayAccess<TKey, TValue>
* @implements \Mantle\Support\Enumerable<TKey, TValue>
*/
class Collection implements ArrayAccess, Enumerable {

Check failure on line 33 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in invariant position in implemented type ArrayAccess<TKey of (int|string), TValue> of class Mantle\Support\Collection.

Check failure on line 33 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in invariant position in implemented type Mantle\Support\Enumerable<TKey of (int|string), TValue> of class Mantle\Support\Collection.
/**
* The enumerated values trait.
*
Expand All @@ -42,7 +43,7 @@
*
* @var array<TKey, TValue>
*/
protected $items = [];

Check failure on line 46 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in invariant position in property Mantle\Support\Collection::$items.

/**
* Create a new collection.
Expand All @@ -65,7 +66,7 @@
* @param iterable<TKeyFrom, TValueFrom>|\WP_Query $value
* @return static<TKeyFrom, TValueFrom>
*/
public static function from( $value ) {
public static function from( mixed $value ) {
global $post;
if ( $value instanceof \WP_Query ) {
$items = [];
Expand Down Expand Up @@ -109,6 +110,15 @@
return $this->items;
}

/**
* Get a lazy collection for the items in this collection.
*
* @return Lazy_Collection<TKey, TValue>
*/
public function lazy(): Lazy_Collection {
return new Lazy_Collection( $this->items );
}

/**
* Get the average value of a given key.
*
Expand Down Expand Up @@ -213,7 +223,7 @@
* @param mixed $value
* @return bool
*/
public function contains( $key, $operator = null, $value = null ) {

Check failure on line 226 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in contravariant position in parameter key of method Mantle\Support\Collection::contains().
if ( func_num_args() === 1 ) {
if ( $this->use_as_callable( $key ) ) {
$placeholder = new stdClass();
Expand All @@ -221,7 +231,7 @@
return $this->first( $key, $placeholder ) !== $placeholder;
}

return in_array( $key, $this->items ); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
return in_array( $key, $this->items, false ); // phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse
}

return $this->contains( $this->operator_for_where( ...func_get_args() ) );
Expand All @@ -234,7 +244,7 @@
* @param TValue|null $value
* @return bool
*/
public function contains_strict( $key, $value = null ) {

Check failure on line 247 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in contravariant position in parameter key of method Mantle\Support\Collection::contains_strict().

Check failure on line 247 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in contravariant position in parameter value of method Mantle\Support\Collection::contains_strict().
if ( func_num_args() === 2 ) {
return $this->contains( fn ( $item ) => data_get( $item, $key ) === $value );
}
Expand All @@ -243,7 +253,7 @@
return ! is_null( $this->first( $key ) );
}

return in_array( $key, $this->items, true );
return in_array( $key, $this->items, true );
}

/**
Expand All @@ -255,7 +265,7 @@
* @return bool
*/
public function doesnt_contain( $key, $operator = null, $value = null ) {
return ! $this->contains( ...func_get_args() );
return ! $this->contains( ...func_get_args() );
}

/**
Expand All @@ -282,7 +292,7 @@
* @param \Mantle\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TValue> $items
* @return static
*/
public function diff( $items ) {

Check failure on line 295 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in contravariant position in parameter items of method Mantle\Support\Collection::diff().

Check failure on line 295 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in invariant position in parameter items of method Mantle\Support\Collection::diff().
return new static( array_diff( $this->items, $this->get_arrayable_items( $items ) ) );
}

Expand All @@ -293,7 +303,7 @@
* @param callable(TValue, TValue): int $callback
* @return static
*/
public function diff_using( $items, callable $callback ) {

Check failure on line 306 in src/mantle/support/class-collection.php

View workflow job for this annotation

GitHub Actions / lint / lint PHP 8.4

Template type TValue is declared as covariant, but occurs in contravariant position in parameter items of method Mantle\Support\Collection::diff_using().
return new static( array_udiff( $this->items, $this->get_arrayable_items( $items ), $callback ) );
}

Expand Down Expand Up @@ -522,7 +532,7 @@
$result = new static( $results );

if ( ! empty( $next_groups ) ) {
return $result->map->groupBy( $next_groups, $preserve_keys ); // @phpstan-ignore-line undefined method
return $result->map->group_by( $next_groups, $preserve_keys );
}

return $result;
Expand Down Expand Up @@ -1104,6 +1114,26 @@
return $this->slice( $count );
}

/**
* Skip items in the collection until the given condition is met.
*
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function skip_until( $value ) {
return new static( $this->lazy()->skip_until( $value )->all() );
}

/**
* Skip items in the collection while the given condition is met.
*
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function skip_while( $value ) {
return new static( $this->lazy()->skip_while( $value )->all() );
}

/**
* Slice the underlying collection array.
*
Expand Down Expand Up @@ -1289,7 +1319,7 @@
/**
* Take the first or last {$limit} items.
*
* @param int $limit
* @param int $limit
* @return static
*/
public function take( $limit ) {
Expand All @@ -1300,13 +1330,32 @@
return $this->slice( 0, $limit );
}

/**
* Take items in the collection until the given condition is met.
*
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function take_until( $value ) {
return new static( $this->lazy()->take_until( $value )->all() );
}

/**
* Take items in the collection while the given condition is met.
*
* @param TValue|callable(TValue,TKey): bool $value
* @return static
*/
public function take_while( $value ) {
return new static( $this->lazy()->take_while( $value )->all() );
}

/**
* Transform each item in the collection using a callback.
*
* @param callable(TValue, TKey): TValue $callback
* @return $this
*/
public function transform( callable $callback ) {
public function transform( callable $callback ): static {
$this->items = $this->map( $callback )->all();

return $this;
Expand Down Expand Up @@ -1388,6 +1437,16 @@
return count( $this->items );
}

/**
* Count the number of items in the collection by a field or using a callback.
*
* @param (callable(TValue, TKey): array-key)|string|null $count_by
* @return static<array-key, int>
*/
public function count_by( $count_by = null ) {
return new static( $this->lazy()->count_by( $count_by )->all() );
}

/**
* Add an item to the collection.
*
Expand Down
Loading
Loading