Completed
Push — master ( 41c9a2...969b43 )
by Mikael
01:54
created

Route::getHandlerType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 163
    public function set(
50
        $method = null,
51
        $mount = null,
52
        $path = null,
53
        $handler = null,
54
        string $info = null
55
    ) : object {
56 163
        $this->mount = rtrim($mount, "/");
57 163
        $this->path = $path;
58 163
        $this->handler = $handler;
59 163
        $this->info = $info;
60
61 163
        $this->method = $method;
62 163
        if (is_string($method)) {
63 22
            $this->method = array_map("trim", explode("|", $method));
64
        }
65 163
        if (is_array($this->method)) {
66 64
            $this->method = array_map("strtoupper", $this->method);
67
        }
68
69 163
        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 146
    public function match(string $query, string $method = null)
83
    {
84 146
        $this->arguments = [];
85 146
        $this->methodMatched = null;
86 146
        $this->pathMatched = null;
87
88 146
        $matcher = new RouteMatcher();
89 146
        $res = $matcher->match(
90 146
            $this->mount,
91 146
            $this->path,
92 146
            $this->getAbsolutePath(),
93 146
            $query,
94 146
            $this->method,
0 ignored issues
show
Bug introduced by
It seems like $this->method can also be of type string; however, Anax\Route\RouteMatcher::match() does only seem to accept null|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95 146
            $method
96
        );
97 146
        $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...
98 146
        $this->methodMatched = $matcher->methodMatched;
99 146
        $this->pathMatched = $matcher->pathMatched;
100
101 146
        return $res;
102
    }
103
104
105
106
    /**
107
     * Handle the action for the route.
108
     *
109
     * @param string                       $path the matched path
110
     * @param ContainerInjectableInterface $di   container with services
111
     *
112
     * @return mixed
113
     */
114 141
    public function handle(
115
        string $path = null,
116
        ContainerInterface $di = null
117
    ) {
118 141 View Code Duplication
        if ($this->mount) {
0 ignored issues
show
Duplication introduced by
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...
119
            // Remove the mount path to get base for controller
120 24
            $len = strlen($this->mount);
121 24
            if (substr($path, 0, $len) == $this->mount) {
122 24
                $path = ltrim(substr($path, $len), "/");
123
            }
124
        }
125
126
        try {
127 141
            $handler = new RouteHandler();
128 141
            return $handler->handle($this->methodMatched, $path, $this->handler, $this->arguments, $di);
0 ignored issues
show
Documentation introduced by
$this->handler is of type callable|null, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129 21
        } catch (ConfigurationException $e) {
130 4
            throw new ConfigurationException(
131 4
                $e->getMessage()
132 4
                . " Route matched method='{$this->methodMatched}', mount='{$this->mount}', path='$path', handler='"
133
                . (
134 4
                    is_string($this->handler)
135 2
                        ? "$this->handler"
136 4
                        : "array/object"
137
                )
138 4
                . "'."
139
            );
140
        }
141
    }
142
143
144
145
    /**
146
     * Set the path that matched this route.
147
     *
148
     * @param string $path to set
149
     *
150
     * @return $this
151
     */
152 21
    public function setMatchedPath($path)
153
    {
154 21
        $this->pathMatched = $path;
155 21
        return $this;
156
    }
157
158
159
160
    /**
161
     * Get the matched basename of the path, its the part without the mount
162
     * point.
163
     *
164
     * @return string|null
165
     */
166 3
    public function getMatchedPath()
167
    {
168 3
        $path = $this->pathMatched;
169 3 View Code Duplication
        if ($this->mount) {
0 ignored issues
show
Duplication introduced by
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...
170 1
            $len = strlen($this->mount);
171 1
            if (substr($path, 0, $len) == $this->mount) {
172 1
                $path = ltrim(substr($path, $len), "/");
173
            }
174
        }
175
176 3
        return $path;
177
    }
178
179
180
181
    /**
182
     * Set the name of the route.
183
     *
184
     * @param string $name set a name for the route
185
     *
186
     * @return $this
187
     */
188 1
    public function setName($name)
189
    {
190 1
        $this->name = $name;
191 1
        return $this;
192
    }
193
194
195
196
    /**
197
     * Get information of the route.
198
     *
199
     * @return null|string as route information.
200
     */
201 2
    public function getInfo()
202
    {
203 2
        return $this->info;
204
    }
205
206
207
208
    /**
209
     * Get the path for the route.
210
     *
211
     * @return string
212
     */
213 1
    public function getPath()
214
    {
215 1
        return $this->path;
216
    }
217
218
219
220
    /**
221
     * Get the absolute $path by adding $mount.
222
     *
223
     * @return string|null as absolute path for this route.
224
     */
225 146
    public function getAbsolutePath()
226
    {
227 146
        if (is_null($this->mount) && is_null($this->path)) {
228
            return null;
229
        }
230
231 146
        if (empty($this->mount)) {
232 126
            return $this->path;
233
        }
234
235 24
        return $this->mount . "/" . $this->path;
236
    }
237
238
239
240
    /**
241
     * Get the request method for the route.
242
     *
243
     * @return string representing the request method supported
244
     */
245 1
    public function getRequestMethod() : string
246
    {
247 1
        return is_array($this->method)
248 1
            ? implode("|", $this->method)
249 1
            : "";
250
    }
251
252
253
254
    /**
255
     * Get the handler type as a informative string.
256
     *
257
     * @param ContainerInjectableInterface $di container with services
258
     *
259
     * @return string representing the handler.
260
     */
261
    public function getHandlerType(ContainerInterface $di = null) : string
262
    {
263
        $handler = new RouteHandler();
264
        return $handler->getHandlerType($this->handler, $di);
0 ignored issues
show
Documentation introduced by
$this->handler is of type callable|null, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
265
    }
266
}
267