Page MenuHomeDevCentral

Implement any() and all() for all collections
ClosedPublic

Authored by dereckson on Jul 2 2023, 18:07.
Tags
None
Referenced Files
F4075793: D3221.id8254.diff
Thu, Jan 30, 01:22
Unknown Object (File)
Mon, Jan 27, 04:12
Unknown Object (File)
Wed, Jan 22, 23:00
Unknown Object (File)
Wed, Jan 22, 22:44
Unknown Object (File)
Mon, Jan 20, 13:25
Unknown Object (File)
Mon, Jan 20, 07:40
Unknown Object (File)
Tue, Jan 14, 14:18
Unknown Object (File)
Mon, Jan 13, 14:54
Subscribers
None

Details

Summary

The any() and all() methods allow to determine if elements of the collection
pass a boolean test:

  • any($fn) will return true if at least one element satisfies $fn
  • all($fn) will return true if all elements of the collection satisfy $fn

Those was considered as functions for inclusion in PHP language, but rejected.

The $fn callback parameter must be a callable with one or two arguments,
fn ($value) or fn ($key, $value) and return a boolean.

Any other values than true will be falsy. That allows to cope with legacy
PHP functions returning true when the operation succeeds and an error code
if not, like Memcached::deleteMulti().

The execution of the callbacks stop when the result is definitive and can't
be affected by further values:

  • for any($fn), execution stops after the first callback returning true
  • for all($fn), execution stops after the first callback returning false

References:

Test Plan

Unit tests provided.

Actual use in the new keruald/cache library

Diff Detail

Repository
rKERUALD Keruald libraries development repository
Lint
Lint Warnings
SeverityLocationCodeMessage
Warningomnitools/src/Collections/WithCollection.php:35PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Unit
Tests Passed
Branch
hof-any-all
Build Status
Buildable 5078
Build 5359: arc lint + arc unit