EvaluateContext::actual()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of expect package.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
namespace expect\context;
12
13
use expect\Context;
14
use expect\MatcherEvaluator;
15
use expect\MatcherFactory;
16
use expect\ResultReporter;
17
18
class EvaluateContext implements Context
19
{
20
    private $actual;
21
    private $negated;
22
    private $factory;
23
    private $reporter;
24
25
    public function __construct(MatcherFactory $factory, ResultReporter $reporter)
26
    {
27
        $this->actual = null;
28
        $this->negated = false;
29
        $this->factory = $factory;
30
        $this->reporter = $reporter;
31
    }
32
33
    public function actual($actual)
34
    {
35
        $this->actual = $actual;
36
37
        return $this;
38
    }
39
40
    public function not()
41
    {
42
        $this->negated = true;
43
44
        return $this;
45
    }
46
47
    public function evaluate($name, $arguments = [])
48
    {
49
        $matcher = $this->factory->create($name, $arguments);
50
51
        $evaluator = MatcherEvaluator::fromMatcher($matcher);
52
53
        if ($this->negated) {
54
            $evaluator->negated();
55
        }
56
57
        $result = $evaluator->evaluate($this->actual);
58
        $result->reportTo($this->reporter);
59
    }
60
61
    public function __call($name, $arguments = [])
62
    {
63
        if (method_exists($this, $name)) {
64
            return call_user_func_array([$this, $name], $arguments);
65
        } else {
66
            $this->evaluate($name, $arguments);
67
        }
68
    }
69
}
70