1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace DependencyAnalyzer\Inspector\RuleViolationDetector; |
5
|
|
|
|
6
|
|
|
use DependencyAnalyzer\DependencyGraph; |
7
|
|
|
use DependencyAnalyzer\DependencyGraph\FullyQualifiedStructuralElementName\Base as FQSEN; |
8
|
|
|
use DependencyAnalyzer\DependencyGraph\StructuralElementPatternMatcher; |
9
|
|
|
use DependencyAnalyzer\Inspector\Responses\VerifyDependencyResponse; |
10
|
|
|
use Fhaculty\Graph\Vertex; |
11
|
|
|
|
12
|
|
|
class DependencyRule |
13
|
|
|
{ |
14
|
|
|
// group namespaces |
15
|
|
|
// check dependency direction |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $definition; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Component[] |
24
|
|
|
*/ |
25
|
|
|
protected $components = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $ruleName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $ruleName |
34
|
|
|
* @param array $components |
35
|
|
|
* @da-internal \DependencyAnalyzer\Inspector\RuleViolationDetector\DependencyRuleFactory |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $ruleName, array $components) |
38
|
|
|
{ |
39
|
|
|
$this->ruleName = $ruleName; |
40
|
|
|
$this->components = $components; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getRuleName(): string |
44
|
|
|
{ |
45
|
|
|
return $this->ruleName; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isSatisfyBy(DependencyGraph $graph): VerifyDependencyResponse |
49
|
|
|
{ |
50
|
|
|
$response = new VerifyDependencyResponse($this->getRuleName()); |
51
|
|
|
|
52
|
|
|
foreach ($graph->getDependencyArrows() as $dependencyArrow) { |
53
|
|
|
foreach ($dependencyArrow->getDependencies() as $dependency) { |
54
|
|
|
/** @var DependencyGraph\FullyQualifiedStructuralElementName\Base $dependerFQSEN */ |
55
|
|
|
$dependerFQSEN = $dependency[0]; |
56
|
|
|
/** @var DependencyGraph\FullyQualifiedStructuralElementName\Base $dependeeFQSEN */ |
57
|
|
|
$dependeeFQSEN = $dependency[1]; |
58
|
|
|
// $depender = $dependencyArrow->getDependerClass(); |
59
|
|
|
// $dependee = $dependencyArrow->getDependeeClass(); |
60
|
|
|
|
61
|
|
|
// TODO: add exclude rule |
62
|
|
|
$dependerComponent = $this->getComponent($dependerFQSEN); |
63
|
|
|
$dependeeComponent = $this->getComponent($dependeeFQSEN); |
64
|
|
|
if (is_null($dependerComponent) || is_null($dependeeComponent) || $dependerComponent === $dependeeComponent) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($this->components as $component) { |
69
|
|
|
if ($component->isBelongedTo($dependeeFQSEN->toString())) { |
70
|
|
|
if (!$component->verifyDepender($dependerFQSEN, $dependeeFQSEN)) { |
71
|
|
|
$response->addRuleViolation( |
72
|
|
|
$dependerComponent->getName(), |
73
|
|
|
$dependerFQSEN->toString(), |
74
|
|
|
$dependeeComponent->getName(), |
75
|
|
|
$dependeeFQSEN->toString() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($component->isBelongedTo($dependerFQSEN->toString())) { |
81
|
|
|
if (!$component->verifyDependee($dependeeFQSEN->toString())) { |
82
|
|
|
$response->addRuleViolation( |
83
|
|
|
$dependerComponent->getName(), |
84
|
|
|
$dependerFQSEN->toString(), |
85
|
|
|
$dependeeComponent->getName(), |
86
|
|
|
$dependeeFQSEN->toString() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $response; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getComponentName(FQSEN $fqsen): ?string |
98
|
|
|
{ |
99
|
|
|
return $this->getComponent($fqsen) ? $this->getComponent($fqsen)->getName() : null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getComponent(FQSEN $fqsen): ?Component |
103
|
|
|
{ |
104
|
|
|
foreach ($this->components as $component) { |
105
|
|
|
if ($component->isBelongedTo($fqsen->toString())) { |
106
|
|
|
return $component; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getDefinition() |
114
|
|
|
{ |
115
|
|
|
return $this->definition; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function toArray() |
119
|
|
|
{ |
120
|
|
|
$components = []; |
121
|
|
|
foreach ($this->components as $component) { |
122
|
|
|
$components[$component->getName()] = $component->toArray(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return [$this->getRuleName() => $components]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|