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); |
|
|
|
|
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
|
|
|
|
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..