Test Failed
Push — master ( ae28f9...7916f9 )
by Vsevolods
03:53
created

Route::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Routing;
4
5
use InvalidArgumentException;
6
use Venta\Contracts\Routing\Route as RouteContract;
7
8
/**
9
 * Class Route
10
 *
11
 * @package Venta\Routing
12
 */
13
class Route implements RouteContract
14
{
15
    /**
16
     * @var string
17
     */
18
    private $domain = '';
19
20
    /**
21
     * Host to apply route to.
22
     *
23
     * @var string
24
     */
25
    private $host = '';
26
27
    /**
28
     * @var string
29
     */
30
    private $input = '';
31
32
    /**
33
     * Route allowed methods.
34
     *
35
     * @var string[]
36
     */
37
    private $methods = [];
38
39
    /**
40
     * List of middleware class names.
41
     *
42
     * @var string[]
43
     */
44
    private $middlewares = [];
45
46
    /**
47
     * Route name.
48
     *
49
     * @var string
50
     */
51
    private $name = '';
52
53
    /**
54
     * Route path
55
     *
56
     * @var string
57
     */
58
    private $path = '';
59
60
    /**
61
     * @var string
62
     */
63
    private $responder;
64
65
    /**
66
     * Scheme to apply route to.
67
     *
68
     * @var string
69
     */
70
    private $scheme = '';
71
72
    /**
73
     * Route variables.
74
     *
75
     * @var string[]
76
     */
77
    private $variables = [];
78
79
    /**
80
     * Route constructor.
81
     *
82
     * @param array $methods
83
     * @param string $path
84
     * @param string $responder
85
     */
86
    public function __construct(array $methods, string $path, string $responder)
87
    {
88
        $this->methods = $methods;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
        $this->path = '/' . ltrim($path, '/');
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
        $this->responder = $responder;
91
    }
92
93
    /**
94
     * @param $path
95
     * @param callable|string $handler
96
     * @return Route
97
     */
98
    public static function any($path, $handler): Route
99
    {
100
        return new static(['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
101
    }
102
103
    /**
104
     * @param string $path
105
     * @param callable|string $handler
106
     * @return Route
107
     */
108
    public static function delete(string $path, $handler): Route
109
    {
110
        return new static(['DELETE'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
111
    }
112
113
    /**
114
     * @param string $path
115
     * @param callable|string $handler
116
     * @return Route
117
     */
118
    public static function get(string $path, $handler): Route
119
    {
120
        return new static(['GET'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
121
    }
122
123
    /**
124
     * @param string $path
125
     * @param callable|string $handler
126
     * @return Route
127
     */
128
    public static function head(string $path, $handler): Route
129
    {
130
        return new static(['HEAD'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
131
    }
132
133
    /**
134
     * @param string $path
135
     * @param callable|string $handler
136
     * @return Route
137
     */
138
    public static function options(string $path, $handler): Route
139
    {
140
        return new static(['OPTIONS'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
141
    }
142
143
    /**
144
     * @param string $path
145
     * @param callable|string $handler
146
     * @return Route
147
     */
148
    public static function patch(string $path, $handler): Route
149
    {
150
        return new static(['PATCH'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
151
    }
152
153
    /**
154
     * @param string $path
155
     * @param callable|string $handler
156
     * @return Route
157
     */
158
    public static function post(string $path, $handler): Route
159
    {
160
        return new static(['POST'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
161
    }
162
163
    /**
164
     * @param string $path
165
     * @param callable|string $handler
166
     * @return Route
167
     */
168
    public static function put(string $path, $handler): Route
169
    {
170
        return new static(['PUT'], $path, $handler);
0 ignored issues
show
Documentation introduced by
$handler is of type callable, but the function expects a string.

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...
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function compilePath(array $variables = []): string
177
    {
178
        $path = $this->getPath();
179
        foreach ($variables as $key => $value) {
180
            $pattern = sprintf('~%s~x', sprintf('\{\s*%s\s*(?::\s*([^{}]*(?:\{(?-1)\}[^{}]*)*))?\}', preg_quote($key)));
181
            preg_match($pattern, $path, $matches);
182
            if (isset($matches[1]) && !preg_match('/' . $matches[1] . '/', (string)$value)) {
183
                throw new InvalidArgumentException(
184
                    sprintf('Substitution value "%s" does not match "%s" parameter "%s" pattern.',
185
                        $value, $key, $matches[1]
186
                    )
187
                );
188
            }
189
            $path = preg_replace($pattern, $value, $path);
190
        }
191
        // 1. remove patterns for named prameters
192
        // 2. remove optional segments' ending delimiters
193
        // 3. split path into an array of optional segments and remove those
194
        //    containing unsubstituted parameters starting from the last segment
195
        $path = preg_replace('/{(\w+):(.+?)}/', '{$1}', $path);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
196
        $path = str_replace(']', '', $path);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
197
        $segments = array_reverse(explode('[', $path));
198
        foreach ($segments as $n => $segment) {
199
            if (strpos($segment, '{') !== false) {
200
                if (isset($segments[$n - 1])) {
201
                    throw new InvalidArgumentException(
202
                        'Optional segments with unsubstituted parameters cannot '
203
                        . 'contain segments with substituted parameters when using FastRoute'
204
                    );
205
                }
206
                unset($segments[$n]);
207
                if (count($segments) == 0) {
208
                    preg_match('/{.+}/', $segment, $params);
209
                    throw new InvalidArgumentException(
210
                        sprintf('Parameter "%s" is mandatory', $params[0] ?? $segment)
211
                    );
212
                }
213
            }
214
        }
215
        $path = implode('', array_reverse($segments));
216
217
        return $path;
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    public function getDomain(): string
224
    {
225
        return $this->domain;
226
    }
227
228
    /**
229
     * @inheritDoc
230
     */
231
    public function getHost(): string
232
    {
233
        return $this->host;
234
    }
235
236
    /**
237
     * @inheritDoc
238
     */
239
    public function getInput(): string
240
    {
241
        return $this->input;
242
    }
243
244
    /**
245
     * @inheritDoc
246
     */
247
    public function getMethods(): array
248
    {
249
        return $this->methods;
250
    }
251
252
    /**
253
     * @inheritDoc
254
     */
255
    public function getMiddlewares(): array
256
    {
257
        return $this->middlewares;
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    public function getName(): string
264
    {
265
        return $this->name;
266
    }
267
268
    /**
269
     * @inheritDoc
270
     */
271
    public function getPath(): string
272
    {
273
        return $this->path;
274
    }
275
276
    /**
277
     * @inheritDoc
278
     */
279
    public function getResponder(): string
280
    {
281
        return $this->responder;
282
    }
283
284
    /**
285
     * @inheritDoc
286
     */
287
    public function getScheme(): string
288
    {
289
        return $this->scheme;
290
    }
291
292
    /**
293
     * @inheritDoc
294
     */
295
    public function getVariables(): array
296
    {
297
        return $this->variables;
298
    }
299
300
    /**
301
     * @inheritDoc
302
     */
303
    public function secure(): RouteContract
304
    {
305
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
306
        $route->scheme = 'https';
307
308
        return $route;
309
    }
310
311
    /**
312
     * @inheritDoc
313
     */
314
    public function withDomain(string $domainClass): RouteContract
315
    {
316
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
317
        $route->domain = $domainClass;
318
319
        return $route;
320
    }
321
322
    /**
323
     * @inheritDoc
324
     */
325
    public function withHost(string $host): RouteContract
326
    {
327
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
328
        $route->host = $host;
329
330
        return $route;
331
    }
332
333
    /**
334
     * @inheritDoc
335
     */
336
    public function withInput(string $inputClass): RouteContract
337
    {
338
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
339
        $route->input = $inputClass;
340
341
        return $route;
342
    }
343
344
    /**
345
     * @inheritDoc
346
     */
347
    public function withMiddleware(string $middleware): RouteContract
348
    {
349
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
350
        $route->middlewares[] = $middleware;
351
352
        return $route;
353
    }
354
355
    /**
356
     * @inheritDoc
357
     */
358
    public function withName(string $name): RouteContract
359
    {
360
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
361
        $route->name = $name;
362
363
        return $route;
364
    }
365
366
    /**
367
     * @inheritDoc
368
     */
369
    public function withPathPrefix(string $prefix): RouteContract
370
    {
371
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
372
        $route->path = $prefix == '/' || $prefix == '' ?
373
            $route->path :
374
            sprintf('/%s/%s', trim($prefix, '/'), ltrim($route->path, '/'));
375
376
        return $route;
377
    }
378
379
    /**
380
     * @inheritDoc
381
     */
382
    public function withVariables(array $variables): RouteContract
383
    {
384
        $route = clone $this;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
385
        $route->variables = $variables;
386
387
        return $route;
388
    }
389
390
}