Test Failed
Push — graphviz-refactoring ( 3f1b14 )
by Luis
02:18
created

plClassGraphElements   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 87
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B extractFrom() 0 22 4
A needAssociation() 0 3 3
A addElementsForAttributes() 0 5 2
A isAssociationResolved() 0 3 1
A __construct() 0 4 1
A addAssociationForVariable() 0 5 2
A addElementsForParameters() 0 7 3
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
class plClassGraphElements
9
{
10
    /** @var plHasDotRepresentation[] */
11
    private $dotElements = [];
12
13
    /** @var bool[] */
14
    private $associations = [];
15
16
    /** @var bool */
17
    private $createAssociations;
18
19
    /** @var plNodeLabelBuilder */
20
    private $labelBuilder;
21
22
    /** @var plHasNodeIdentifier[] */
23
    private $structure;
24
25
    public function __construct(bool $createAssociations, plNodeLabelBuilder $labelBuilder)
26
    {
27
        $this->createAssociations = $createAssociations;
28
        $this->labelBuilder = $labelBuilder;
29
    }
30
31
    /**
32
     * @param plHasNodeIdentifier[] $structure
33
     * @return plHasDotRepresentation[]
34
     */
35
    public function extractFrom(plPhpClass $class, array $structure): array
36
    {
37
        $this->dotElements = [];
38
        $this->associations = [];
39
        $this->structure = $structure;
40
41
        if ($this->createAssociations) {
42
            $this->addElementsForAttributes($class);
43
            $this->addElementsForParameters($class);
44
        }
45
46
        $this->dotElements[] = new plNode($class, $this->labelBuilder->labelForClass($class));
47
48
        if ($class->hasParent()) {
49
            $this->dotElements[] = plEdge::inheritance($class->extends, $class);
50
        }
51
52
        foreach ($class->implements as $interface) {
53
            $this->dotElements[] = plEdge::implementation($interface, $class);
54
        }
55
56
        return $this->dotElements;
57
    }
58
59
    /** @return plHasDotRepresentation[] */
60
    private function addElementsForAttributes(plPhpClass $class): void
61
    {
62
        /** @var plPhpAttribute $attribute */
63
        foreach ($class->attributes as $attribute) {
64
            $this->addAssociationForVariable($class, $attribute);
65
        }
66
    }
67
68
    /** @return plHasDotRepresentation[] */
69
    private function addElementsForParameters(plPhpClass $class): void
70
    {
71
        if (!$class->hasConstructor()) {
72
            return;
73
        }
74
        foreach ($class->constructorParameters() as $parameter) {
75
            $this->addAssociationForVariable($class, $parameter);
76
        }
77
    }
78
79
    private function addAssociationForVariable(plPhpClass $class, plPhpVariable $attribute): void
80
    {
81
        if ($this->needAssociation($attribute)) {
82
            $this->dotElements[] = plEdge::association($this->structure[(string)$attribute->type], $class);
83
            $this->associations[strtolower($attribute->type)] = true;
84
        }
85
    }
86
87
    private function needAssociation(plPhpVariable $attribute): bool
88
    {
89
        return $attribute->hasType() && !$attribute->isBuiltIn() && !$this->isAssociationResolved($attribute->type);
90
    }
91
92
    private function isAssociationResolved(string $type): bool
93
    {
94
        return array_key_exists(strtolower($type), $this->associations);
95
    }
96
}
97