Passed
Push — parser-refactoring ( e758c6 )
by Luis
04:05
created

Definitions   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addClass() 0 8 1
A addExternalInterface() 0 3 1
A all() 0 3 1
A get() 0 3 1
A add() 0 6 3
A addInterface() 0 6 1
A parent() 0 3 1
A isInterface() 0 3 1
A has() 0 3 1
A interfaces() 0 3 1
A isClass() 0 3 1
A addExternalClass() 0 3 1
A hasParent() 0 3 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
namespace PhUml\Parser;
8
9
class Definitions
10
{
11
    private static $classDefaults = [
12
        'attributes' => [],
13
        'functions' => [],
14
        'implements' => [],
15
        'extends' => null,
16
    ];
17
18
    private static $interfaceDefaults = [
19
        'functions' => [],
20
        'extends' => null,
21
    ];
22
23
    /** @var array */
24
    private $definitions;
25
26 75
    public function __construct()
27
    {
28 75
        $this->definitions = [];
29 75
    }
30
31 51
    public function add(array $definition): void
32
    {
33 51
        if ($this->isClass($definition)) {
34 39
            $this->addClass($definition);
35 45
        } elseif ($this->isInterface($definition)) {
36 27
            $this->addInterface($definition);
37
        }
38 51
    }
39
40 9
    public function get(string $name)
41
    {
42 9
        return $this->definitions[$name] ?? null;
43
    }
44
45 15
    public function addExternalClass(string $name): void
46
    {
47 15
        $this->definitions[$name] = array_merge(['class' => $name], self::$classDefaults);
48 15
    }
49
50 9
    public function addExternalInterface(string $name): void
51
    {
52 9
        $this->definitions[$name] = array_merge(['interface' => $name], self::$interfaceDefaults);
53 9
    }
54
55 27
    public function has(string $definitionName): bool
56
    {
57 27
        return isset($this->definitions[$definitionName]);
58
    }
59
60 48
    public function all(): array
61
    {
62 48
        return $this->definitions;
63
    }
64
65
    /** @return string[] */
66 39
    public function interfaces(array $definition): array
67
    {
68 39
        return $definition['implements'];
69
    }
70
71 45
    public function hasParent(array $definition): bool
72
    {
73 45
        return isset($definition['extends']);
74
    }
75
76 21
    public function parent(array $definition): ?string
77
    {
78 21
        return $definition['extends'] ?? null;
79
    }
80
81 57
    public function isClass(array $definition): bool
82
    {
83 57
        return isset($definition['class']);
84
    }
85
86 48
    public function isInterface(array $definition): bool
87
    {
88 48
        return isset($definition['interface']);
89
    }
90
91 39
    private function addClass(array $definition): void
92
    {
93 39
        $this->definitions[$definition['class']] = [
94 39
            'class' => $definition['class'],
95 39
            'attributes' => $definition['attributes'] ?? self::$classDefaults['attributes'],
96 39
            'functions' => $definition['functions'] ?? self::$classDefaults['functions'],
97 39
            'implements' => $definition['implements'] ?? self::$classDefaults['implements'],
98 39
            'extends' => $definition['extends'] ?? self::$classDefaults['extends'],
99
        ];
100 39
    }
101
102 27
    private function addInterface(array $definition): void
103
    {
104 27
        $this->definitions[$definition['interface']] = [
105 27
            'interface' => $definition['interface'],
106 27
            'functions' => $definition['functions'] ?? self::$interfaceDefaults['functions'],
107 27
            'extends' => $definition['extends'] ?? self::$interfaceDefaults['extends'],
108
        ];
109 27
    }
110
}
111