|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 8.0 |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhUml\Graphviz\Builders; |
|
9
|
|
|
|
|
10
|
|
|
use PhUml\Code\ClassDefinition; |
|
11
|
|
|
use PhUml\Code\Codebase; |
|
12
|
|
|
use PhUml\Code\Name; |
|
13
|
|
|
use PhUml\Code\Variables\HasType; |
|
14
|
|
|
use PhUml\Graphviz\Edge; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* It creates edges by inspecting a class |
|
18
|
|
|
* |
|
19
|
|
|
* 1. It creates edges by inspecting the attributes of a class |
|
20
|
|
|
* 2. It creates edges by inspecting the parameters of the constructor of a class |
|
21
|
|
|
*/ |
|
22
|
|
|
final class EdgesBuilder implements AssociationsBuilder |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var bool[] */ |
|
25
|
|
|
private array $associations = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* It creates an edge if the attribute |
|
29
|
|
|
* |
|
30
|
|
|
* - Has type information and it's not a PHP's built-in type |
|
31
|
|
|
* - The association hasn't already been resolved |
|
32
|
|
|
* |
|
33
|
|
|
* @return Edge[] |
|
34
|
|
|
*/ |
|
35
|
15 |
|
public function fromAttributes(ClassDefinition $class, Codebase $codebase): array |
|
36
|
|
|
{ |
|
37
|
15 |
|
return $this->buildEdgesFor($class, $class->attributes(), $codebase); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* It creates an edge if the constructor parameter |
|
42
|
|
|
* |
|
43
|
|
|
* - Has type information and it's not a PHP's built-in type |
|
44
|
|
|
* - The association hasn't already been resolved |
|
45
|
|
|
* |
|
46
|
|
|
* @return Edge[] |
|
47
|
|
|
*/ |
|
48
|
15 |
|
public function fromConstructor(ClassDefinition $class, Codebase $codebase): array |
|
49
|
|
|
{ |
|
50
|
15 |
|
return $this->buildEdgesFor($class, $class->constructorParameters(), $codebase); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param HasType[] $variables |
|
55
|
|
|
* @return Edge[] |
|
56
|
|
|
*/ |
|
57
|
15 |
|
private function buildEdgesFor(ClassDefinition $class, array $variables, Codebase $codebase): array |
|
58
|
|
|
{ |
|
59
|
15 |
|
$edges = []; |
|
60
|
15 |
|
foreach ($variables as $parameter) { |
|
61
|
12 |
|
if (! $this->needAssociation($class, $parameter)) { |
|
62
|
7 |
|
continue; |
|
63
|
|
|
} |
|
64
|
12 |
|
$edges[] = $this->addAssociations($class, $parameter, $codebase); |
|
65
|
|
|
} |
|
66
|
15 |
|
return array_merge(...$edges); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @return Edge[] */ |
|
70
|
12 |
|
private function addAssociations(ClassDefinition $class, HasType $attribute, Codebase $codebase): array |
|
71
|
|
|
{ |
|
72
|
12 |
|
$this->markAssociationResolvedFor($class, $attribute); |
|
73
|
|
|
|
|
74
|
12 |
|
return array_map( |
|
75
|
12 |
|
static fn (Name $reference): Edge => Edge::association($codebase->get($reference), $class), |
|
76
|
12 |
|
$attribute->references() |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
12 |
|
private function needAssociation(ClassDefinition $class, HasType $attribute): bool |
|
81
|
|
|
{ |
|
82
|
12 |
|
return ! $this->isAssociationResolved($class, $attribute); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
12 |
|
private function isAssociationResolved(ClassDefinition $class, HasType $attribute): bool |
|
86
|
|
|
{ |
|
87
|
12 |
|
return array_key_exists($this->associationKey($class, $attribute), $this->associations); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
12 |
|
private function markAssociationResolvedFor(ClassDefinition $class, HasType $attribute): void |
|
91
|
|
|
{ |
|
92
|
12 |
|
$this->associations[$this->associationKey($class, $attribute)] = true; |
|
93
|
12 |
|
} |
|
94
|
|
|
|
|
95
|
12 |
|
private function associationKey(ClassDefinition $class, HasType $attribute): string |
|
96
|
|
|
{ |
|
97
|
12 |
|
return $class->name() . $attribute->type(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|