-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathHaveCorrespondingUnit.php
57 lines (46 loc) · 1.53 KB
/
HaveCorrespondingUnit.php
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
<?php
declare(strict_types=1);
namespace Arkitect\Expression\ForClasses;
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Expression\Description;
use Arkitect\Expression\Expression;
use Arkitect\Rules\Violation;
use Arkitect\Rules\Violations;
final class HaveCorrespondingUnit implements Expression
{
/** @var \Closure */
private $inferFqnFunction;
public function __construct(\Closure $inferFqnFunction)
{
$this->inferFqnFunction = $inferFqnFunction;
}
public function describe(ClassDescription $theClass, string $because): Description
{
$correspondingFqn = $this->inferCorrespondingFqn($theClass);
return new Description("should have a matching unit named: '$correspondingFqn'", $because);
}
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
$correspondingFqn = $this->inferCorrespondingFqn($theClass);
if (
!trait_exists($correspondingFqn)
&& !class_exists($correspondingFqn)
&& !interface_exists($correspondingFqn)
) {
$violations->add(
new Violation(
$theClass->getFQCN(),
$this->describe($theClass, $because)->toString()
)
);
}
}
/**
* @return class-string
*/
public function inferCorrespondingFqn(ClassDescription $theClass): string
{
$inferFqn = $this->inferFqnFunction;
return $inferFqn($theClass->getFQCN());
}
}