|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hal\Violation\Package; |
|
4
|
|
|
|
|
5
|
|
|
use Hal\Metric\Metric; |
|
6
|
|
|
use Hal\Metric\PackageMetric; |
|
7
|
|
|
use Hal\Violation\Violation; |
|
8
|
|
|
|
|
9
|
|
|
class StableDependenciesPrinciple implements Violation |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var PackageMetric|null */ |
|
12
|
|
|
private $metric; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
private $violatingInstabilities = []; |
|
16
|
|
|
|
|
17
|
|
|
public function getName() |
|
18
|
|
|
{ |
|
19
|
|
|
return 'Stable Dependencies Principle'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function apply(Metric $metric) |
|
23
|
|
|
{ |
|
24
|
|
|
if (! $metric instanceof PackageMetric) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
$instability = $metric->getInstability(); |
|
28
|
|
|
$violatingInstabilities = array_filter( |
|
29
|
|
|
$metric->getDependentInstabilities(), |
|
30
|
|
|
function ($otherInstability) use ($instability) { |
|
31
|
|
|
return $otherInstability >= $instability; |
|
32
|
|
|
} |
|
33
|
|
|
); |
|
34
|
|
|
if (count($violatingInstabilities) > 0) { |
|
35
|
|
|
$this->violatingInstabilities = $violatingInstabilities; |
|
36
|
|
|
$this->metric = $metric; |
|
37
|
|
|
$metric->get('violations')->add($this); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getLevel() |
|
42
|
|
|
{ |
|
43
|
|
|
return Violation::WARNING; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getDescription() |
|
47
|
|
|
{ |
|
48
|
|
|
$count = count($this->violatingInstabilities); |
|
49
|
|
|
$thisInstability = round($this->metric->getInstability(), 3); |
|
50
|
|
|
$packages = implode("\n* ", array_map(function ($name, $instability) { |
|
51
|
|
|
$name = $name === '\\' ? 'global' : substr($name, 0, -1); |
|
52
|
|
|
$instability = round($instability, 3); |
|
53
|
|
|
return "$name ($instability)"; |
|
54
|
|
|
}, array_keys($this->violatingInstabilities), $this->violatingInstabilities)); |
|
55
|
|
|
return <<<EOT |
|
56
|
|
|
Packages should depend in the direction of stability. |
|
57
|
|
|
|
|
58
|
|
|
This package is more stable ({$thisInstability}) than {$count} package(s) that it depends on. |
|
59
|
|
|
The packages that are more stable are |
|
60
|
|
|
|
|
61
|
|
|
* {$packages} |
|
62
|
|
|
EOT; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.