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
|
|
|
public function fromAttributes(ClassDefinition $class, Codebase $codebase): array |
36
|
|
|
{ |
37
|
|
|
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
|
|
|
public function fromConstructor(ClassDefinition $class, Codebase $codebase): array |
49
|
|
|
{ |
50
|
|
|
return $this->buildEdgesFor($class, $class->constructorParameters(), $codebase); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param HasType[] $variables |
55
|
|
|
* @return Edge[] |
56
|
|
|
*/ |
57
|
|
|
private function buildEdgesFor(ClassDefinition $class, array $variables, Codebase $codebase): array |
58
|
|
|
{ |
59
|
|
|
$edges = []; |
60
|
|
|
foreach ($variables as $parameter) { |
61
|
|
|
if (! $this->needAssociation($class, $parameter)) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
$edges[] = $this->addAssociations($class, $parameter, $codebase); |
65
|
|
|
} |
66
|
|
|
return array_merge(...$edges); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @return Edge[] */ |
70
|
|
|
private function addAssociations(ClassDefinition $class, HasType $attribute, Codebase $codebase): array |
71
|
|
|
{ |
72
|
|
|
$this->markAssociationResolvedFor($class, $attribute); |
73
|
|
|
|
74
|
|
|
return array_map( |
75
|
|
|
fn (Name $reference): Edge => Edge::association($codebase->get($reference), $class), |
76
|
|
|
$attribute->references() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function needAssociation(ClassDefinition $class, HasType $attribute): bool |
81
|
|
|
{ |
82
|
|
|
return ! $this->isAssociationResolved($class, $attribute); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function isAssociationResolved(ClassDefinition $class, HasType $attribute): bool |
86
|
|
|
{ |
87
|
|
|
return array_key_exists($this->associationKey($class, $attribute), $this->associations); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function markAssociationResolvedFor(ClassDefinition $class, HasType $attribute): void |
91
|
|
|
{ |
92
|
|
|
$this->associations[$this->associationKey($class, $attribute)] = true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function associationKey(ClassDefinition $class, HasType $attribute): string |
96
|
|
|
{ |
97
|
|
|
return strtolower($class->name() . '.' . $attribute->type()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|