Passed
Push — master ( 1416ae...b5a8b1 )
by Luis
54s queued 13s
created

EdgesBuilder::fromProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
The type PhUml\Code\ClassDefinition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhUml\Code\Codebase;
12
use PhUml\Code\Name;
13
use PhUml\Code\Variables\HasType;
14
use PhUml\Graphviz\Edge;
0 ignored issues
show
Bug introduced by
The type PhUml\Graphviz\Edge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * It creates edges by inspecting a class
18
 *
19
 * 1. It creates edges by inspecting the properties of a class
20
 * 2. It creates edges by inspecting the parameters of the constructor of a class
21
 */
22
final class EdgesBuilder implements AssociationsBuilder
23
{
24
    /** @var bool[] */
25
    private array $associations = [];
26
27
    /**
28
     * It creates an edge if the property
29
     *
30
     * - Has type information, and it's not a PHP's built-in type
31
     * - The association hasn't already been resolved
32
     *
33
     * @return Edge[]
34
     */
35 15
    public function fromProperties(ClassDefinition $class, Codebase $codebase): array
36
    {
37 15
        return $this->buildEdgesFor($class, $class->properties(), $codebase);
38
    }
39
40
    /**
41
     * It creates an edge if the constructor parameter
42
     *
43
     * - Has type information and it's not a PHP's built-in type
44
     * - The association hasn't already been resolved
45
     *
46
     * @return Edge[]
47
     */
48 15
    public function fromConstructor(ClassDefinition $class, Codebase $codebase): array
49
    {
50 15
        return $this->buildEdgesFor($class, $class->constructorParameters(), $codebase);
51
    }
52
53
    /**
54
     * @param HasType[] $variables
55
     * @return Edge[]
56
     */
57 15
    private function buildEdgesFor(ClassDefinition $class, array $variables, Codebase $codebase): array
58
    {
59 15
        $edges = [];
60 15
        foreach ($variables as $parameter) {
61 12
            if (! $this->needAssociation($class, $parameter)) {
62 7
                continue;
63
            }
64 12
            $edges[] = $this->addAssociations($class, $parameter, $codebase);
65
        }
66 15
        return array_merge(...$edges);
67
    }
68
69
    /** @return Edge[] */
70 12
    private function addAssociations(ClassDefinition $class, HasType $property, Codebase $codebase): array
71
    {
72 12
        $this->markAssociationResolvedFor($class, $property);
73
74 12
        return array_map(
75 12
            static fn (Name $reference): Edge => Edge::association($codebase->get($reference), $class),
76 12
            $property->references()
77
        );
78
    }
79
80 12
    private function needAssociation(ClassDefinition $class, HasType $property): bool
81
    {
82 12
        return ! $this->isAssociationResolved($class, $property);
83
    }
84
85 12
    private function isAssociationResolved(ClassDefinition $class, HasType $property): bool
86
    {
87 12
        return array_key_exists($this->associationKey($class, $property), $this->associations);
88
    }
89
90 12
    private function markAssociationResolvedFor(ClassDefinition $class, HasType $property): void
91
    {
92 12
        $this->associations[$this->associationKey($class, $property)] = true;
93
    }
94
95 12
    private function associationKey(ClassDefinition $class, HasType $property): string
96
    {
97 12
        return $class->name() . $property->type();
98
    }
99
}
100