AbstractPart   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Test Coverage

Coverage 91.38%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 181
ccs 53
cts 58
cp 0.9138
rs 10
c 0
b 0
f 0
wmc 21

18 Methods

Rating   Name   Duplication   Size   Complexity  
A isInitialized() 0 3 1
A set() 0 6 1
A get() 0 5 1
A setData() 0 5 1
A getData() 0 5 1
A add() 0 6 1
A has() 0 5 1
A __set() 0 3 1
A __unset() 0 3 1
A offsetSet() 0 3 1
A remove() 0 7 1
A offsetGet() 0 3 1
A __get() 0 3 1
A preparePartValue() 0 9 3
A __isset() 0 3 1
A initialize() 0 9 2
A offsetUnset() 0 3 1
A offsetExists() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Purl;
6
7
use ArrayAccess;
8
9
/**
10
 * AbstractPart class is implemented by each part of a Url where necessary.
11
 *
12
 * @implements ArrayAccess
13
 */
14
abstract class AbstractPart implements ArrayAccess
15
{
16
    /** @var bool */
17
    protected $initialized = false;
18
19
    /** @var mixed[] */
20
    protected $data = [];
21
22
    /** @var string[] */
23
    protected $partClassMap = [];
24
25
    /**
26
     * @return mixed[]
27
     */
28 4
    public function getData() : array
29
    {
30 4
        $this->initialize();
31
32 4
        return $this->data;
33
    }
34
35
    /**
36
     * @param mixed[] $data
37
     */
38 7
    public function setData(array $data) : void
39
    {
40 7
        $this->initialize();
41
42 7
        $this->data = $data;
43 7
    }
44
45 1
    public function isInitialized() : bool
46
    {
47 1
        return $this->initialized;
48
    }
49
50 3
    public function has(string $key) : bool
51
    {
52 3
        $this->initialize();
53
54 3
        return isset($this->data[$key]);
55
    }
56
57
    /**
58
     * @return mixed|null
59
     */
60 36
    public function get(string $key)
61
    {
62 36
        $this->initialize();
63
64 36
        return $this->data[$key] ?? null;
65
    }
66
67
    /**
68
     * @param mixed $value
69
     */
70 8
    public function set(string $key, $value) : AbstractPart
71
    {
72 8
        $this->initialize();
73 8
        $this->data[$key] = $value;
74
75 8
        return $this;
76
    }
77
78
    /**
79
     * @param mixed $value
80
     */
81 3
    public function add($value) : AbstractPart
82
    {
83 3
        $this->initialize();
84 3
        $this->data[] = $value;
85
86 3
        return $this;
87
    }
88
89 3
    public function remove(string $key) : AbstractPart
90
    {
91 3
        $this->initialize();
92
93 3
        unset($this->data[$key]);
94
95 3
        return $this;
96
    }
97
98
    public function __isset(string $key) : bool
99
    {
100
        return $this->has($key);
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106 35
    public function __get(string $key)
107
    {
108 35
        return $this->get($key);
109
    }
110
111
    /**
112
     * @param mixed $value
113
     */
114 6
    public function __set(string $key, $value) : AbstractPart
115
    {
116 6
        return $this->set($key, $value);
117
    }
118
119
    public function __unset(string $key) : void
120
    {
121
        $this->remove($key);
122
    }
123
124
    /**
125
     * @param mixed $key
126
     */
127 2
    public function offsetExists($key) : bool
128
    {
129 2
        $this->initialize();
130
131 2
        return isset($this->data[$key]);
132
    }
133
134
    /**
135
     * @param mixed $key
136
     *
137
     * @return mixed
138
     */
139 1
    public function offsetGet($key)
140
    {
141 1
        return $this->get($key);
142
    }
143
144
    /**
145
     * @param mixed $key
146
     * @param mixed $value
147
     *
148
     * @return void
149
     */
150 1
    public function offsetSet($key, $value)
151
    {
152 1
        $this->set($key, $value);
153 1
    }
154
155
    /**
156
     * @param mixed $key
157
     *
158
     * @return void
159
     */
160 1
    public function offsetUnset($key)
161
    {
162 1
        $this->remove($key);
163 1
    }
164
165 49
    protected function initialize() : void
166
    {
167 49
        if ($this->initialized === true) {
168 40
            return;
169
        }
170
171 49
        $this->initialized = true;
172
173 49
        $this->doInitialize();
174 49
    }
175
176
    /**
177
     * @param string|AbstractPart $value
178
     *
179
     * @return mixed
180
     */
181 41
    protected function preparePartValue(string $key, $value)
182
    {
183 41
        if (! isset($this->partClassMap[$key])) {
184 29
            return $value;
185
        }
186
187 41
        $className = $this->partClassMap[$key];
188
189 41
        return ! $value instanceof $className ? new $className($value) : $value;
190
    }
191
192
    abstract public function __toString() : string;
193
194
    abstract protected function doInitialize() : void;
195
}
196