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

Routes   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 73
rs 10
ccs 21
cts 21
cp 1
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addRoutesForAllMethods() 0 4 2
A redirect() 0 10 2
A __call() 0 6 2
A addRouteByName() 0 5 1
A routes() 0 3 1
A createRoute() 0 12 3
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 Routes
27
{
28
    /** @var list<Route> */
29
    private array $routes = [];
30
31
    /**
32
     * @psalm-suppress MixedArgument
33
     */
34 53
    public function __call(string $name, array $arguments): void
35
    {
36 53
        if ($name === 'any') {
37 9
            $this->addRoutesForAllMethods($arguments);
38
        } else {
39 44
            $this->addRouteByName($name, $arguments);
40
        }
41
    }
42
43
    /**
44
     * @return list<Route>
45
     */
46 72
    public function routes(): array
47
    {
48 72
        return $this->routes;
49
    }
50
51 20
    public function redirect(
52
        string $uri,
53
        string $destination,
54
        int $status = 302,
55
        string $method = null,
56
    ): void {
57 20
        if ($method === null) {
58 10
            $this->addRoutesForAllMethods([$uri, new RedirectController($destination, $status)]);
59
        } else {
60 10
            $this->addRouteByName($method, [$uri, new RedirectController($destination, $status)]);
61
        }
62
    }
63
64
    /**
65
     * @psalm-suppress MixedArgument
66
     */
67 19
    private function addRoutesForAllMethods(array $arguments): void
68
    {
69 19
        foreach (Request::ALL_METHODS as $methodName) {
70 19
            $this->routes[] = $this->createRoute($methodName, ...$arguments);
0 ignored issues
show
Bug introduced by
The call to Gacela\Router\Routes::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

70
            /** @scrutinizer ignore-call */ 
71
            $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...
71
        }
72
    }
73
74
    /**
75
     * @psalm-suppress MixedArgument
76
     */
77 54
    private function addRouteByName(string $httpMethod, array $arguments): void
78
    {
79 54
        $route = $this->createRoute(strtoupper(trim($httpMethod)), ...$arguments);
80
81 53
        $this->routes[] = $route;
82
    }
83
84
    /**
85
     * @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...
86
     */
87 73
    private function createRoute(
88
        string $httpMethod,
89
        string $path,
90
        object|string $controller,
91
        string $action = '__invoke',
92
    ): Route {
93 73
        if (!in_array($httpMethod, Request::ALL_METHODS)) {
94 1
            throw new UnsupportedHttpMethodException($httpMethod);
95
        }
96 72
        $path = ($path === '/') ? '' : $path;
97
98 72
        return new Route($httpMethod, $path, $controller, $action);
99
    }
100
}
101