Completed
Push — master ( 9d1ab7...13f027 )
by Luis
04:13 queued 02:19
created

ClassGraphBuilder::extractFrom()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
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 NodeLabelBuilder */
34
    private $labelBuilder;
35
36 78
    public function __construct(NodeLabelBuilder $labelBuilder, AssociationsBuilder $associationsBuilder = null)
37
    {
38 78
        $this->associationsBuilder = $associationsBuilder ?? new NoAssociationsBuilder();
39 78
        $this->labelBuilder = $labelBuilder;
40 78
    }
41
42
    /**
43
     * The order in which the nodes and edges are created is as follows
44
     *
45
     * 1. The edges discovered via attributes inspection
46
     * 2. The edges discovered via the constructor parameters
47
     * 3. The node representing the class itself
48
     * 4. The parent class, if any
49
     * 5. The interfaces it implements, if any
50
     *
51
     * @return \PhUml\Graphviz\HasDotRepresentation[]
52
     */
53 60
    public function extractFrom(ClassDefinition $class, Structure $structure): array
54
    {
55 60
        $this->dotElements = [];
56
57 60
        $this->addAssociations($this->associationsBuilder->fromAttributes($class, $structure));
58 60
        $this->addAssociations($this->associationsBuilder->fromConstructor($class, $structure));
59
60 60
        $this->dotElements[] = new Node($class, $this->labelBuilder->forClass($class));
61
62 60
        if ($class->hasParent()) {
63 30
            $this->dotElements[] = Edge::inheritance($class->extends(), $class);
64
        }
65
66 60
        foreach ($class->implements() as $interface) {
67 9
            $this->dotElements[] = Edge::implementation($interface, $class);
68
        }
69
70 60
        return $this->dotElements;
71
    }
72
73
    /** @param Edge[] */
74 60
    private function addAssociations(array $edges): void
75
    {
76 60
        $this->dotElements = array_merge($this->dotElements, $edges);
77 60
    }
78
}
79