Passed
Pull Request — master (#3)
by Бабичев
02:05
created

Attachable::checkProperty()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 2
nop 1
crap 3.3332
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions\Runtime;
6
7
trait Attachable
8
{
9
10
    /**
11
     * @var string
12
     */
13
    protected $_name;
14
15
    /**
16
     * Attachable constructor.
17
     *
18
     * @param string $key
19
     * @param array $storage
20
     */
21 5
    protected function initializer(string $key, array $storage): void
22
    {
23 5
        $this->_name = $key;
24 5
        $this->attached($storage);
25 5
    }
26
27
    /**
28
     * @param array $storage
29
     * @return array
30
     */
31
    protected function filter(array $storage): array
32
    {
33 5
        return \array_filter($storage, function (string $key) {
34 5
            $this->checkProperty($key);
35 5
            return $key{0} !== '@'; // for xml
36 5
        }, \ARRAY_FILTER_USE_KEY);
37
    }
38
39
    /**
40
     * @param string $key
41
     */
42 5
    protected function checkProperty(string $key): void
43
    {
44 5
        if ($key{0} === '_' || !\property_exists($this, $key)) {
45
            throw new Runtime(\sprintf('The key `%s` is not registered', $key));
46
        }
47 5
    }
48
49
    /**
50
     * @param array $storage
51
     */
52 5
    protected function attached(array $storage): void
53
    {
54 5
        foreach ($this->filter($storage) as $key => $value) {
55 5
            $this->$key = $value;
56
        }
57 5
    }
58
59
}
60