Passed
Pull Request — master (#22)
by Luis
24:40 queued 21:48
created

ClassGraphBuilder::extractFrom()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 2
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 31 at column 21
Loading history...
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