Passed
Pull Request — main (#6)
by
unknown
02:15
created

RoutingConfigurator::addRouteByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
use function in_array;
8
9
/**
10
 * @method head(string $path, object|string $controller, string $action = '__invoke')
11
 * @method connect(string $path, object|string $controller, string $action = '__invoke')
12
 * @method get(string $path, object|string $controller, string $action = '__invoke')
13
 * @method post(string $path, object|string $controller, string $action = '__invoke')
14
 * @method put(string $path, object|string $controller, string $action = '__invoke')
15
 * @method patch(string $path, object|string $controller, string $action = '__invoke')
16
 * @method delete(string $path, object|string $controller, string $action = '__invoke')
17
 * @method options(string $path, object|string $controller, string $action = '__invoke')
18
 * @method trace(string $path, object|string $controller, string $action = '__invoke')
19
 * @method any(string $path, object|string $controller, string $action = '__invoke')
20
 */
21
final class RoutingConfigurator
22
{
23
    /** @var list<Route> */
24
    private array $routes = [];
25
26
    /** @var array<class-string, callable|class-string|object> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
27
    private array $mappingInterfaces = [];
28
29
    /**
30
     * @psalm-suppress MixedArgument
31
     */
32 53
    public function __call(string $name, array $arguments): void
33
    {
34 53
        if ($name === 'any') {
35 9
            $this->addRoutesForAllMethods($arguments);
36
        } else {
37 44
            $this->addRouteByName($name, $arguments);
38
        }
39
    }
40
41
    /**
42
     * @return list<Route>
43
     */
44 72
    public function routes(): array
45
    {
46 72
        return $this->routes;
47
    }
48
49
    /**
50
     * @param array<class-string, callable|class-string|object> $array
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
51
     */
52 1
    public function setMappingInterfaces(array $array): self
53
    {
54 1
        $this->mappingInterfaces = $array;
55 1
        return $this;
56
    }
57
58
    /**
59
     * @return array<class-string, callable|class-string|object>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, callable|class-string|object> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, callable|class-string|object>.
Loading history...
60
     */
61 50
    public function getMappingInterfaces(): array
62
    {
63 50
        return $this->mappingInterfaces;
64
    }
65
66 20
    public function redirect(
67
        string $uri,
68
        string $destination,
69
        int $status = 302,
70
        string $method = null,
71
    ): void {
72 20
        if ($method === null) {
73 10
            $this->addRoutesForAllMethods([$uri, new RedirectController($destination, $status)]);
74
        } else {
75 10
            $this->addRouteByName($method, [$uri, new RedirectController($destination, $status)]);
76
        }
77
    }
78
79
    /**
80
     * @psalm-suppress MixedArgument
81
     */
82 19
    private function addRoutesForAllMethods(array $arguments): void
83
    {
84 19
        foreach (Request::ALL_METHODS as $methodName) {
85 19
            $this->routes[] = $this->createRoute($methodName, ...$arguments);
0 ignored issues
show
Bug introduced by
The call to Gacela\Router\RoutingConfigurator::createRoute() has too few arguments starting with controller. ( Ignorable by Annotation )

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

85
            /** @scrutinizer ignore-call */ 
86
            $this->routes[] = $this->createRoute($methodName, ...$arguments);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
86
        }
87
    }
88
89
    /**
90
     * @psalm-suppress MixedArgument
91
     */
92 54
    private function addRouteByName(string $httpMethod, array $arguments): void
93
    {
94 54
        $route = $this->createRoute(strtoupper(trim($httpMethod)), ...$arguments);
95
96 53
        $this->routes[] = $route;
97
    }
98
99
    /**
100
     * @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...
101
     */
102 73
    private function createRoute(
103
        string $httpMethod,
104
        string $path,
105
        object|string $controller,
106
        string $action = '__invoke',
107
    ): Route {
108 73
        if (!in_array($httpMethod, Request::ALL_METHODS)) {
109 1
            throw new UnsupportedHttpMethodException($httpMethod);
110
        }
111 72
        $path = ($path === '/') ? '' : $path;
112
113 72
        return new Route($httpMethod, $path, $controller, $action);
114
    }
115
}
116