Passed
Pull Request — master (#63)
by Anatoly
02:19
created

Route::assertParamsContainValidPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Annotation;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException;
19
use Sunrise\Http\Router\RouteDescriptorInterface;
20
21
/**
22
 * Import functions
23
 */
24
use function is_array;
25
use function is_int;
26
use function is_string;
27
use function is_subclass_of;
28
29
/**
30
 * Annotation for a route description
31
 *
32
 * @Annotation
33
 *
34
 * @Target({"CLASS"})
35
 */
36
final class Route implements RouteDescriptorInterface
37
{
38
39
    /**
40
     * A route name
41
     *
42
     * @var string
43
     */
44
    private $name;
45
46
    /**
47
     * A route host
48
     *
49
     * @var null|string
50
     */
51
    private $host;
52
53
    /**
54
     * A route path
55
     *
56
     * @var string
57
     */
58
    private $path;
59
60
    /**
61
     * A route methods
62
     *
63
     * @var string[]
64
     */
65
    private $methods;
66
67
    /**
68
     * A route middlewares
69
     *
70
     * @var string[]
71
     */
72
    private $middlewares;
73
74
    /**
75
     * A route attributes
76
     *
77
     * @var array
78
     */
79
    private $attributes;
80
81
    /**
82
     * A route summary
83
     *
84
     * @var string
85
     */
86
    private $summary;
87
88
    /**
89
     * A route description
90
     *
91
     * @var string
92
     */
93
    private $description;
94
95
    /**
96
     * A route tags
97
     *
98
     * @var string[]
99
     */
100
    private $tags;
101
102
    /**
103
     * A route priority
104
     *
105
     * @var int
106
     */
107
    private $priority;
108
109
    /**
110
     * Constructor of the annotation
111
     *
112
     * @param array $params
113
     *
114
     * @throws InvalidDescriptorArgumentException
115
     */
116 136
    public function __construct(array $params)
117
    {
118 136
        $this->name = $this->extractNameFromParams($params);
119 123
        $this->host = $this->extractHostFromParams($params);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->host is correct as $this->extractHostFromParams($params) targeting Sunrise\Http\Router\Anno...extractHostFromParams() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120 113
        $this->path = $this->extractPathFromParams($params);
121 100
        $this->methods = $this->extractMethodsFromParams($params);
122 78
        $this->middlewares = $this->extractMiddlewaresFromParams($params);
123 56
        $this->attributes = $this->extractAttributesFromParams($params);
124 47
        $this->summary = $this->extractSummaryFromParams($params);
125 39
        $this->description = $this->extractDescriptionFromParams($params);
126 32
        $this->tags = $this->extractTagsFromParams($params);
127 16
        $this->priority = $this->extractPriorityFromParams($params);
128 7
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 7
    public function getName() : string
134
    {
135 7
        return $this->name;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141 7
    public function getHost() : ?string
142
    {
143 7
        return $this->host;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 7
    public function getPath() : string
150
    {
151 7
        return $this->path;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 7
    public function getMethods() : array
158
    {
159 7
        return $this->methods;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 6
    public function getMiddlewares() : array
166
    {
167 6
        return $this->middlewares;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 6
    public function getAttributes() : array
174
    {
175 6
        return $this->attributes;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 6
    public function getSummary() : string
182
    {
183 6
        return $this->summary;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 6
    public function getDescription() : string
190
    {
191 6
        return $this->description;
192
    }
193
194
    /**
195
     * {@inheritDoc}
196
     */
197 6
    public function getTags() : array
198
    {
199 6
        return $this->tags;
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205 4
    public function getPriority() : int
206
    {
207 4
        return $this->priority;
208
    }
209
210
    /**
211
     * @param array $params
212
     *
213
     * @return string
214
     *
215
     * @throws InvalidDescriptorArgumentException
216
     */
217 136
    private function extractNameFromParams(array $params) : string
218
    {
219 136
        $name = $params['name'] ?? '';
220
221 136
        if ('' === $name || !is_string($name)) {
222 13
            throw new InvalidDescriptorArgumentException('@Route.name must contain a non-empty string.');
223
        }
224
225 123
        return $name;
226
    }
227
228
    /**
229
     * @param array $params
230
     *
231
     * @return null|string
232
     *
233
     * @throws InvalidDescriptorArgumentException
234
     */
235 123
    private function extractHostFromParams(array $params) : ?string
236
    {
237 123
        $host = $params['host'] ?? null;
238
239 123
        if (isset($host) && ('' === $host || !is_string($host))) {
240 10
            throw new InvalidDescriptorArgumentException('@Route.host must contain a non-empty string.');
241
        }
242
243 113
        return $host;
244
    }
245
246
    /**
247
     * @param array $params
248
     *
249
     * @return string
250
     *
251
     * @throws InvalidDescriptorArgumentException
252
     */
253 113
    private function extractPathFromParams(array $params) : string
254
    {
255 113
        $path = $params['path'] ?? '';
256
257 113
        if ('' === $path || !is_string($path)) {
258 13
            throw new InvalidDescriptorArgumentException('@Route.path must contain a non-empty string.');
259
        }
260
261 100
        return $path;
262
    }
263
264
    /**
265
     * @param array $params
266
     *
267
     * @return string[]
268
     *
269
     * @throws InvalidDescriptorArgumentException
270
     */
271 100
    private function extractMethodsFromParams(array $params) : array
272
    {
273 100
        $methods = $params['methods'] ?? [];
274
275 100
        if ([] === $methods || !is_array($methods)) {
276 13
            throw new InvalidDescriptorArgumentException('@Route.methods must contain a non-empty array.');
277
        }
278
279 87
        foreach ($methods as $value) {
280 87
            if (!is_string($value)) {
281 9
                throw new InvalidDescriptorArgumentException('@Route.methods must contain strings.');
282
            }
283
        }
284
285 78
        return $methods;
286
    }
287
288
    /**
289
     * @param array $params
290
     *
291
     * @return string[]
292
     *
293
     * @throws InvalidDescriptorArgumentException
294
     */
295 78
    private function extractMiddlewaresFromParams(array $params) : array
296
    {
297 78
        $middlewares = $params['middlewares'] ?? [];
298
299 78
        if (!is_array($middlewares)) {
300 9
            throw new InvalidDescriptorArgumentException('@Route.middlewares must contain an array.');
301
        }
302
303 69
        foreach ($middlewares as $value) {
304 17
            if (!is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
305 13
                throw new InvalidDescriptorArgumentException(
306 13
                    '@Route.middlewares must contain the class names of existing middlewares.'
307
                );
308
            }
309
        }
310
311 56
        return $middlewares;
312
    }
313
314
    /**
315
     * @param array $params
316
     *
317
     * @return array
318
     *
319
     * @throws InvalidDescriptorArgumentException
320
     */
321 56
    private function extractAttributesFromParams(array $params) : array
322
    {
323 56
        $attributes = $params['attributes'] ?? [];
324
325 56
        if (!is_array($attributes)) {
326 9
            throw new InvalidDescriptorArgumentException('@Route.attributes must contain an array.');
327
        }
328
329 47
        return $attributes;
330
    }
331
332
    /**
333
     * @param array $params
334
     *
335
     * @return string
336
     *
337
     * @throws InvalidDescriptorArgumentException
338
     */
339 47
    private function extractSummaryFromParams(array $params) : string
340
    {
341 47
        $summary = $params['summary'] ?? '';
342
343 47
        if (!is_string($summary)) {
344 8
            throw new InvalidDescriptorArgumentException('@Route.summary must contain a string.');
345
        }
346
347 39
        return $summary;
348
    }
349
350
    /**
351
     * @param array $params
352
     *
353
     * @return string
354
     *
355
     * @throws InvalidDescriptorArgumentException
356
     */
357 39
    private function extractDescriptionFromParams(array $params) : string
358
    {
359 39
        $description = $params['description'] ?? '';
360
361 39
        if (!is_string($description)) {
362 7
            throw new InvalidDescriptorArgumentException('@Route.description must contain a string.');
363
        }
364
365 32
        return $description;
366
    }
367
368
    /**
369
     * @param array $params
370
     *
371
     * @return string[]
372
     *
373
     * @throws InvalidDescriptorArgumentException
374
     */
375 32
    private function extractTagsFromParams(array $params) : array
376
    {
377 32
        $tags = $params['tags'] ?? [];
378
379 32
        if (!is_array($tags)) {
380 8
            throw new InvalidDescriptorArgumentException('@Route.tags must contain an array.');
381
        }
382
383 24
        foreach ($tags as $value) {
384 11
            if (!is_string($value)) {
385 8
                throw new InvalidDescriptorArgumentException('@Route.tags must contain strings.');
386
            }
387
        }
388
389 16
        return $tags;
390
    }
391
392
    /**
393
     * @param array $params
394
     *
395
     * @return int
396
     *
397
     * @throws InvalidDescriptorArgumentException
398
     */
399 16
    private function extractPriorityFromParams(array $params) : int
400
    {
401 16
        $priority = $params['priority'] ?? 0;
402
403 16
        if (!is_int($priority)) {
404 9
            throw new InvalidDescriptorArgumentException('@Route.priority must contain an integer.');
405
        }
406
407 7
        return $priority;
408
    }
409
}
410