Passed
Push — cleanup ( 1df3c6...d09870 )
by Luis
12:15
created

ExternalDefinitionsResolver::resolveParentClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
rs 9.4285
c 1
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\Raw;
9
10
use PhUml\Parser\Raw\RawDefinition;
11
use PhUml\Parser\Raw\RawDefinitions;
12
13
/**
14
 * It checks the parent of a definition and the interfaces it implements looking for external
15
 * definitions
16
 *
17
 * An external definition is a class or interface that belongs to a third party library or
18
 * a PHP's built-in class
19
 */
20
class ExternalDefinitionsResolver
21
{
22
    public function resolve(RawDefinitions $definitions): void
23
    {
24
        foreach ($definitions->all() as $definition) {
25
            if ($definition->isClass()) {
26
                $this->resolveForClass($definitions, $definition);
27
            } else {
28
                $this->resolveForInterface($definitions, $definition);
29
            }
30
        }
31
    }
32
33
    private function resolveForClass(RawDefinitions $definitions, RawDefinition $definition): void
34
    {
35
        foreach ($definition->interfaces() as $interface) {
36
            if (!$definitions->has($interface)) {
37
                $definitions->addExternalInterface($interface);
38
            }
39
        }
40
        $this->resolveParentClass($definitions, $definition);
41
    }
42
43
    private function resolveForInterface(RawDefinitions $definitions, RawDefinition $definition): void
44
    {
45
        $this->resolveParentInterface($definitions, $definition);
46
    }
47
48
    private function resolveParentClass(RawDefinitions $definitions, RawDefinition $definition): void
49
    {
50
        if ($definition->hasParent()) {
51
            $parent = $definition->parent();
52
            if (!$definitions->has($parent)) {
53
                $definitions->addExternalClass($parent);
54
            }
55
        }
56
    }
57
58
    private function resolveParentInterface(RawDefinitions $definitions, RawDefinition $definition): void
59
    {
60
        if ($definition->hasParent()) {
61
            $parent = $definition->parent();
62
            if (!$definitions->has($parent)) {
63
                $definitions->addExternalInterface($parent);
64
            }
65
        }
66
    }
67
}
68