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
|
|
|
|
9
|
|
|
class StateInspector implements StateInspectorInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var InspectionInterface[] |
13
|
|
|
*/ |
14
|
|
|
private $inspections = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var IssueInterface[] |
18
|
|
|
*/ |
19
|
|
|
private $issues = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* StateInspectorManager constructor. |
23
|
|
|
* @param InspectorInterface[] $inspections |
24
|
|
|
*/ |
25
|
6 |
|
public function __construct(array $inspections = []) |
26
|
|
|
{ |
27
|
6 |
|
foreach ($inspections as $name => $inspection) { |
28
|
1 |
|
if(is_numeric($name)){ |
29
|
1 |
|
$name = null; |
30
|
|
|
} |
31
|
1 |
|
$this->addInspection($inspection, $name); |
32
|
|
|
} |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $object |
37
|
|
|
* @param bool $bubble |
38
|
|
|
*/ |
39
|
5 |
|
final public function inspect($object, $bubble = false) |
40
|
|
|
{ |
41
|
5 |
|
$this->issues = []; //reset |
42
|
5 |
|
foreach ($this->inspections as $name => $inspection) { |
43
|
4 |
|
$issues = $this->inspection($name, $object, $bubble); |
44
|
3 |
|
$this->issues = array_merge($this->issues, $issues); |
|
|
|
|
45
|
|
|
} |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $name |
50
|
|
|
* @param mixed $object |
51
|
|
|
* @param bool $bubble |
52
|
|
|
* @return InspectionInterface[] |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
5 |
|
final public function inspection(string $name, $object, $bubble = false): array |
56
|
|
|
{ |
57
|
5 |
|
if (isset($this->inspections[$name]) === false) { |
58
|
|
|
$msg = $name.' no such inspection!'; |
59
|
|
|
throw new StateInspectorException( |
60
|
|
|
new Issue($msg, $name.' is no inspection.', 'Please create new Inspection or use other name.'), |
61
|
|
|
$msg |
62
|
|
|
); |
63
|
|
|
} |
64
|
5 |
|
$inspection = $this->inspections[$name]; |
65
|
|
|
|
66
|
5 |
|
if ($inspection->supports($object) === false) { |
67
|
|
|
$msg = $name.' inspection doesnt support object type!'; |
68
|
|
|
throw new StateInspectorException( |
69
|
|
|
new Issue($msg, get_class($object).' is not supported.', 'Please create new Inspection for this type.'), |
70
|
|
|
$msg |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
5 |
|
$inspection->inspect($object) |
76
|
3 |
|
? $inspection->success() |
77
|
5 |
|
: $inspection->failure(); |
78
|
2 |
|
} catch (\Exception $e) { |
79
|
2 |
|
if ($bubble) { |
80
|
1 |
|
throw $e; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return $inspection->getIssues(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return IssueInterface[] |
89
|
|
|
*/ |
90
|
5 |
|
final public function getIssues(): array |
91
|
|
|
{ |
92
|
5 |
|
return $this->issues; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param InspectionInterface $inspection |
97
|
|
|
* @param string|null $name |
98
|
|
|
* @return StateInspectorInterface |
99
|
|
|
*/ |
100
|
5 |
|
final public function addInspection(InspectionInterface $inspection, string $name = null): StateInspectorInterface |
101
|
|
|
{ |
102
|
5 |
|
if(null === $name){ |
103
|
5 |
|
$name = get_class($inspection); |
104
|
|
|
} |
105
|
5 |
|
$this->inspections[$name] = $inspection; |
106
|
|
|
|
107
|
5 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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..