Test Failed
Pull Request — main (#6)
by Chema
02:21
created

Route::method()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
6
7
use Gacela\Resolver\InstanceCreator;
8
9
use function is_object;
10
11
final class Route
12
{
13
    /**
14
     * @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...
15
     */
16
    public function __construct(
17
        private string $method,
18
        private string $path,
19
        private object|string $controller,
20
        private string $action = '__invoke',
21
    ) {
22
    }
23
24
    /**
25
     * @psalm-suppress MixedMethodCall
26
     */
27 52
    public function run(RoutingConfigurator $routingConfigurator): string
28
    {
29
        $params = (new RouteParams($this))->asArray();
30
31
        if (is_object($this->controller)) {
32
            return (string)$this->controller->{$this->action}(...$params);
33 52
        }
34
35
        $creator = new InstanceCreator($routingConfigurator->getMappingInterfaces());
36
        $controller = $creator->createByClassName($this->controller);
37
38 53
        return (string)$controller->{$this->action}(...$params);
39
    }
40 53
41 53
    public function path(): string
42
    {
43 52
        return $this->path;
44 52
    }
45 50
46 50
    public function method(): string
47
    {
48
        return $this->method;
49
    }
50
51
    /**
52
     * @return object|class-string
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...
53
     */
54 50
    public function controller(): object|string
55
    {
56 50
        return $this->controller;
57
    }
58 50
59
    public function action(): string
60
    {
61
        return $this->action;
62
    }
63 50
64 50
    public function getPathPattern(): string
65
    {
66 50
        $pattern = preg_replace('#({.*})#U', '(.*)', $this->path);
67
68
        return '#^/' . $pattern . '$#';
69 50
    }
70
71 50
    public function requestMatches(): bool
72
    {
73
        if (!$this->methodMatches()) {
74
            return false;
75
        }
76
77 50
        if (!$this->pathMatches()) {
78
            return false;
79 50
        }
80
81
        return true;
82 50
    }
83
84 50
    public function methodMatches(): bool
85
    {
86
        return Request::instance()->isMethod($this->method);
87 51
    }
88
89 51
    public function isRedirected(Redirect $redirect): bool
90
    {
91 51
        return $this->path() === $redirect->destination()
0 ignored issues
show
Bug introduced by
The method destination() does not exist on Gacela\Router\Redirect. ( Ignorable by Annotation )

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

91
        return $this->path() === $redirect->/** @scrutinizer ignore-call */ destination()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
            && $this->method() === $redirect->method();
0 ignored issues
show
Bug introduced by
The method method() does not exist on Gacela\Router\Redirect. ( Ignorable by Annotation )

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

92
            && $this->method() === $redirect->/** @scrutinizer ignore-call */ method();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
    }
94 52
95
    private function pathMatches(): bool
96 52
    {
97 9
        $path = Request::instance()->path();
98
99
        return preg_match($this->getPathPattern(), $path)
100 51
            || preg_match($this->getPathPatternWithoutOptionals(), $path);
101 1
    }
102
103
    private function getPathPatternWithoutOptionals(): string
104 50
    {
105
        $pattern = preg_replace('#/({.*\?})#U', '(/(.*))?', $this->path);
106
107 52
        return '#^/' . $pattern . '$#';
108
    }
109
}
110