Completed
Push — master ( 11b479...0fd37c )
by Mikael
02:25
created

src/Route/Route.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Anax\Route;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Route\Exception\ConfigurationException;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Route to match a $path, mounted on $mount having a $handler to call.
11
 */
12
class Route
13
{
14
    /**
15
     * @var string       $name          a name for this route.
16
     * @var string       $info          description of route.
17
     * @var array        $method        the method(s) to support
18
     * @var string       $methodMatched the matched method.
19
     * @var string       $mount         where to mount the path
20
     * @var string       $path          the path rule for this route
21
     * @var string       $pathMatched   the matched path.
22
     * @var callable     $handler       the callback to handle this route
23
     * @var null|array   $arguments     arguments for the callback, extracted
24
     *                                  from path
25
     */
26
    private $name;
27
    private $info;
28
    private $method;
29
    private $methodMatched;
30
    private $mount;
31
    private $path;
32
    private $pathMatched;
33
    private $handler;
34
    private $arguments = [];
35
36
37
38
    /**
39
     * Set values for route.
40
     *
41
     * @param string|array           $method  as request method to support
42
     * @param string                 $mount   where to mount the path
43
     * @param string                 $path    for this route
44
     * @param string|array|callable  $handler for this path, callable or equal
45
     * @param string                 $info    description of the route
46
     *
47
     * @return $this
48
     */
49 147
    public function set(
50
        $method = null,
51
        $mount = null,
52
        $path = null,
53
        $handler = null,
54
        string $info = null
55
    ) : object {
56 147
        $this->mount = rtrim($mount, "/");
57 147
        $this->path = $path;
58 147
        $this->handler = $handler;
59 147
        $this->info = $info;
60
61 147
        $this->method = $method;
62 147
        if (is_string($method)) {
63 22
            $this->method = array_map("trim", explode("|", $method));
64
        }
65 147
        if (is_array($this->method)) {
66 64
            $this->method = array_map("strtoupper", $this->method);
67
        }
68
69 147
        return $this;
70
    }
71
72
73
74
    /**
75
     * Check if the route matches a query and request method.
76
     *
77
     * @param string $query  to match against
78
     * @param string $method as request method
79
     *
80
     * @return boolean true if query matches the route
81
     */
82 132
    public function match(string $query, string $method = null)
83
    {
84 132
        $this->arguments = [];
85 132
        $this->methodMatched = null;
86 132
        $this->pathMatched = null;
87
88 132
        $matcher = new RouteMatcher();
89 132
        $res = $matcher->match(
90 132
            $this->getAbsolutePath(),
91 132
            $query,
92 132
            $this->method,
93 132
            $method
94
        );
95 132
        $this->arguments = $matcher->arguments;
0 ignored issues
show
Documentation Bug introduced by
It seems like $matcher->arguments can be null. However, the property $arguments is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
96 132
        $this->methodMatched = $matcher->methodMatched;
97 132
        $this->pathMatched = $matcher->pathMatched;
98
99 132
        return $res;
100
    }
101
102
103
104
    /**
105
     * Handle the action for the route.
106
     *
107
     * @param string                       $path the matched path
108
     * @param ContainerInjectableInterface $di   container with services
109
     *
110
     * @return mixed
111
     */
112 121
    public function handle(
113
        string $path = null,
114
        ContainerInterface $di = null
115
    ) {
116 121 View Code Duplication
        if ($this->mount) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
            // Remove the mount path to get base for controller
118 13
            $len = strlen($this->mount);
119 13
            if (substr($path, 0, $len) == $this->mount) {
120 13
                $path = ltrim(substr($path, $len), "/");
121
            }
122
        }
123
124 121
        $handler = new RouteHandler();
125 121
        return $handler->handle($this->methodMatched, $path, $this->handler, $this->arguments, $di);
126
    }
127
128
129
130
    /**
131
     * Get the matched basename of the path, its the part without the mount
132
     * point.
133
     *
134
     * @return string|null
135
     */
136 3
    public function getMatchedPath()
137
    {
138 3
        $path = $this->pathMatched;
139 3 View Code Duplication
        if ($this->mount) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140 1
            $len = strlen($this->mount);
141 1
            if (substr($path, 0, $len) == $this->mount) {
142 1
                $path = ltrim(substr($path, $len), "/");
143
            }
144
        }
145
146 3
        return $path;
147
    }
148
149
150
151
    /**
152
     * Set the name of the route.
153
     *
154
     * @param string $name set a name for the route
155
     *
156
     * @return $this
157
     */
158 1
    public function setName($name)
159
    {
160 1
        $this->name = $name;
161 1
        return $this;
162
    }
163
164
165
166
    /**
167
     * Get information of the route.
168
     *
169
     * @return null|string as route information.
170
     */
171 2
    public function getInfo()
172
    {
173 2
        return $this->info;
174
    }
175
176
177
178
    /**
179
     * Get the path for the route.
180
     *
181
     * @return string
182
     */
183 1
    public function getPath()
184
    {
185 1
        return $this->path;
186
    }
187
188
189
190
    /**
191
     * Get the absolute $path by adding $mount.
192
     *
193
     * @return string|null as absolute path for this route.
194
     */
195 132
    public function getAbsolutePath()
196
    {
197 132
        if (is_null($this->path)) {
198 71
            return null;
199
        }
200
201 96
        if (empty($this->mount)) {
202 94
            return $this->path;
203
        }
204
205 2
        return $this->mount . "/" . $this->path;
206
    }
207
208
209
210
    /**
211
     * Get the request method for the route.
212
     *
213
     * @return string representing the request method supported
214
     */
215 1
    public function getRequestMethod() : string
216
    {
217 1
        return is_array($this->method)
218 1
            ? implode("|", $this->method)
219 1
            : "";
220
    }
221
}
222