Completed
Push — symfony-console-application ( 3187e2...c3ee2a )
by Luis
10:39
created

Definitions   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 99
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
    public function __construct()
27
    {
28
        $this->definitions = [];
29
    }
30
31
    public function add(array $definition): void
32
    {
33
        if ($this->isClass($definition)) {
34
            $this->addClass($definition);
35
        } elseif ($this->isInterface($definition)) {
36
            $this->addInterface($definition);
37
        }
38
    }
39
40
    public function get(string $name)
41
    {
42
        return $this->definitions[$name] ?? null;
43
    }
44
45
    public function addExternalClass(string $name): void
46
    {
47
        $this->definitions[$name] = array_merge(['class' => $name], self::$classDefaults);
48
    }
49
50
    public function addExternalInterface(string $name): void
51
    {
52
        $this->definitions[$name] = array_merge(['interface' => $name], self::$interfaceDefaults);
53
    }
54
55
    public function has(string $definitionName): bool
56
    {
57
        return isset($this->definitions[$definitionName]);
58
    }
59
60
    public function all(): array
61
    {
62
        return $this->definitions;
63
    }
64
65
    /** @return string[] */
66
    public function interfaces(array $definition): array
67
    {
68
        return $definition['implements'];
69
    }
70
71
    public function hasParent(array $definition): bool
72
    {
73
        return isset($definition['extends']);
74
    }
75
76
    public function parent(array $definition): ?string
77
    {
78
        return $definition['extends'] ?? null;
79
    }
80
81
    public function isClass(array $definition): bool
82
    {
83
        return isset($definition['class']);
84
    }
85
86
    public function isInterface(array $definition): bool
87
    {
88
        return isset($definition['interface']);
89
    }
90
91
    private function addClass(array $definition): void
92
    {
93
        $this->definitions[$definition['class']] = [
94
            'class' => $definition['class'],
95
            'attributes' => $definition['attributes'] ?? self::$classDefaults['attributes'],
96
            'functions' => $definition['functions'] ?? self::$classDefaults['functions'],
97
            'implements' => $definition['implements'] ?? self::$classDefaults['implements'],
98
            'extends' => $definition['extends'] ?? self::$classDefaults['extends'],
99
        ];
100
    }
101
102
    private function addInterface(array $definition): void
103
    {
104
        $this->definitions[$definition['interface']] = [
105
            'interface' => $definition['interface'],
106
            'functions' => $definition['functions'] ?? self::$interfaceDefaults['functions'],
107
            'extends' => $definition['extends'] ?? self::$interfaceDefaults['extends'],
108
        ];
109
    }
110
}
111