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

Attachable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 49
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 6 1
A initializer() 0 4 1
A checkProperty() 0 4 3
A attached() 0 4 2
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