1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* PHP version 8.1 |
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\Graphviz\Edge; |
13
|
|
|
use PhUml\Graphviz\HasDotRepresentation; |
14
|
|
|
use PhUml\Graphviz\Node; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* It produces the collection of nodes and edges related to a class |
18
|
|
|
* |
19
|
|
|
* - It creates an edge with the class it extends, if any, |
20
|
|
|
* - It creates edges from the interfaces it implements |
21
|
|
|
* - It creates a node with the class itself |
22
|
|
|
* - It, optionally, discovers associations between classes/interfaces, by inspecting both: |
23
|
|
|
* - The class properties |
24
|
|
|
* - The class constructor's parameters |
25
|
|
|
*/ |
26
|
|
|
final class ClassGraphBuilder |
27
|
|
|
{ |
28
|
|
|
/** @var HasDotRepresentation[] */ |
29
|
|
|
private array $dotElements; |
30
|
|
|
|
31
|
|
|
private readonly AssociationsBuilder $associationsBuilder; |
|
|
|
|
32
|
|
|
|
33
|
29 |
|
public function __construct(AssociationsBuilder $associationsBuilder = null) |
34
|
|
|
{ |
35
|
29 |
|
$this->dotElements = []; |
36
|
29 |
|
$this->associationsBuilder = $associationsBuilder ?? new NoAssociationsBuilder(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The order in which the nodes and edges are created is as follows |
41
|
|
|
* |
42
|
|
|
* 1. The edges discovered via properties inspection |
43
|
|
|
* 2. The edges discovered via the constructor parameters |
44
|
|
|
* 3. The node representing the class itself |
45
|
|
|
* 4. The parent class, if any |
46
|
|
|
* 5. The interfaces it implements, if any |
47
|
|
|
* |
48
|
|
|
* @return HasDotRepresentation[] |
49
|
|
|
*/ |
50
|
22 |
|
public function extractFrom(ClassDefinition $class, Codebase $codebase): array |
51
|
|
|
{ |
52
|
22 |
|
$this->dotElements = []; |
53
|
|
|
|
54
|
22 |
|
$this->addAssociations($this->associationsBuilder->fromProperties($class, $codebase)); |
55
|
22 |
|
$this->addAssociations($this->associationsBuilder->fromConstructor($class, $codebase)); |
56
|
|
|
|
57
|
22 |
|
$this->dotElements[] = new Node($class); |
58
|
|
|
|
59
|
22 |
|
if ($class->hasParent()) { |
60
|
11 |
|
$this->dotElements[] = Edge::inheritance($codebase->get($class->parent()), $class); |
61
|
|
|
} |
62
|
|
|
|
63
|
22 |
|
foreach ($class->interfaces() as $interface) { |
64
|
10 |
|
$this->dotElements[] = Edge::implementation($codebase->get($interface), $class); |
65
|
|
|
} |
66
|
|
|
|
67
|
22 |
|
foreach ($class->traits() as $trait) { |
68
|
8 |
|
$this->dotElements[] = Edge::use($codebase->get($trait), $class); |
69
|
|
|
} |
70
|
|
|
|
71
|
22 |
|
return $this->dotElements; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @param Edge[] $edges */ |
75
|
22 |
|
private function addAssociations(array $edges): void |
76
|
|
|
{ |
77
|
22 |
|
$this->dotElements = array_merge($this->dotElements, $edges); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|