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

Rule   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 88
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hinge() 0 8 3
A pathInit() 0 6 3
A __construct() 0 7 2
A afterPrepare() 0 8 1
A prepare() 0 2 1
1
<?php
2
3
namespace Bavix\Router;
4
5
abstract class Rule
6
{
7
8
    use Attachable;
9
10
    /**
11
     * @var null|string
12
     */
13
    protected $type;
14
15
    /**
16
     * @var null|Path
17
     */
18
    protected $path;
19
20
    /**
21
     * @var array
22
     */
23
    protected $methods;
24
25
    /**
26
     * @var array
27
     */
28
    protected $defaults;
29
30
    /**
31
     * Rule constructor.
32
     *
33
     * @param string $key
34
     * @param array $storage
35
     * @param null|self $parent
36
     */
37
    public function __construct(string $key, array $storage, ?self $parent = null)
38
    {
39
        $this->prepare();
40
        $this->initializer($key, $storage);
41
        $this->pathInit();
42
        if ($parent) {
43
            $this->afterPrepare($parent);
44
        }
45
    }
46
47
    /**
48
     * @param Path|null $path
49
     */
50
    protected function hinge(?Path $path): void
51
    {
52
        if ($this->path && $path) {
53
            $this->path->hinge($path);
54
            return;
55
        }
56
57
        $this->path = $path ?? $this->path;
58
    }
59
60
    /**
61
     * @param self $parent
62
     * @return void
63
     */
64
    protected function afterPrepare(self $parent): void
65
    {
66
        $this->hinge($parent->path);
67
        $this->_key = $parent->_key . '.' . $this->_key;
68
        $this->methods = $this->methods ?? $parent->methods;
69
        $this->defaults = \array_merge(
70
            (array)$parent->defaults,
71
            (array)$this->defaults
72
        );
73
    }
74
75
    /**
76
     * if this.path === string then new Path(string, [])
77
     * else this.path === array then new Path(...this.path)
78
     */
79
    protected function pathInit(): void
80
    {
81
        if (\is_string($this->path)) {
0 ignored issues
show
introduced by
The condition is_string($this->path) is always false.
Loading history...
82
            $this->path = new Path($this->path);
83
        } elseif (\is_array($this->path)) {
0 ignored issues
show
introduced by
The condition is_array($this->path) is always false.
Loading history...
84
            $this->path = new Path(...$this->path);
85
        }
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    protected function prepare(): void
92
    {
93
        // todo: a lot of logic
94
    }
95
96
}
97