resolveExternalAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 2
b 2
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code;
7
8
use PhUml\Code\ClassDefinition;
9
use PhUml\Code\Codebase;
10
use PhUml\Code\Name;
11
use PhUml\Code\Parameters\Parameter;
12
use PhUml\Code\Properties\Property;
13
14
/**
15
 * It checks the properties and the constructor parameters of a class looking for external definitions
16
 *
17
 * An external definition is either a class or interface from a third party library, or a built-in class or interface
18
 *
19
 * In the case of a third-party library or built-in type a `ClassDefinition` is added by default.
20
 * Although we don't really know if it's an interface or trait since we don't have access to the source code
21
 */
22
final class ExternalAssociationsResolver implements RelationshipsResolver
23
{
24 13
    public function resolve(Codebase $codebase): void
25
    {
26 13
        foreach ($codebase->definitions() as $definition) {
27 12
            if ($definition instanceof ClassDefinition) {
28 12
                $this->resolveForClass($definition, $codebase);
29
            }
30
        }
31
    }
32
33 12
    private function resolveForClass(ClassDefinition $definition, Codebase $codebase): void
34
    {
35 12
        $this->resolveExternalAttributes($definition, $codebase);
36 12
        $this->resolveExternalConstructorParameters($definition, $codebase);
37
    }
38
39 12
    private function resolveExternalAttributes(ClassDefinition $definition, Codebase $codebase): void
40
    {
41 12
        array_map(function (Property $property) use ($codebase): void {
42 6
            $this->resolveExternalAssociationsFromTypeNames($property->references(), $codebase);
43 12
        }, $definition->properties());
44
    }
45
46 12
    private function resolveExternalConstructorParameters(ClassDefinition $definition, Codebase $codebase): void
47
    {
48 12
        array_map(function (Parameter $parameter) use ($codebase): void {
49 7
            $this->resolveExternalAssociationsFromTypeNames($parameter->references(), $codebase);
50 12
        }, $definition->constructorParameters());
51
    }
52
53
    /** @param Name[] $references */
54 9
    private function resolveExternalAssociationsFromTypeNames(array $references, Codebase $codebase): void
55
    {
56 9
        array_map(static function (Name $reference) use ($codebase): void {
57 9
            if ($codebase->has($reference)) {
58 4
                return;
59
            }
60 5
            $codebase->add(new ClassDefinition($reference));
61
        }, $references);
62
    }
63
}
64