StateInspector   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 101
ccs 30
cts 38
cp 0.7895
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A inspect() 0 8 2
B inspection() 0 31 6
A getIssues() 0 4 1
A addInspection() 0 9 2
1
<?php
2
3
namespace Freshcells\StateInspector;
4
5
use Freshcells\StateInspector\Exception\StateInspectorException;
6
use Freshcells\StateInspector\Inspection\InspectionInterface;
7
use Freshcells\StateInspector\Issue\Issue;
8
use Freshcells\StateInspector\Issue\IssueInterface;
9
10
class StateInspector implements StateInspectorInterface
11
{
12
    /**
13
     * @var InspectionInterface[]
14
     */
15
    private $inspections = [];
16
17
    /**
18
     * @var IssueInterface[]
19
     */
20
    private $issues = [];
21
22
    /**
23
     * StateInspectorManager constructor.
24
     * @param InspectionInterface[] $inspections
25
     */
26 6
    public function __construct(array $inspections = [])
27
    {
28 6
        foreach ($inspections as $name => $inspection) {
29 1
            if (is_numeric($name)) {
30 1
                $name = null;
31
            }
32 1
            $this->addInspection($inspection, $name);
33
        }
34 6
    }
35
36
    /**
37
     * @param mixed $object
38
     * @param bool $bubble
39
     */
40 5
    final public function inspect($object, $bubble = false)
41
    {
42 5
        $this->issues = []; //reset
43 5
        foreach ($this->inspections as $name => $inspection) {
44 4
            $issues       = $this->inspection($name, $object, $bubble);
45 3
            $this->issues = array_merge($this->issues, $issues);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->issues, $issues) of type array is incompatible with the declared type array<integer,object<Fre...\Issue\IssueInterface>> of property $issues.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        }
47 4
    }
48
49
    /**
50
     * @param string $name
51
     * @param mixed $object
52
     * @param bool $bubble
53
     * @return IssueInterface[]
54
     * @throws \Exception
55
     */
56 5
    final public function inspection(string $name, $object, $bubble = false): array
57
    {
58 5
        if (isset($this->inspections[$name]) === false) {
59
            $msg = $name.' no such inspection!';
60
            throw new StateInspectorException(
61
                new Issue($msg, $name.' is no inspection.', 'Please create new Inspection or use other name.'),
62
                $msg
63
            );
64
        }
65 5
        $inspection = $this->inspections[$name];
66
67 5
        if ($inspection->supports($object) === false) {
68
            $msg = $name.' inspection does not support object type!';
69
            throw new StateInspectorException(
70
                new Issue($msg, get_class($object).' is not supported.', 'Please create new Inspection for this type.'),
71
                $msg
72
            );
73
        }
74
75
        try {
76 5
            $inspection->inspect($object)
77 3
                ? $inspection->success()
78 5
                : $inspection->failure();
79 2
        } catch (\Exception $e) {
80 2
            if ($bubble) {
81 1
                throw $e;
82
            }
83
        }
84
85 4
        return $inspection->getIssues();
86
    }
87
88
    /**
89
     * @return IssueInterface[]
90
     */
91 5
    final public function getIssues(): array
92
    {
93 5
        return $this->issues;
94
    }
95
96
    /**
97
     * @param InspectionInterface $inspection
98
     * @param string|null $name
99
     * @return StateInspectorInterface
100
     */
101 5
    final public function addInspection(InspectionInterface $inspection, string $name = null): StateInspectorInterface
102
    {
103 5
        if (null === $name) {
104 5
            $name = get_class($inspection);
105
        }
106 5
        $this->inspections[$name] = $inspection;
107
108 5
        return $this;
109
    }
110
}
111