Issues (20)

Mezon/Router/UrlParserBase.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait UrlParserBase
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2021/09/27)
11
 * @copyright Copyright (c) 2021, http://aeon.su
12
 */
13
trait UrlParserBase
14
{
15
16
    /**
17
     * Parsed parameters of the calling router
18
     *
19
     * @var mixed[]
20
     */
21
    protected $parameters = [];
22
23
    /**
24
     * Method returns parameters
25
     *
26
     * @return mixed[]
27
     */
28
    protected function getParameters(): array
29
    {
30
        return $this->parameters;
31
    }
32
33
    /**
34
     * Method sets parameters
35
     *
36
     * @param mixed[] $parameters
37
     *            parameters
38
     */
39
    protected function setParameters(array $parameters): void
40
    {
41
        $this->parameters = $parameters;
42
    }
43
44
    /**
45
     * Method returns route parameter
46
     *
47
     * @param string $name
48
     *            route parameter
49
     * @return mixed route parameter
50
     */
51
    public function getParam(string $name)
52
    {
53
        if (! isset($this->getParameters()[$name])) {
54
            throw (new \Exception('Parameter ' . $name . ' was not found in route', - 1));
55
        }
56
57
        return $this->getParameters()[$name];
58
    }
59
60
    /**
61
     * Does parameter exists
62
     *
63
     * @param string $name
64
     *            param name
65
     * @return bool true if the parameter exists
66
     */
67
    public function hasParam(string $name): bool
68
    {
69
        return isset($this->getParameters()[$name]);
70
    }
71
72
    /**
73
     * Method searches dynamic route processor
74
     *
75
     * @param string $route
76
     *            route
77
     * @param string $requestMethod
78
     *            request method
79
     * @return array{0: string, 1:string}|callable|string|false route's handler or false in case the handler was not found
80
     */
81
    abstract protected function getDynamicRouteProcessor(string $route, string $requestMethod = '');
82
83
    /**
84
     * Checking that method exists
85
     *
86
     * @param mixed $processor
87
     *            callback object
88
     * @param ?string $functionName
89
     *            callback method
90
     * @return bool true if method does not exists
91
     */
92
    private function methodDoesNotExists($processor, ?string $functionName): bool
93
    {
94
        return $functionName === null ||
95
            (isset($processor[0]) && is_object($processor[0]) && method_exists($processor[0], $functionName) === false);
96
    }
97
98
    /**
99
     * Method executes route handler
100
     *
101
     * @param
102
     *            callable|string|array{0: string, 1: string} $processor processor
103
     * @psalm-param callable|string|array{0: string, 1: string} $processor
104
     * @param string $route
105
     *            route
106
     * @return mixed route handler execution result
107
     */
108
    protected function executeHandler($processor, string $route)
109
    {
110
        if (is_callable($processor)) {
111
            return call_user_func_array($processor, $this->getMiddlewareResult($route));
0 ignored issues
show
It seems like getMiddlewareResult() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
            return call_user_func_array($processor, $this->/** @scrutinizer ignore-call */ getMiddlewareResult($route));
Loading history...
112
        }
113
114
        $functionName = $processor[1] ?? null;
115
116
        $callableDescription = Utils::getCallableDescription($processor);
117
118
        if ($this->methodDoesNotExists($processor, $functionName)) {
119
            throw (new \Exception("'$callableDescription' does not exists"));
120
        } else {
121
            // @codeCoverageIgnoreStart
122
            throw (new \Exception("'$callableDescription' must be callable entity"));
123
            // @codeCoverageIgnoreEnd
124
        }
125
    }
126
}
127