Passed
Push — main ( fe77ac...556dc2 )
by Chema
02:14
created

Route::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
use ReflectionClass;
8
use ReflectionNamedType;
9
10
use function count;
11
use function is_object;
12
13
/**
14
 * @method static get(string $path, object|string $controller, string $action = '__invoke')
15
 * @method static head(string $path, object|string $controller, string $action = '__invoke')
16
 * @method static connect(string $path, object|string $controller, string $action = '__invoke')
17
 * @method static post(string $path, object|string $controller, string $action = '__invoke')
18
 * @method static delete(string $path, object|string $controller, string $action = '__invoke')
19
 * @method static options(string $path, object|string $controller, string $action = '__invoke')
20
 * @method static patch(string $path, object|string $controller, string $action = '__invoke')
21
 * @method static put(string $path, object|string $controller, string $action = '__invoke')
22
 * @method static trace(string $path, object|string $controller, string $action = '__invoke')
23
 */
24
final class Route
25
{
26
    private static ?Request $request = null;
27
28
    private static bool $isResponded = false;
29
30
    /**
31
     * @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...
32
     */
33 42
    private function __construct(
34
        private string $method,
35
        private string $path,
36
        private object|string $controller,
37
        private string $action = '__invoke',
38
    ) {
39 42
    }
40
41
    /**
42
     * @psalm-suppress MixedArgument
43
     */
44 42
    public static function __callStatic(string $name, array $arguments): void
45
    {
46 42
        match ($name) {
47 42
            'head' => self::route(Request::METHOD_HEAD, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...ETHOD_HEAD, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48 42
            'connect' => self::route(Request::METHOD_CONNECT, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...OD_CONNECT, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49 42
            'post' => self::route(Request::METHOD_POST, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...ETHOD_POST, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50 42
            'delete' => self::route(Request::METHOD_DELETE, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...HOD_DELETE, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
51 42
            'options' => self::route(Request::METHOD_OPTIONS, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...OD_OPTIONS, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52 42
            'patch' => self::route(Request::METHOD_PATCH, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...THOD_PATCH, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53 42
            'put' => self::route(Request::METHOD_PUT, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...METHOD_PUT, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54 42
            'trace' => self::route(Request::METHOD_TRACE, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...THOD_TRACE, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55 42
            default => self::route(Request::METHOD_GET, ...$arguments),
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::route(Gacela\Route...METHOD_GET, $arguments) targeting Gacela\Router\Route::route() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56 42
        };
57
    }
58
59
    /**
60
     * @internal for testing
61
     */
62 42
    public static function reset(): void
63
    {
64 42
        self::$isResponded = false;
65 42
        self::$request = null;
66
    }
67
68 42
    public function requestMatches(): bool
69
    {
70 42
        if (self::$isResponded) {
71 1
            return false;
72
        }
73
74 42
        if (!$this->methodMatches()) {
75 1
            return false;
76
        }
77
78 41
        if (!$this->pathMatches()) {
79 1
            return false;
80
        }
81
82 40
        return true;
83
    }
84
85
    /**
86
     * @psalm-suppress MixedMethodCall
87
     */
88 40
    public function run(): string
89
    {
90 40
        self::$isResponded = true;
91
92 40
        if (is_object($this->controller)) {
93
            return (string)$this->controller
94
                ->{$this->action}(...$this->getParams());
95
        }
96 40
        return (string)(new $this->controller())
97 40
            ->{$this->action}(...$this->getParams());
98
    }
99
100
    /**
101
     * @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...
102
     */
103 42
    private static function route(
104
        string $method,
105
        string $path,
106
        object|string $controller,
107
        string $action = '__invoke',
108
    ): void {
109 42
        $path = ($path === '/') ? '' : $path;
110
111 42
        $route = new self($method, $path, $controller, $action);
112
113 42
        if ($route->requestMatches()) {
114 40
            echo $route->run();
115
        }
116
    }
117
118 42
    private function methodMatches(): bool
119
    {
120 42
        return $this->request()->isMethod($this->method);
121
    }
122
123 41
    private function pathMatches(): bool
124
    {
125 41
        return (bool)preg_match($this->getPathPattern(), $this->request()->path())
126 41
            || (bool)preg_match($this->getPathPatternWithoutOptionals(), $this->request()->path());
127
    }
128
129 41
    private function getPathPattern(): string
130
    {
131 41
        $pattern = preg_replace('#({.*})#U', '(.*)', $this->path);
132
133 41
        return '#^/' . $pattern . '$#';
134
    }
135
136 3
    private function getPathPatternWithoutOptionals(): string
137
    {
138 3
        $pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path);
139
140 3
        return '#^/' . $pattern . '$#';
141
    }
142
143 40
    private function getParams(): array
144
    {
145 40
        $params = [];
146 40
        $pathParamKeys = [];
147 40
        $pathParamValues = [];
148
149 40
        preg_match($this->getPathPattern(), '/' . $this->path, $pathParamKeys);
150 40
        preg_match($this->getPathPattern(), $this->request()->path(), $pathParamValues);
151
152 40
        unset($pathParamValues[0], $pathParamKeys[0]);
153 40
        $pathParamKeys = array_map(static fn ($key) => trim($key, '{}'), $pathParamKeys);
154
155 40
        while (count($pathParamValues) !== count($pathParamKeys)) {
156 2
            array_shift($pathParamKeys);
157
        }
158
159 40
        $pathParams = array_combine($pathParamKeys, $pathParamValues);
160 40
        $actionParams = (new ReflectionClass($this->controller))
161 40
            ->getMethod($this->action)
162 40
            ->getParameters();
163
164 40
        foreach ($actionParams as $actionParam) {
165 36
            $paramName = $actionParam->getName();
166
            /** @var string|null $paramType */
167 36
            $paramType = null;
168
169 36
            if ($actionParam->getType() && is_a($actionParam->getType(), ReflectionNamedType::class)) {
170 36
                $paramType = $actionParam->getType()->getName();
171
            }
172
173 36
            $value = match ($paramType) {
174 36
                'string' => $pathParams[$paramName] ?? '',
175 36
                'int' => (int)($pathParams[$paramName] ?? 0),
176 36
                'float' => (float)($pathParams[$paramName] ?? 0.0),
177 36
                'bool' => (bool)json_decode($pathParams[$paramName] ?? '0'),
178 36
                null => null,
179 36
            };
180
181 36
            $params[$paramName] = $value;
182
        }
183
184 40
        return $params;
185
    }
186
187 42
    private function request(): Request
188
    {
189 42
        return self::$request ??= Request::fromGlobals();
190
    }
191
}
192