1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace DependencyAnalyzer\Inspector\RuleViolationDetector; |
5
|
|
|
|
6
|
|
|
use DependencyAnalyzer\DependencyGraph; |
7
|
|
|
use DependencyAnalyzer\Patterns\QualifiedNamePattern; |
8
|
|
|
use DependencyAnalyzer\Inspector\Responses\VerifyDependencyResponse; |
9
|
|
|
use Fhaculty\Graph\Vertex; |
10
|
|
|
|
11
|
|
|
class DependencyRule |
12
|
|
|
{ |
13
|
|
|
// group namespaces |
14
|
|
|
// check dependency direction |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $definition; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Component[] |
23
|
|
|
*/ |
24
|
|
|
protected $components = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $ruleName; |
30
|
|
|
|
31
|
|
|
public function __construct(string $ruleName, array $components) |
32
|
|
|
{ |
33
|
|
|
$this->ruleName = $ruleName; |
34
|
|
|
$this->components = $components; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getRuleName(): string |
38
|
|
|
{ |
39
|
|
|
return $this->ruleName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isSatisfyBy(DependencyGraph $graph): VerifyDependencyResponse |
43
|
|
|
{ |
44
|
|
|
$response = new VerifyDependencyResponse($this->getRuleName()); |
45
|
|
|
// $errors = []; |
46
|
|
|
|
47
|
|
|
foreach ($graph->getDependencyArrows() as $edge) { |
48
|
|
|
$depender = $edge->getVertexStart(); |
49
|
|
|
$dependee = $edge->getVertexEnd(); |
50
|
|
|
|
51
|
|
|
// TODO: add exclude rule |
52
|
|
|
if (is_null($this->getComponentName($depender)) || is_null($this->getComponentName($dependee))) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($this->components as $component) { |
57
|
|
|
if ($component->isBelongedTo($dependee->getId())) { |
58
|
|
|
if (!$component->verifyDepender($depender->getId())) { |
59
|
|
|
$response->addRuleViolation( |
60
|
|
|
$this->getComponent($depender)->getName(), |
61
|
|
|
$depender->getId(), |
62
|
|
|
$this->getComponent($dependee)->getName(), |
63
|
|
|
$dependee->getId() |
64
|
|
|
); |
65
|
|
|
// $errors[] = "{$depender->getId()}({$this->getComponentName($depender)}) must not depend on {$dependee->getId()}({$this->getComponentName($dependee)})."; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($component->isBelongedTo($depender->getId())) { |
70
|
|
|
if (!$component->verifyDependee($dependee->getId())) { |
71
|
|
|
$response->addRuleViolation( |
72
|
|
|
$this->getComponent($depender)->getName(), |
73
|
|
|
$depender->getId(), |
74
|
|
|
$this->getComponent($dependee)->getName(), |
75
|
|
|
$dependee->getId() |
76
|
|
|
); |
77
|
|
|
// $errors[] = "{$depender->getId()}({$this->getComponentName($depender)}) must not depend on {$dependee->getId()}({$this->getComponentName($dependee)})."; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
// return $errors; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getComponentName(Vertex $vertex): ?string |
88
|
|
|
{ |
89
|
|
|
return $this->getComponent($vertex) ? $this->getComponent($vertex)->getName() : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getComponent(Vertex $vertex): ?Component |
93
|
|
|
{ |
94
|
|
|
foreach ($this->components as $component) { |
95
|
|
|
if ($component->isBelongedTo($vertex->getId())) { |
96
|
|
|
return $component; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getDefinition() |
104
|
|
|
{ |
105
|
|
|
return $this->definition; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|