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

Rule   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 197
ccs 40
cts 65
cp 0.6153
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getProtocol() 0 3 1
A hinge() 0 8 3
A pathInit() 0 6 3
A getHost() 0 3 1
A jsonSerialize() 0 8 2
A __construct() 0 7 2
A getName() 0 3 1
A afterPrepare() 0 10 1
A serialize() 0 3 1
A getDefaults() 0 3 1
A serializeArray() 0 9 2
A getMethods() 0 3 1
A unserialize() 0 5 2
A getPath() 0 3 1
A prepare() 0 4 1
1
<?php
2
3
namespace Bavix\Router;
4
5
abstract class Rule implements \Serializable, \JsonSerializable
6
{
7
8
    use Attachable;
9
10
    /**
11
     * @var string
12
     */
13
    protected $_protocol;
14
15
    /**
16
     * @var string
17
     */
18
    protected $_host;
19
20
    /**
21
     * @var null|string
22
     */
23
    protected $type;
24
25
    /**
26
     * @var null|Path
27
     */
28
    protected $path;
29
30
    /**
31
     * @var null|array
32
     */
33
    protected $methods;
34
35
    /**
36
     * @var array
37
     */
38
    protected $defaults;
39
40
    /**
41
     * Rule constructor.
42
     *
43
     * @param string $key
44
     * @param iterable $storage
45
     * @param null|self $parent
46
     */
47 5
    public function __construct(string $key, $storage, ?self $parent = null)
48
    {
49 5
        $this->prepare();
50 5
        $this->initializer($key, $storage);
0 ignored issues
show
Bug introduced by
$storage of type iterable is incompatible with the type array expected by parameter $storage of Bavix\Router\Rule::initializer(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $this->initializer($key, /** @scrutinizer ignore-type */ $storage);
Loading history...
51 5
        $this->pathInit();
52 5
        if ($parent) {
53 2
            $this->afterPrepare($parent);
54
        }
55 5
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getName(): string
61
    {
62
        return $this->_name;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 3
    public function getProtocol(): string
69
    {
70 3
        return $this->_protocol;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 3
    public function getHost(): string
77
    {
78 3
        return $this->_host;
79
    }
80
81
    /**
82
     * @return Path
83
     */
84 3
    public function getPath(): Path
85
    {
86 3
        return $this->path;
87
    }
88
89
    /**
90
     * @return null|array
91
     */
92 4
    public function getMethods(): ?array
93
    {
94 4
        return $this->methods;
95
    }
96
97
    /**
98
     * @return array
99
     */
100 4
    public function getDefaults(): array
101
    {
102 4
        return (array)$this->defaults;
103
    }
104
105
    /**
106
     * @param Path|null $path
107
     */
108 2
    protected function hinge(?Path $path): void
109
    {
110 2
        if ($this->path && $path) {
111 2
            $this->path->hinge($path);
112 2
            return;
113
        }
114
115
        $this->path = $path ?? $this->path;
116
    }
117
118
    /**
119
     * @param self $parent
120
     * @return void
121
     */
122 2
    protected function afterPrepare(self $parent): void
123
    {
124 2
        $this->hinge($parent->path);
125 2
        $this->_protocol = $parent->protocol ?? $parent->_protocol ?? $this->_protocol;
126 2
        $this->_host = $parent->host ?? $parent->_host ?? $this->_host;
127 2
        $this->_name = $parent->_name . '.' . $this->_name;
128 2
        $this->methods = $this->methods ?? $parent->methods;
129 2
        $this->defaults = \array_merge(
130 2
            (array)$parent->defaults,
131 2
            (array)$this->defaults
132
        );
133 2
    }
134
135
    /**
136
     * if this.path === string then new Path(string, [])
137
     * else this.path === array then new Path(...this.path)
138
     */
139 5
    protected function pathInit(): void
140
    {
141 5
        if (\is_string($this->path)) {
0 ignored issues
show
introduced by
The condition is_string($this->path) is always false.
Loading history...
142 4
            $this->path = new Path($this->path);
143 1
        } elseif (\is_array($this->path)) {
0 ignored issues
show
introduced by
The condition is_array($this->path) is always false.
Loading history...
144
            $this->path = new Path(...$this->path);
145
        }
146 5
    }
147
148
    /**
149
     * @return void
150
     */
151 5
    protected function prepare(): void
152
    {
153 5
        $this->_protocol = '\w+';
154 5
        $this->_host = '[^\/]+';
155 5
    }
156
157
    /**
158
     * @return array
159
     * @throws
160
     */
161
    protected function serializeArray(): array
162
    {
163
        $properties = [];
164
        $reflation = new \ReflectionClass($this);
165
        $protected = $reflation->getProperties(\ReflectionProperty::IS_PROTECTED);
166
        foreach ($protected as $property) {
167
            $properties[] = $property->name;
168
        }
169
        return $properties;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    public function serialize(): string
176
    {
177
        return \serialize($this->serializeArray());
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183
    public function unserialize($serialized): void
184
    {
185
        $data = \unserialize($serialized, null);
186
        foreach ($data as $key => $value) {
187
            $this->{$key} = $value;
188
        }
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194
    public function jsonSerialize(): array
195
    {
196
        $data = [];
197
        foreach ($this->serializeArray() as $key => $value) {
198
            $key = \ltrim($value, '_');
199
            $data[$key] = $this->$value;
200
        }
201
        return $data;
202
    }
203
204
}
205