Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TraitCollectionFunctional
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
5 / 5
5
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
 mapToArray
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 mapToArray(callable $fn): array
20    {
21        return array_map($fn, $this->collection);
22    }
23
24    public function filter(callable $fn): static
25    {
26        return static::fromArray(array_filter($this->collection, $fn, ARRAY_FILTER_USE_BOTH));
27    }
28
29    public function reduce(callable $fn, mixed $initial): mixed
30    {
31        return array_reduce($this->collection, $fn, $initial);
32    }
33
34    public function each(callable $fn): void
35    {
36        array_walk($this->collection, $fn);
37    }
38}