Passed
Push — 1.5 ( c7c5a4...fe8795 )
by Luis
06:06
created

ExternalAssociationsResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveExternalAttributes() 0 7 3
A resolveExternalConstructorParameters() 0 7 3
A resolveForClass() 0 5 1
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\Parser\Code;
9
10
use PhUml\Code\Attributes\Attribute;
11
use PhUml\Code\ClassDefinition;
12
use PhUml\Code\Codebase;
13
use PhUml\Code\Variables\Variable;
14
15
/**
16
 * It checks the attributes and the constructor parameters of a class looking for external definitions
17
 *
18
 * An external definition is either a class or interface from a third party library, or a built-in
19
 * class
20
 *
21
 * In this case a `ClassDefinition` is added by default.
22
 * Although we don't really know if it's an interface since we don't have access to the source code
23
 */
24
class ExternalAssociationsResolver extends ExternalDefinitionsResolver
25
{
26 27
    protected function resolveForClass(ClassDefinition $definition, Codebase $codebase): void
27
    {
28 27
        parent::resolveForClass($definition, $codebase);
29 27
        $this->resolveExternalAttributes($definition, $codebase);
30 27
        $this->resolveExternalConstructorParameters($definition, $codebase);
31 27
    }
32
33
    private function resolveExternalAttributes(ClassDefinition $definition, Codebase $codebase): void
34
    {
35 27
        array_map(function (Attribute $attribute) use ($codebase) {
36 15
            if ($attribute->isAReference() && !$codebase->has($attribute->typeName())) {
37 3
                $codebase->add($this->externalClass($attribute->typeName()));
38
            }
39 27
        }, $definition->attributes());
40 27
    }
41
42
    private function resolveExternalConstructorParameters(ClassDefinition $definition, Codebase $codebase): void
43
    {
44 27
        array_map(function (Variable $parameter) use ($codebase) {
45 15
            if ($parameter->isAReference() && !$codebase->has($parameter->typeName())) {
46 3
                $codebase->add($this->externalClass($parameter->typeName()));
47
            }
48 27
        }, $definition->constructorParameters());
49
    }
50
}
51