|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace DependencyAnalyzer\Inspector\RuleViolationDetector; |
|
5
|
|
|
|
|
6
|
|
|
use DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName; |
|
7
|
|
|
use DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName\Base as FQSEN; |
|
8
|
|
|
use DependencyAnalyzer\DependencyGraph\StructuralElementPatternMatcher; |
|
9
|
|
|
use DependencyAnalyzer\Exceptions\InvalidFullyQualifiedStructureElementNameException; |
|
10
|
|
|
use DependencyAnalyzer\Exceptions\InvalidQualifiedNamePatternException; |
|
11
|
|
|
|
|
12
|
|
|
class Component |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var StructuralElementPatternMatcher |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $matcher; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var StructuralElementPatternMatcher |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $publicMatcher; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var StructuralElementPatternMatcher |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $dependerMatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var StructuralElementPatternMatcher |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $dependeeMatcher; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $attributes = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var StructuralElementPatternMatcher[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $extraPatterns = []; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
string $name, |
|
51
|
|
|
StructuralElementPatternMatcher $pattern, |
|
52
|
|
|
StructuralElementPatternMatcher $dependerPatterns = null, |
|
53
|
|
|
StructuralElementPatternMatcher $dependeePatterns = null, |
|
54
|
|
|
StructuralElementPatternMatcher $publicPattern = null, |
|
55
|
|
|
array $extraPatterns = null |
|
56
|
|
|
) { |
|
57
|
|
|
$this->name = $name; |
|
58
|
|
|
$this->matcher = $pattern; |
|
59
|
|
|
$this->dependerMatcher = $dependerPatterns; |
|
60
|
|
|
$this->dependeeMatcher = $dependeePatterns; |
|
61
|
|
|
$this->publicMatcher = $publicPattern; |
|
62
|
|
|
|
|
63
|
|
|
if (!is_null($extraPatterns)) { |
|
64
|
|
|
try { |
|
65
|
|
|
foreach ($extraPatterns as $callee => $callerPattern) { |
|
66
|
|
|
FullyQualifiedStructuralElementName::createFromString($callee); |
|
67
|
|
|
} |
|
68
|
|
|
} catch (InvalidFullyQualifiedStructureElementNameException $e) { |
|
69
|
|
|
throw new InvalidQualifiedNamePatternException($callee); |
|
70
|
|
|
} |
|
71
|
|
|
$this->extraPatterns = $extraPatterns; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getName(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->name; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getDefineMatcher(): StructuralElementPatternMatcher |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->matcher; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function isBelongedTo(string $className): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->matcher->isMatch($className); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function verifyDepender(FQSEN $depender, FQSEN $dependee): bool |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->isBelongedTo($depender->toString())) { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!is_null($extraPattern = $this->getExtraPattern($dependee))) { |
|
97
|
|
|
return $extraPattern->isMatchWithFQSEN($depender); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return ( |
|
101
|
|
|
(is_null($this->publicMatcher) ? true : $this->publicMatcher->isMatchWithFQSEN($dependee)) && |
|
102
|
|
|
(is_null($this->dependerMatcher) ? true : $this->dependerMatcher->isMatchWithFQSEN($depender)) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function verifyDependee(string $className): bool |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->isBelongedTo($className)) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} elseif (is_null($this->dependeeMatcher)) { |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->dependeeMatcher->isMatch($className); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $className |
|
119
|
|
|
* @param StructuralElementPatternMatcher[] $patterns |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function checkPatterns(string $className, array $patterns): bool |
|
123
|
|
|
{ |
|
124
|
|
|
foreach ($patterns as $pattern) { |
|
125
|
|
|
if ($pattern->isMatch($className)) { |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function getExtraPattern(FQSEN $dependee): ?StructuralElementPatternMatcher |
|
134
|
|
|
{ |
|
135
|
|
|
foreach ($this->extraPatterns as $callee => $callerPattern) { |
|
136
|
|
|
$calleeFQSEN = FullyQualifiedStructuralElementName::createFromString($callee); |
|
137
|
|
|
if ($calleeFQSEN->include($dependee)) { |
|
138
|
|
|
return $this->extraPatterns[$callee]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setAttribute(string $key, $name): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->attributes[$key] = $name; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function getAttribute(string $key) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->attributes[$key] ?? null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function toArray() |
|
156
|
|
|
{ |
|
157
|
|
|
$ret = [ |
|
158
|
|
|
'define' => $this->matcher->toArray() |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
if (!is_null($this->dependerMatcher)) { |
|
162
|
|
|
$ret['depender'] = $this->dependerMatcher->toArray(); |
|
163
|
|
|
} |
|
164
|
|
|
if (!is_null($this->dependeeMatcher)) { |
|
165
|
|
|
$ret['dependee'] = $this->dependeeMatcher->toArray(); |
|
166
|
|
|
} |
|
167
|
|
|
if (!is_null($this->publicMatcher)) { |
|
168
|
|
|
$ret['public'] = $this->publicMatcher->toArray(); |
|
169
|
|
|
} |
|
170
|
|
|
if (!empty($this->extraPatterns)) { |
|
171
|
|
|
$ret['extra'] = []; |
|
172
|
|
|
foreach ($this->extraPatterns as $callee => $callerPattern) { |
|
173
|
|
|
$ret['extra'][$callee] = $callerPattern->toArray(); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $ret; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|