1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace DependencyAnalyzer\Inspector\RuleViolationDetector; |
5
|
|
|
|
6
|
|
|
use DependencyAnalyzer\Matcher\ClassNameMatcher; |
7
|
|
|
|
8
|
|
|
class Component |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $name; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ClassNameMatcher |
17
|
|
|
*/ |
18
|
|
|
protected $matcher; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ClassNameMatcher |
22
|
|
|
*/ |
23
|
|
|
protected $dependerMatcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ClassNameMatcher |
27
|
|
|
*/ |
28
|
|
|
protected $dependeeMathcer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Component constructor. |
32
|
|
|
* @param string $name |
33
|
|
|
* @param ClassNameMatcher $pattern |
34
|
|
|
* @param ClassNameMatcher $dependerPatterns |
35
|
|
|
* @param ClassNameMatcher $dependeePatterns |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $name, ClassNameMatcher $pattern, ClassNameMatcher $dependerPatterns = null, ClassNameMatcher $dependeePatterns = null) |
38
|
|
|
{ |
39
|
|
|
$this->name = $name; |
40
|
|
|
$this->matcher = $pattern; |
41
|
|
|
$this->dependerMatcher = $dependerPatterns; |
42
|
|
|
$this->dependeeMathcer = $dependeePatterns; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getName(): string |
46
|
|
|
{ |
47
|
|
|
return $this->name; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isBelongedTo(string $className): bool |
51
|
|
|
{ |
52
|
|
|
return $this->matcher->isMatch($className); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function verifyDepender(string $className): bool |
56
|
|
|
{ |
57
|
|
|
if ($this->isBelongedTo($className)) { |
58
|
|
|
return true; |
59
|
|
|
} elseif (is_null($this->dependerMatcher)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->dependerMatcher->isMatch($className); |
64
|
|
|
// return $this->checkPatterns($className, $this->dependerPatterns); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function verifyDependee(string $className): bool |
68
|
|
|
{ |
69
|
|
|
if ($this->isBelongedTo($className)) { |
70
|
|
|
return true; |
71
|
|
|
} elseif (is_null($this->dependeeMathcer)) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->dependeeMathcer->isMatch($className); |
76
|
|
|
// return $this->checkPatterns($className, $this->dependeePatterns); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $className |
81
|
|
|
* @param ClassNameMatcher[] $patterns |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function checkPatterns(string $className, array $patterns): bool |
85
|
|
|
{ |
86
|
|
|
foreach ($patterns as $pattern) { |
87
|
|
|
if ($pattern->isMatch($className)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function toArray() |
96
|
|
|
{ |
97
|
|
|
$ret = [ |
98
|
|
|
'define' => $this->matcher->toArray() |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if (!is_null($this->dependerMatcher)) { |
102
|
|
|
$ret['depender'] = $this->dependerMatcher->toArray(); |
103
|
|
|
} |
104
|
|
|
if (!is_null($this->dependeeMathcer)) { |
105
|
|
|
$ret['dependee'] = $this->dependeeMathcer->toArray(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $ret; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|