-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathDependsOnlyOnTheseNamespaces.php
More file actions
59 lines (48 loc) · 1.77 KB
/
DependsOnlyOnTheseNamespaces.php
File metadata and controls
59 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Arkitect\Expression\ForClasses;
use Arkitect\Analyzer\ClassDependency;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;
class DependsOnlyOnTheseNamespaces implements Expression
{
/** @var string[] */
private $namespaces;
public function __construct(string ...$namespace)
{
$this->namespaces = $namespace;
}
public function describe(ClassDescription $theClass, string $because): Description
{
$desc = implode(', ', $this->namespaces);
return new Description("should depend only on classes in one of these namespaces: $desc", $because);
}
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
$dependencies = $theClass->getDependencies();
/** @var ClassDependency $dependency */
foreach ($dependencies as $dependency) {
if (
'' === $dependency->getFQCN()->namespace()
|| $theClass->namespaceMatches($dependency->getFQCN()->namespace())
) {
continue;
}
if (!$dependency->matchesOneOf(...$this->namespaces)) {
$violation = Violation::createWithErrorLine(
$theClass->getFQCN(),
ViolationMessage::withDescription(
$this->describe($theClass, $because),
"depends on {$dependency->getFQCN()->toString()}"
),
$dependency->getLine()
);
$violations->add($violation);
}
}
}
}