Dispatcher::isClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPKitchen\CodeSpecs\Expectation\Routing;
4
5
use PHPKitchen\CodeSpecs\Contract\ExpectationMatcher;
6
use PHPKitchen\CodeSpecs\Expectation\Internal\Assert;
7
use PHPKitchen\CodeSpecs\Expectation\Internal\StepsList;
8
use PHPKitchen\CodeSpecs\Expectation\Matcher\ArrayMatcher;
9
use PHPKitchen\CodeSpecs\Expectation\Matcher\BooleanMatcher;
10
use PHPKitchen\CodeSpecs\Expectation\Matcher\ClassMatcher;
11
use PHPKitchen\CodeSpecs\Expectation\Matcher\DirectoryMatcher;
12
use PHPKitchen\CodeSpecs\Expectation\Matcher\FileMatcher;
13
use PHPKitchen\CodeSpecs\Expectation\Matcher\NumberMatcher;
14
use PHPKitchen\CodeSpecs\Expectation\Matcher\ObjectMatcher;
15
use PHPKitchen\CodeSpecs\Expectation\Matcher\StringMatcher;
16
use PHPKitchen\CodeSpecs\Expectation\Matcher\ValueMatcher;
17
18
/**
19
 * Represents matchers dispatcher.
20
 *
21
 * Required to dispatch asserts to specific matchers.
22
 *
23
 * @package PHPKitchen\CodeSpecs\Expectation
24
 * @author Dima Kolodko <[email protected]>
25
 */
26
class Dispatcher {
27
    /**
28
     * @var mixed actual value or variable that will be matched to expectations.
29
     */
30
    protected $actual;
31
    /**
32
     * @var string description of expectation. If expectation fails this description will be displayed in console.
33
     */
34
    protected $variableName;
35
    protected $deferAsserts = false;
36
37
    public function __construct($actual, $variableName = '', $deferAsserts) {
38
        $this->actual = $actual;
39
        $this->variableName = $variableName;
40
        $this->deferAsserts = $deferAsserts;
41
        $this->init();
42
    }
43
44
    /**
45
     * Override this method if you want to initialize anything after constructor.
46
     */
47
    protected function init(): void {
48
    }
49
50
    public function isMixed(): ValueMatcher {
51
        return $this->createMatcher(ValueMatcher::class, 'value');
52
    }
53
54
    public function isString(): StringMatcher {
55
        return $this->createMatcher(StringMatcher::class, 'boolean');
56
    }
57
58
    public function isArray(): ArrayMatcher {
59
        return $this->createMatcher(ArrayMatcher::class, 'array');
60
    }
61
62
    public function isBoolean(): BooleanMatcher {
63
        return $this->createMatcher(BooleanMatcher::class, 'boolean');
64
    }
65
66
    public function isNumber(): NumberMatcher {
67
        return $this->createMatcher(NumberMatcher::class, 'number');
68
    }
69
70
    public function isObject(): ObjectMatcher {
71
        return $this->createMatcher(ObjectMatcher::class, 'object');
72
    }
73
74
    public function isClass(): ClassMatcher {
75
        return $this->createMatcher(ClassMatcher::class, 'class');
76
    }
77
78
    public function isFile(): FileMatcher {
79
        return $this->createMatcher(FileMatcher::class, 'file');
80
    }
81
82
    public function isDirectory(): DirectoryMatcher {
83
        return $this->createMatcher(DirectoryMatcher::class, 'directory');
84
    }
85
86
    protected function createMatcher($className, $id): ExpectationMatcher {
87
        $variableName = $this->variableName ?: $id;
88
89
        $stepsList = StepsList::getInstance();
90
        $assert = new Assert($stepsList, $this->actual, "I see that {$variableName}");
91
92
        if ($this->deferAsserts) {
93
            $assert->switchToDelayedExecutionStrategy();
94
        }
95
96
        return new $className($assert);
97
    }
98
}