Passed
Push — master ( dd3fe0...2f26d2 )
by Zlatin
02:38
created

Route   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 30.56%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 11
cts 36
cp 0.3056
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B compile() 0 30 5
A getRegEx() 0 3 1
A __construct() 0 6 1
A getPattern() 0 3 1
A getParameters() 0 3 1
A getCallback() 0 3 1
1
<?php
2
namespace DevOp\Core\Router;
3
4
class Route
5
{
6
7
    /**
8
     * @var string
9
     */
10
    private $pattern;
11
12
    /**
13
     * @var string
14
     */
15
    private $regEx;
16
17
    /**
18
     * @var array
19
     */
20
    private $parameters = [];
21
22
    /**
23
     * @var object|callback|array
24
     */
25
    private $callback;
26
27
    /**
28
     * @param string $name
29
     * @param string $pattern
30
     * @param object|callback|array $callback
31
     */
32 4
    public function __construct($pattern, $callback)
33
    {
34 4
        $this->pattern = $pattern;
35 4
        $this->callback = $callback;
36
        
37 4
        $this->compile($pattern);
38 4
    }
39
40
    /**
41
     * @return stringsssss
0 ignored issues
show
Bug introduced by
The type DevOp\Core\Router\stringsssss was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
     */
43
    public function getRegEx()
44
    {
45
        return $this->regEx;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->regEx returns the type string which is incompatible with the documented return type DevOp\Core\Router\stringsssss.
Loading history...
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getPattern()
52
    {
53
        return $this->pattern;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function getParameters()
60
    {
61
        return $this->getParameters();
62
    }
63
64
    /**
65
     * @return object|callback|array
66
     */
67
    public function getCallback()
68
    {
69
        return $this->callback;
70
    }
71
72 4
    public function compile($pattern)
73
    {
74 4
        $matches = [];
75 4
        preg_match_all('#\[(.*?)\]#s', $pattern, $matches, PREG_SET_ORDER);
76
77 4
        if (empty($matches)) {
78 4
            $this->regEx = "#^{$pattern}$#s";
79 4
            return;
80
        }
81
82
        $components = array_map(function($values) {
83
            return ['name' => $values[0], 'value' => $values[1]];
84
        }, $matches);
85
86
        $replaces = $parameters = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $parameters is dead and can be removed.
Loading history...
87
        foreach ($components AS $route) {
88
            $placeholder = ltrim($route['value'], '/');
89
            $optional = substr($route['value'], 0, 1) === '/';
90
            if (strchr($placeholder, ':')) {
91
                list($parameter, $value) = explode(':', $placeholder);
92
                $regEx = "(?P<$parameter>{$value})";
93
                $this->parameters[] = $parameter;
94
            } else {
95
                $regEx = "(?P<$placeholder>[^/]++)";
96
                $this->parameters[] = $placeholder;
97
            }
98
            $replaces[$route['name']] = !$optional ? $regEx : "(?:/{$regEx})";
99
        }
100
101
        $this->regEx = sprintf("#^%s?$#s", str_replace(array_keys($replaces), array_values($replaces), $pattern));
102
    }
103
}
104