Completed
Push — master ( b6ead1...d7f0f7 )
by Jesse
03:42
created

Filtering   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A that() 0 4 1
A filterWith() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use function array_filter;
8
use Closure;
9
use Stratadox\Collection\Collection;
10
use Stratadox\Collection\Filterable;
11
use Stratadox\Specification\Contract\Satisfiable;
12
13
/**
14
 * Behaviour to allow "filtering" the immutable collection.
15
 *
16
 * Provides access to filtering behaviour in the form of a method that
17
 * returns a modified copy of the original (immutable) collection.
18
 *
19
 * @package Stratadox\Collection
20
 * @author  Stratadox
21
 */
22
trait Filtering
23
{
24
    /**
25
     * @see Filterable::that
26
     * @param Satisfiable $condition
27
     * @return Filterable|static
28
     */
29
    public function that(Satisfiable $condition): Filterable
30
    {
31
        return $this->newCopy(array_filter(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->newCopy(ar...ion, 'isSatisfiedBy'))) returns the type Stratadox\Collection\Collection which includes types incompatible with the type-hinted return Stratadox\Collection\Filterable.
Loading history...
32
            $this->items(), [$condition, 'isSatisfiedBy']
33
        ));
34
    }
35
36
    /**
37
     * @see Filterable::filterWith
38
     * @param Closure $function
39
     * @return Filterable|static
40
     */
41
    public function filterWith(Closure $function): Filterable
42
    {
43
        return $this->newCopy(array_filter(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->newCopy(ar...s->items(), $function)) returns the type Stratadox\Collection\Collection which includes types incompatible with the type-hinted return Stratadox\Collection\Filterable.
Loading history...
44
            $this->items(), $function
45
        ));
46
    }
47
48
    /** @see Collection::items() */
49
    abstract public function items(): array;
50
51
    /**
52
     * @see ImmutableCollection::newCopy()
53
     * @param array $items
54
     * @return Collection|static
55
     */
56
    abstract protected function newCopy(array $items): Collection;
57
}
58