-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathResideInOneOfTheseNamespaces.php
More file actions
58 lines (47 loc) · 1.79 KB
/
ResideInOneOfTheseNamespaces.php
File metadata and controls
58 lines (47 loc) · 1.79 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
<?php
declare(strict_types=1);
namespace Arkitect\Expression\ForClasses;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Expression\MergeableExpression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\ViolationMessage;
use Arkitect\Rules\Violations;
class ResideInOneOfTheseNamespaces implements Expression, MergeableExpression
{
/** @var string[] */
private $namespaces;
public function __construct(string ...$namespaces)
{
$this->namespaces = array_unique($namespaces);
}
public function describe(ClassDescription $theClass, string $because): Description
{
$descr = implode(', ', $this->namespaces);
return new Description("should reside in one of these namespaces: $descr", $because);
}
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
$resideInNamespace = false;
foreach ($this->namespaces as $namespace) {
if ($theClass->namespaceMatches($namespace)) {
$resideInNamespace = true;
}
}
if (!$resideInNamespace) {
$violation = Violation::create(
$theClass->getFQCN(),
ViolationMessage::selfExplanatory($this->describe($theClass, $because))
);
$violations->add($violation);
}
}
public function mergeWith(Expression $expression): Expression
{
if (!$expression instanceof self) {
throw new \InvalidArgumentException('Can not merge expressions. The given expression should be of type '.\get_class($this).' but is of type '.\get_class($expression));
}
return new self(...array_merge($this->namespaces, $expression->namespaces));
}
}