Test Failed
Push — master ( 397588...614ce5 )
by Hannes
02:13
created

Evaluator::evaluate()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 14
nop 2
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
use hanneskod\readmetester\Expectation\ExpectationInterface;
8
use hanneskod\readmetester\Expectation\Status;
9
use hanneskod\readmetester\Expectation\Failure;
10
use hanneskod\readmetester\Runner\OutcomeInterface;
11
12
/**
13
 * Evaluate expectations against a set of outcomes
14
 */
15
class Evaluator
16
{
17
    /**
18
     * Evaluate expectations against a set of outcomes
19
     *
20
     * All outcomes must be handled by at least one expectation. Expectations
21
     * that does not handle an outcome defaults to failure.
22
     *
23
     * @param  ExpectationInterface[] $expectations
24
     * @param  OutcomeInterface[]     $outcomes
25
     * @return Status[]
26
     */
27
    public function evaluate(array $expectations, array $outcomes): array
28
    {
29
        $statuses = [];
30
31
        foreach ($expectations as $index => $expectation) {
32
            $statuses[$index] = new Failure("Failed $expectation");
33
        }
34
35
        foreach ($outcomes as $outcome) {
36
            $isHandled = false;
37
38
            foreach ($expectations as $index => $expectation) {
39
                if ($expectation->handles($outcome)) {
40
                    $statuses[$index] = $expectation->handle($outcome);
41
                    $isHandled = true;
42
                }
43
            }
44
45
            if (!$isHandled) {
46
                $statuses[] = new Failure("Unhandled $outcome");
47
            }
48
        }
49
50
        return $statuses;
51
    }
52
}
53