Completed
Push — master ( b54cbd...d09870 )
by Luis
18:59
created

StructureBuilder::buildInterfaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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;
9
10
use PhUml\Code\Attribute;
11
use PhUml\Code\ClassDefinition;
12
use PhUml\Code\Definition;
13
use PhUml\Code\InterfaceDefinition;
14
use PhUml\Code\Method;
15
use PhUml\Code\Structure;
16
use PhUml\Code\TypeDeclaration;
17
use PhUml\Code\Variable;
18
use PhUml\Parser\Raw\RawDefinition;
19
use PhUml\Parser\Raw\RawDefinitions;
20
21
/**
22 81
 * It builds a `Structure` from a `RawDefinitions`
23
 */
24 81
class StructureBuilder
25 81
{
26
    /** @var Structure */
27 69
    private $structure;
28
29 69
    /** @var DefinitionBuilder */
30 63
    protected $builder;
31 54
32 36
    public function __construct(Structure $structure = null, DefinitionBuilder $builder = null)
33 63
    {
34
        $this->structure = $structure ?? new Structure();
35
        $this->builder = $builder ?? new DefinitionBuilder();
36 69
    }
37
38
    public function buildFrom(RawDefinitions $definitions): Structure
39 21
    {
40
        foreach ($definitions->all() as $definition) {
41 21
            if ($this->structure->has($definition->name())) {
42 21
                continue;
43 21
            }
44 21
            if ($definition->isClass()) {
45
                $this->structure->addClass($this->buildClass($definitions, $definition));
46
            } elseif ($definition->isInterface()) {
47
                $this->structure->addInterface($this->buildInterface($definitions, $definition));
48 48
            }
49
        }
50 48
        return $this->structure;
51 48
    }
52 48
53 48
    protected function buildInterface(RawDefinitions $definitions, RawDefinition $interface): InterfaceDefinition
54 48
    {
55 48
        return new InterfaceDefinition(
56
            $interface->name(),
57
            $this->builder->methods($interface),
58
            $this->resolveRelatedInterface($definitions, $interface->parent())
59
        );
60 63
    }
61
62 63
    protected function buildClass(RawDefinitions $definitions, RawDefinition $class): ClassDefinition
63 63
    {
64 42
        return new ClassDefinition(
65 42
            $class->name(),
66
            $this->builder->attributes($class),
67 63
            $this->builder->methods($class),
68
            $this->buildInterfaces($definitions, $class->interfaces()),
69
            $this->resolveParentClass($definitions, $class->parent())
70
        );
71 42
    }
72
73 42
    /**
74 42
     * @param string[] $implements
75 42
     * @return Definition[]
76
     */
77 42
    protected function buildInterfaces(RawDefinitions $definitions, array $implements): array
78
    {
79
        return array_map(function (string $interface) use ($definitions) {
80
            return $this->resolveRelatedInterface($definitions, $interface);
81 54
        }, $implements);
82
    }
83 54
84 54
    protected function resolveRelatedInterface(RawDefinitions $definitions, ?string $interface): ?Definition
85 36
    {
86 36
        if ($interface === null) {
87
            return null;
88 54
        }
89
        if (!$this->structure->has($interface)) {
90
            $this->structure->addInterface($this->buildInterface(
91 36
                $definitions,
92
                $definitions->get($interface)
93 36
            ));
94 36
        }
95 33
        return $this->structure->get($interface);
96
    }
97
98 9
    protected function resolveParentClass(RawDefinitions $definitions, ?string $parent): ?Definition
99 9
    {
100 9
        if ($parent === null) {
101 9
            return null;
102
        }
103 9
        if (!$this->structure->has($parent)) {
104 9
            $this->structure->addClass($this->buildClass($definitions, $definitions->get($parent)));
105 9
        }
106
        return $this->structure->get($parent);
107
    }
108
}
109