CompositeSpecification::remainderUnsatisfiedBy()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.0312
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Larium\Specification;
6
7
class CompositeSpecification extends LeafSpecification
8
{
9
    private $specifications = [];
10
11 3
    public function __construct(Specification ...$specifications)
12
    {
13 3
        $this->specifications = $specifications;
14 3
    }
15
16 3
    public function isSatisfiedBy(Candidate $candidate): bool
17
    {
18 3
        foreach ($this->specifications as $specification) {
19 3
            if ($specification->isSatisfiedBy($candidate) === false) {
20 3
                return false;
21
            }
22
        }
23
24 2
        return true;
25
    }
26
27 1
    public function remainderUnsatisfiedBy(Candidate $candidate): ?Specification
28
    {
29 1
        if ($this->isSatisfiedBy($candidate)) {
30
            return null;
31
        }
32
33 1
        $unsatisfied = [];
34 1
        foreach ($this->specifications as $specification) {
35 1
            if (!$specification->isSatisfiedBy($candidate)) {
36 1
                $unsatisfied[] = $specification;
37
            }
38
        }
39
40 1
        return new static(...$unsatisfied);
41
    }
42
}
43