Passed
Pull Request — master (#125)
by Rustam
15:41 queued 08:14
created

Route::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 2
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use RuntimeException;
8
use Yiisoft\Http\Method;
9
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
10
11
/**
12
 * Route defines a mapping from URL to callback / name and vice versa.
13
 */
14
final class Route implements RouteInterface, RouteParametersInterface
15
{
16
    private ?string $name = null;
17
    /** @var string[] */
18
    private array $methods;
19
    private string $pattern;
20
    private ?string $host = null;
21
    private bool $override = false;
22
    private ?MiddlewareDispatcher $dispatcher;
23
    private bool $actionAdded = false;
24
    private array $middlewareDefinitions = [];
25
    private array $disabledMiddlewareDefinitions = [];
26
    private array $defaults = [];
27
28 43
    private function __construct(?MiddlewareDispatcher $dispatcher = null)
29
    {
30 43
        $this->dispatcher = $dispatcher;
31 43
    }
32
33 5
    public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
34
    {
35 5
        $this->dispatcher = $dispatcher;
36 5
    }
37
38
    /**
39
     * @return self
40
     */
41 5
    public function withDispatcher(MiddlewareDispatcher $dispatcher): RouteInterface
42
    {
43 5
        $route = clone $this;
44 5
        $route->dispatcher = $dispatcher;
45 5
        return $route;
46
    }
47
48 10
    public function getDispatcherWithMiddlewares(): MiddlewareDispatcher
49
    {
50 10
        if ($this->dispatcher->hasMiddlewares()) {
0 ignored issues
show
Bug introduced by
The method hasMiddlewares() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        if ($this->dispatcher->/** @scrutinizer ignore-call */ hasMiddlewares()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            return $this->dispatcher;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dispatcher could return the type null which is incompatible with the type-hinted return Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
52
        }
53
54 10
        foreach ($this->middlewareDefinitions as $index => $definition) {
55 10
            if (in_array($definition, $this->disabledMiddlewareDefinitions, true)) {
56
                unset($this->middlewareDefinitions[$index]);
57
            }
58
        }
59
60 10
        return $this->dispatcher = $this->dispatcher->withMiddlewares($this->middlewareDefinitions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dispatcher...>middlewareDefinitions) could return the type null which is incompatible with the type-hinted return Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
61
    }
62
63 20
    public function hasDispatcher(): bool
64
    {
65 20
        return $this->dispatcher !== null;
66
    }
67
68
    /**
69
     * @param string $pattern
70
     * @param MiddlewareDispatcher|null $dispatcher
71
     *
72
     * @return RouteInterface
73
     */
74 33
    public static function get(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
75
    {
76 33
        return self::methods([Method::GET], $pattern, $dispatcher);
77
    }
78
79
    /**
80
     * @param string $pattern
81
     * @param MiddlewareDispatcher|null $dispatcher
82
     *
83
     * @return RouteInterface
84
     */
85 4
    public static function post(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
86
    {
87 4
        return self::methods([Method::POST], $pattern, $dispatcher);
88
    }
89
90
    /**
91
     * @param string $pattern
92
     * @param MiddlewareDispatcher|null $dispatcher
93
     *
94
     * @return RouteInterface
95
     */
96 1
    public static function put(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
97
    {
98 1
        return self::methods([Method::PUT], $pattern, $dispatcher);
99
    }
100
101
    /**
102
     * @param string $pattern
103
     * @param MiddlewareDispatcher|null $dispatcher
104
     *
105
     * @return RouteInterface
106
     */
107 1
    public static function delete(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
108
    {
109 1
        return self::methods([Method::DELETE], $pattern, $dispatcher);
110
    }
111
112
    /**
113
     * @param string $pattern
114
     * @param MiddlewareDispatcher|null $dispatcher
115
     *
116
     * @return RouteInterface
117
     */
118 1
    public static function patch(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
119
    {
120 1
        return self::methods([Method::PATCH], $pattern, $dispatcher);
121
    }
122
123
    /**
124
     * @param string $pattern
125
     * @param MiddlewareDispatcher|null $dispatcher
126
     *
127
     * @return RouteInterface
128
     */
129 1
    public static function head(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
130
    {
131 1
        return self::methods([Method::HEAD], $pattern, $dispatcher);
132
    }
133
134
    /**
135
     * @param string $pattern
136
     * @param MiddlewareDispatcher|null $dispatcher
137
     *
138
     * @return RouteInterface
139
     */
140 1
    public static function options(string $pattern, ?MiddlewareDispatcher $dispatcher = null): RouteInterface
141
    {
142 1
        return self::methods([Method::OPTIONS], $pattern, $dispatcher);
143
    }
144
145
    /**
146
     * @param array $methods
147
     * @param string $pattern
148
     * @param MiddlewareDispatcher|null $dispatcher
149
     *
150
     * @return RouteInterface
151
     */
152 43
    public static function methods(
153
        array $methods,
154
        string $pattern,
155
        ?MiddlewareDispatcher $dispatcher = null
156
    ): RouteInterface {
157 43
        $route = new self($dispatcher);
158 43
        $route->methods = $methods;
159 43
        $route->pattern = $pattern;
160
161 43
        return $route;
162
    }
163
164
    /**
165
     * @return self
166
     */
167 18
    public function name(string $name): RouteInterface
168
    {
169 18
        $route = clone $this;
170 18
        $route->name = $name;
171 18
        return $route;
172
    }
173
174
    /**
175
     * @return self
176
     */
177 14
    public function pattern(string $pattern): RouteInterface
178
    {
179 14
        $new = clone $this;
180 14
        $new->pattern = $pattern;
181 14
        return $new;
182
    }
183
184
    /**
185
     * @return self
186
     */
187 3
    public function host(string $host): RouteInterface
188
    {
189 3
        $route = clone $this;
190 3
        $route->host = rtrim($host, '/');
191 3
        return $route;
192
    }
193
194
    /**
195
     * @return self
196
     */
197 2
    public function override(): RouteInterface
198
    {
199 2
        $route = clone $this;
200 2
        $route->override = true;
201 2
        return $route;
202
    }
203
204
    /**
205
     * @return self
206
     */
207 1
    public function defaults(array $defaults): RouteInterface
208
    {
209 1
        $route = clone $this;
210 1
        $route->defaults = $defaults;
211 1
        return $route;
212
    }
213
214
    /**
215
     * @return self
216
     */
217 5
    public function middleware($middlewareDefinition): RouteInterface
218
    {
219 5
        if ($this->actionAdded) {
220
            throw new RuntimeException('middleware() can not be used after action().');
221
        }
222 5
        $route = clone $this;
223 5
        array_unshift($route->middlewareDefinitions, $middlewareDefinition);
224 5
        return $route;
225
    }
226
227
    /**
228
     * @return self
229
     */
230 6
    public function prependMiddleware($middlewareDefinition): RouteInterface
231
    {
232 6
        if (!$this->actionAdded) {
233
            throw new RuntimeException('prependMiddleware() can not be used before action().');
234
        }
235 6
        $route = clone $this;
236 6
        $route->middlewareDefinitions[] = $middlewareDefinition;
237 6
        return $route;
238
    }
239
240
    /**
241
     * @return self
242
     */
243 8
    public function action($middlewareDefinition): RouteInterface
244
    {
245 8
        $route = clone $this;
246 8
        array_unshift($route->middlewareDefinitions, $middlewareDefinition);
247 8
        $route->actionAdded = true;
248 8
        return $route;
249
    }
250
251
    /**
252
     * @return self
253
     */
254
    public function disableMiddleware($middlewareDefinition): RouteInterface
255
    {
256
        $route = clone $this;
257
        $route->disabledMiddlewareDefinitions[] = $middlewareDefinition;
258
        return $route;
259
    }
260
261 3
    public function __toString(): string
262
    {
263 3
        $result = '';
264
265 3
        if ($this->name !== null) {
266 2
            $result .= '[' . $this->name . '] ';
267
        }
268
269 3
        if ($this->methods !== []) {
270 3
            $result .= implode(',', $this->methods) . ' ';
271
        }
272 3
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
273 1
            $result .= $this->host;
274
        }
275 3
        $result .= $this->pattern;
276
277 3
        return $result;
278
    }
279
280 17
    public function getName(): string
281
    {
282 17
        return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->host . $this->pattern);
283
    }
284
285 21
    public function getMethods(): array
286
    {
287 21
        return $this->methods;
288
    }
289
290 14
    public function getPattern(): string
291
    {
292 14
        return $this->pattern;
293
    }
294
295 2
    public function getHost(): ?string
296
    {
297 2
        return $this->host;
298
    }
299
300 4
    public function isOverride(): bool
301
    {
302 4
        return $this->override;
303
    }
304
305 1
    public function getDefaults(): array
306
    {
307 1
        return $this->defaults;
308
    }
309
310 13
    public function hasMiddlewares(): bool
311
    {
312 13
        return $this->middlewareDefinitions !== [];
313
    }
314
315
    public function __debugInfo()
316
    {
317
        return [
318
            'name' => $this->name,
319
            'methods' => $this->methods,
320
            'pattern' => $this->pattern,
321
            'host' => $this->host,
322
            'defaults' => $this->defaults,
323
            'override' => $this->override,
324
            'actionAdded' => $this->actionAdded,
325
            'middlewareDefinitions' => $this->middlewareDefinitions,
326
            'disabledMiddlewareDefinitions' => $this->disabledMiddlewareDefinitions,
327
            'middlewareDispatcher' => $this->dispatcher,
328
        ];
329
    }
330
}
331