Passed
Pull Request — main (#1)
by Chema
02:30
created

RoutingConfigurator::route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 9
rs 10
ccs 3
cts 3
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
/**
8
 * @method get(string $path, object|string $controller, string $action = '__invoke')
9
 * @method head(string $path, object|string $controller, string $action = '__invoke')
10
 * @method connect(string $path, object|string $controller, string $action = '__invoke')
11
 * @method post(string $path, object|string $controller, string $action = '__invoke')
12
 * @method delete(string $path, object|string $controller, string $action = '__invoke')
13
 * @method options(string $path, object|string $controller, string $action = '__invoke')
14
 * @method patch(string $path, object|string $controller, string $action = '__invoke')
15
 * @method put(string $path, object|string $controller, string $action = '__invoke')
16
 * @method trace(string $path, object|string $controller, string $action = '__invoke')
17
 */
18
final class RoutingConfigurator
19
{
20
    /** @var list<Route> */
21
    private array $routes = [];
22
23
    /**
24
     * @psalm-suppress MixedArgument
25
     */
26 42
    public function __call(string $name, array $arguments): void
27
    {
28 42
        $this->routes[] = match ($name) {
29 42
            'head' => $this->route(Request::METHOD_HEAD, ...$arguments),
30 42
            'connect' => $this->route(Request::METHOD_CONNECT, ...$arguments),
31 42
            'post' => $this->route(Request::METHOD_POST, ...$arguments),
32 42
            'delete' => $this->route(Request::METHOD_DELETE, ...$arguments),
33 42
            'options' => $this->route(Request::METHOD_OPTIONS, ...$arguments),
34 42
            'patch' => $this->route(Request::METHOD_PATCH, ...$arguments),
35 42
            'put' => $this->route(Request::METHOD_PUT, ...$arguments),
36 42
            'trace' => $this->route(Request::METHOD_TRACE, ...$arguments),
37 42
            default => $this->route(Request::METHOD_GET, ...$arguments),
38 42
        };
39
    }
40
41
    /**
42
     * @return list<Route>
43
     */
44 42
    public function routes(): array
45
    {
46 42
        return $this->routes;
47
    }
48
49
    /**
50
     * @param object|class-string $controller
0 ignored issues
show
Documentation Bug introduced by
The doc comment object|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in object|class-string.
Loading history...
51
     */
52 42
    private function route(
53
        string $method,
54
        string $path,
55
        object|string $controller,
56
        string $action = '__invoke',
57
    ): Route {
58 42
        $path = ($path === '/') ? '' : $path;
59
60 42
        return new Route($method, $path, $controller, $action);
61
    }
62
}
63