RouteMatching::getPatternResolve()   F
last analyzed

Complexity

Conditions 22
Paths 181

Size

Total Lines 60
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 22
eloc 28
c 3
b 0
f 0
nc 181
nop 0
dl 0
loc 60
rs 3.4916

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}