Passed
Pull Request — main (#7)
by
unknown
02:07
created

RouterConfigurator::redirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

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