Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TraitCollectionFunctional
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 map
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reduce
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 each
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Domain\Collection\Trait;
11
12trait TraitCollectionFunctional
13{
14    public function map(callable $fn): static
15    {
16        return static::fromArray(array_map($fn, $this->collection));
17    }
18
19    public function filter(callable $fn): static
20    {
21        return static::fromArray(array_filter($this->collection, $fn, ARRAY_FILTER_USE_BOTH));
22    }
23
24    public function reduce(callable $fn, mixed $initial): mixed
25    {
26        return array_reduce($this->collection, $fn, $initial);
27    }
28
29    public function each(callable $fn): void
30    {
31        array_walk($this->collection, $fn);
32    }
33}