Completed
Push — master ( b54cbd...d09870 )
by Luis
18:59
created

ClassGraphBuilder::extractFrom()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 2
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.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\Structure;
12
use PhUml\Graphviz\Edge;
13
use PhUml\Graphviz\Node;
14
15
/**
16
 * It produces the collection of nodes and edges related to a class
17
 *
18
 * - It creates an edge with the class it extends, if any,
19
 * - It creates edges from the the interfaces it implements
20
 * - It creates a node with the class itself
21
 * - It, optionally, discovers associations between classes/interfaces, by inspecting both:
22
 *   - The class attributes
23
 *   - The class constructor's parameters
24
 */
25
class ClassGraphBuilder
26
{
27
    /** @var \PhUml\Graphviz\HasDotRepresentation[] */
28
    private $dotElements;
29
30
    /** @var AssociationsBuilder */
31
    private $associationsBuilder;
32
33
    /** @var bool */
34
    private $createAssociations;
35
36
    /** @var NodeLabelBuilder */
37
    private $labelBuilder;
38
39
    public function __construct(NodeLabelBuilder $labelBuilder)
40
    {
41
        $this->associationsBuilder = new NoAssociationsBuilder();
42
        $this->labelBuilder = $labelBuilder;
43
    }
44
45
    public function createAssociations(): void
46
    {
47
        $this->createAssociations = true;
48
    }
49
50
    /**
51
     * The order in which the nodes and edges are created is as follows
52
     *
53
     * 1. The edges discovered via attributes inspection
54
     * 2. The edges discovered via the constructor parameters
55
     * 3. The node representing the class itself
56
     * 4. The parent class, if any
57
     * 5. The interfaces it implements, if any
58
     *
59
     * @return \PhUml\Graphviz\HasDotRepresentation[]
60
     */
61
    public function extractFrom(ClassDefinition $class, Structure $structure): array
62
    {
63
        $this->dotElements = [];
64
65
        if ($this->createAssociations) {
66
            $this->associationsBuilder = new EdgesBuilder($structure);
67
        }
68
69
        $this->addAssociations($this->associationsBuilder->attributesAssociationsFrom($class));
70
        $this->addAssociations($this->associationsBuilder->parametersAssociationsFom($class));
71
72
        $this->dotElements[] = new Node($class, $this->labelBuilder->forClass($class));
73
74
        if ($class->hasParent()) {
75
            $this->dotElements[] = Edge::inheritance($class->extends, $class);
76
        }
77
78
        foreach ($class->implements as $interface) {
79
            $this->dotElements[] = Edge::implementation($interface, $class);
80
        }
81
82
        return $this->dotElements;
83
    }
84
85
    /** @param Edge[] */
86
    private function addAssociations(array $edges): void
87
    {
88
        $this->dotElements = array_merge($this->dotElements, $edges);
89
    }
90
}
91