RouteMatching   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 7
Bugs 3 Features 0
Metric Value
eloc 56
c 7
b 3
f 0
dl 0
loc 159
rs 9.28
wmc 39

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F getPatternResolve() 0 60 22
B getPatternRealCount() 0 18 7
A isSameOnStaticStrings() 0 13 5
A showKeyAccordingToScoredList() 0 17 4
1
<?php
2
3
namespace Resta\Router;
4
5
use Resta\Contracts\ApplicationContracts;
6
use Resta\Foundation\ApplicationProvider;
7
8
class RouteMatching extends ApplicationProvider
9
{
10
    /**
11
     * @var null|object
12
     */
13
    protected $route;
14
15
    /**
16
     * RouteMatching constructor.
17
     *
18
     * @param ApplicationContracts $app
19
     * @param null|object $route
20
     */
21
    public function __construct(ApplicationContracts $app, $route=null)
22
    {
23
        parent::__construct($app);
24
25
        $this->route = $route;
26
    }
27
28
    /**
29
     * get route pattern resolve
30
     *
31
     * @return array|int|string
32
     */
33
    public function getPatternResolve()
34
    {
35
        $routes = $this->route->getRoutes();
0 ignored issues
show
Bug introduced by
The method getRoutes() does not exist on null. ( Ignorable by Annotation )

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

35
        /** @scrutinizer ignore-call */ 
36
        $routes = $this->route->getRoutes();

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...
36
37
        if(!isset($routes['pattern'])){
38
            return [];
39
        }
40
41
        $patterns = $routes['pattern'];
42
        $urlRoute = array_filter(route(),'strlen');
43
44
        $patternList = [];
45
46
        foreach($routes['data'] as $patternKey=>$routeData){
47
            if($routeData['http']==httpMethod()){
48
                $patternList[$patternKey]=$patterns[$patternKey];
49
            }
50
        }
51
52
        $scoredList = [];
53
54
        foreach ($patternList as $key=>$pattern){
55
56
            $patternCount = $this->getPatternRealCount($pattern);
57
58
            if(count($urlRoute)==0 && count($urlRoute)==0) return array_key_first($patternList);
59
60
            if(isset($patternCount['optional'])){
61
                $optionalCount = count($patternCount['default']) + count($patternCount['optional']);
62
            }
63
64
            if(count($urlRoute) == count($patternCount['default']) ||
65
                (isset($optionalCount) && count($urlRoute)>count($patternCount['default']) && $optionalCount>=count($urlRoute))
66
            ){
67
68
                foreach ($pattern as $pkey=>$item){
69
70
                    if($this->route->isMatchVaribleRegexPattern($item)===false){
71
                        if(isset($urlRoute[$pkey]) && $urlRoute[$pkey]==$item){
72
                            $scoredList[$key][] = 3;
73
                        }
74
                    }
75
76
                    if($this->route->isMatchVaribleRegexPattern($item) && !$this->route->isOptionalVaribleRegexPattern($item)){
77
                        if(isset($urlRoute[$pkey])){
78
                            $scoredList[$key][] = 2;
79
                        }
80
                    }
81
82
                    if($this->route->isMatchVaribleRegexPattern($item) && $this->route->isOptionalVaribleRegexPattern($item)){
83
                        if(isset($urlRoute[$pkey])){
84
                            $scoredList[$key][] = 1;
85
                        }
86
                    }
87
                }
88
            }
89
90
        }
91
92
        return $this->showKeyAccordingToScoredList($scoredList);
93
94
    }
95
96
    /**
97
     * get pattern real count
98
     *
99
     * @param $pattern
100
     * @return array
101
     */
102
    private function getPatternRealCount($pattern)
103
    {
104
        $list = [];
105
        $list['default'] = [];
106
107
        foreach ($pattern as $key=>$value){
108
            if(($this->route->isMatchVaribleRegexPattern($value)===false) || ($this->route->isMatchVaribleRegexPattern($value)
109
                    && !$this->route->isOptionalVaribleRegexPattern($value))){
110
                $list['default'][$key] = $value;
111
            }
112
113
            if(($this->route->isMatchVaribleRegexPattern($value)
114
                && $this->route->isOptionalVaribleRegexPattern($value))){
115
                $list['optional'][] = true;
116
            }
117
        }
118
119
        return $list;
120
    }
121
122
    /**
123
     * show key according tp scored list
124
     *
125
     * @param array $scoredList
126
     * @return false|int|string
127
     */
128
    private function showKeyAccordingToScoredList($scoredList=array())
129
    {
130
        $scored = [];
131
132
        foreach($scoredList as $key=>$item){
133
            $scored[$key] = array_sum($item);
134
        }
135
136
        if(count($scored)){
137
            $arrayCountValues = array_count_values($scored);
138
139
            if($arrayCountValues[max($scored)]==1){
140
                return array_search(max($scored),$scored);
141
            }
142
        }
143
144
        return null;
145
146
    }
147
148
    /**
149
     * is same on static strings
150
     *
151
     * @param $pattern
152
     * @return bool
153
     */
154
    private function isSameOnStaticStrings($pattern)
0 ignored issues
show
Unused Code introduced by
The method isSameOnStaticStrings() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
155
    {
156
        $route = route();
157
158
        foreach ($pattern as $key=>$item) {
159
            if($this->route->isMatchVaribleRegexPattern($item)===false){
160
                if(isset($route[$key]) && $item!==$route[$key]){
161
                    return false;
162
                }
163
            }
164
        }
165
166
        return true;
167
    }
168
}