Issues (20)

Mezon/Router/SimpleUrlParser.php (2 issues)

Labels
Severity
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
use Mezon\Router\Types\BaseType;
6
7
/**
8
 * Trait SimpleUrlParser
9
 *
10
 * @package Router
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2021/09/27)
13
 * @copyright Copyright (c) 2021, http://aeon.su
14
 */
15
trait SimpleUrlParser
16
{
17
18
    use UrlParserBase;
19
20
    /**
21
     * Method compiles route pattern string in regex string.
22
     * For example [i:id]/some-str in ([\[0-9\]])/some-str
23
     *
24
     * @param string $routerPattern
25
     *            router pattern
26
     * @return string regexp pattern
27
     */
28
    private function _getRouteMatcherRegExPattern(string $routerPattern): string
29
    {
30
        // parsing routes
31
        $compiledRouterPattern = $routerPattern;
32
33
        foreach ($this->types as $typeClass) {
34
            $compiledRouterPattern = preg_replace(
35
                '/' . $typeClass::searchRegExp() . '/',
36
                '(' . $typeClass::parserRegExp() . ')',
37
                $compiledRouterPattern);
38
        }
39
40
        return str_replace('/', '\/', $compiledRouterPattern);
41
    }
42
43
    /**
44
     * Method returns all parameter names in the route
45
     *
46
     * @param string $routerPattern
47
     *            route
48
     * @return string[] names
49
     */
50
    private function _getParameterNames(string $routerPattern): array
51
    {
52
        $regExPattern = [];
53
54
        foreach (array_keys($this->types) as $typeName) {
55
            $regExPattern[] = $typeName;
56
        }
57
58
        $regExPattern = '\[(' . implode('|', $regExPattern) . '):(' . BaseType::PARAMETER_NAME_REGEXP . ')\]';
59
60
        $names = [];
61
        preg_match_all('/' . str_replace('/', '\\/', $regExPattern) . '/', $routerPattern, $names);
62
63
        $return = [];
64
65
        foreach ($names[2] as $name) {
66
            $return[] = $name;
67
        }
68
69
        return $return;
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
     * @psalm-suppress PossiblyUndefinedArrayOffset
81
     */
82
    protected function getDynamicRouteProcessor(string $route, string $requestMethod = '')
83
    {
84
        $routes = $this->paramRoutes[$requestMethod == '' ? $this->getRequestMethod() : $requestMethod];
0 ignored issues
show
It seems like getRequestMethod() 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

84
        $routes = $this->paramRoutes[$requestMethod == '' ? $this->/** @scrutinizer ignore-call */ getRequestMethod() : $requestMethod];
Loading history...
85
86
        foreach ($routes as $item) {
87
            $matches = [];
88
89
            if (preg_match('/^' . $this->_getRouteMatcherRegExPattern($item['pattern']) . '$/u', $route, $matches)) {
90
                $names = $this->_getParameterNames($item['pattern']);
91
92
                $this->parameters = [];
93
                foreach ($names as $i => $name) {
94
                    $this->parameters[$name] = $matches[(int) $i + 1];
95
                }
96
97
                $this->setCalledRoute($item['pattern']);
0 ignored issues
show
It seems like setCalledRoute() 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

97
                $this->/** @scrutinizer ignore-call */ 
98
                       setCalledRoute($item['pattern']);
Loading history...
98
99
                return $item['callback'];
100
            }
101
        }
102
103
        // match was not found
104
        return false;
105
    }
106
}
107