Passed
Push — cleanup ( 1df3c6 )
by Luis
14:39
created

ClassGraphBuilder::addAssociationForVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
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\Code\Variable;
13
use PhUml\Graphviz\Edge;
14
use PhUml\Graphviz\Node;
15
16
class ClassGraphBuilder
17
{
18
    /** @var \PhUml\Graphviz\HasDotRepresentation[] */
19
    private $dotElements = [];
20
21
    /** @var bool[] */
22
    private $associations = [];
23
24
    /** @var bool */
25
    private $createAssociations;
26
27
    /** @var NodeLabelBuilder */
28
    private $labelBuilder;
29
30
    /** @var Structure */
31
    private $structure;
32
33
    public function __construct(NodeLabelBuilder $labelBuilder)
34
    {
35
        $this->createAssociations = false;
36
        $this->labelBuilder = $labelBuilder;
37
    }
38
39
    public function createAssociations(): void
40
    {
41
        $this->createAssociations = true;
42
    }
43
44
    /** @return \PhUml\Graphviz\HasDotRepresentation[] */
45
    public function extractFrom(ClassDefinition $class, Structure $structure): array
46
    {
47
        $this->dotElements = [];
48
        $this->associations = [];
49
        $this->structure = $structure;
50
51
        if ($this->createAssociations) {
52
            $this->addElementsForAttributes($class);
53
            $this->addElementsForParameters($class);
54
        }
55
56
        $this->dotElements[] = new Node($class, $this->labelBuilder->forClass($class));
57
58
        if ($class->hasParent()) {
59
            $this->dotElements[] = Edge::inheritance($class->extends, $class);
60
        }
61
62
        foreach ($class->implements as $interface) {
63
            $this->dotElements[] = Edge::implementation($interface, $class);
64
        }
65
66
        return $this->dotElements;
67
    }
68
69
    /** @return \PhUml\Graphviz\HasDotRepresentation[] */
70
    private function addElementsForAttributes(ClassDefinition $class): void
71
    {
72
        /** @var \PhUml\Code\Attribute $attribute */
73
        foreach ($class->attributes as $attribute) {
74
            $this->addAssociationForVariable($class, $attribute);
75
        }
76
    }
77
78
    /** @return \PhUml\Graphviz\HasDotRepresentation[] */
79
    private function addElementsForParameters(ClassDefinition $class): void
80
    {
81
        if (!$class->hasConstructor()) {
82
            return;
83
        }
84
        foreach ($class->constructorParameters() as $parameter) {
85
            $this->addAssociationForVariable($class, $parameter);
86
        }
87
    }
88
89
    private function addAssociationForVariable(ClassDefinition $class, Variable $attribute): void
90
    {
91
        if ($this->needAssociation($attribute)) {
92
            $this->dotElements[] = Edge::association(
93
                $this->structure->get((string)$attribute->type),
94
                $class
95
            );
96
            $this->associations[strtolower($attribute->type)] = true;
97
        }
98
    }
99
100
    private function needAssociation(Variable $attribute): bool
101
    {
102
        return $attribute->hasType() && !$attribute->isBuiltIn() && !$this->isAssociationResolved($attribute->type);
103
    }
104
105
    private function isAssociationResolved(string $type): bool
106
    {
107
        return array_key_exists(strtolower($type), $this->associations);
108
    }
109
}
110