Test Setup Failed
Push — master ( cd364e...e9efe7 )
by Php Easy Api
04:06
created

RouteMatching::getPatternRealCount()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 17
rs 8.8333
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(isset($patternCount['optional'])){
59
                $optionalCount = count($patternCount['default']) + count($patternCount['optional']);
60
            }
61
62
            if(count($urlRoute) == count($patternCount['default']) ||
63
                (isset($optionalCount) && count($urlRoute)>count($patternCount['default']) && $optionalCount>=count($urlRoute))
64
            ){
65
66
                foreach ($pattern as $pkey=>$item){
67
68
                    if($this->route->isMatchVaribleRegexPattern($item)===false){
69
                        if(isset($urlRoute[$pkey]) && $urlRoute[$pkey]==$item){
70
                            $scoredList[$key][] = 3;
71
                        }
72
                    }
73
74
                    if($this->route->isMatchVaribleRegexPattern($item) && !$this->route->isOptionalVaribleRegexPattern($item)){
75
                        if(isset($urlRoute[$pkey])){
76
                            $scoredList[$key][] = 2;
77
                        }
78
                    }
79
80
                    if($this->route->isMatchVaribleRegexPattern($item) && $this->route->isOptionalVaribleRegexPattern($item)){
81
                        if(isset($urlRoute[$pkey])){
82
                            $scoredList[$key][] = 1;
83
                        }
84
                    }
85
                }
86
            }
87
88
        }
89
90
        return $this->showKeyAccordingToScoredList($scoredList);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->showKeyAcc...ScoredList($scoredList) could also return false which is incompatible with the documented return type array|integer|string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
91
92
    }
93
94
    /**
95
     * get pattern real count
96
     *
97
     * @param $pattern
98
     * @return array
99
     */
100
    private function getPatternRealCount($pattern)
101
    {
102
        $list = [];
103
104
        foreach ($pattern as $key=>$value){
105
            if(($this->route->isMatchVaribleRegexPattern($value)===false) || ($this->route->isMatchVaribleRegexPattern($value)
106
                    && !$this->route->isOptionalVaribleRegexPattern($value))){
107
                $list['default'][$key] = $value;
108
            }
109
110
            if(($this->route->isMatchVaribleRegexPattern($value)
111
                && $this->route->isOptionalVaribleRegexPattern($value))){
112
                $list['optional'][] = true;
113
            }
114
        }
115
116
        return $list;
117
    }
118
119
    /**
120
     * show key according tp scored list
121
     *
122
     * @param array $scoredList
123
     * @return false|int|string
124
     */
125
    private function showKeyAccordingToScoredList($scoredList=array())
126
    {
127
        $scored = [];
128
129
        foreach($scoredList as $key=>$item){
130
            $scored[$key] = array_sum($item);
131
        }
132
133
        if(count($scored)){
134
            return array_search(max($scored),$scored);
135
        }
136
137
        return null;
138
139
    }
140
141
    /**
142
     * is same on static strings
143
     *
144
     * @param $pattern
145
     * @return bool
146
     */
147
    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...
148
    {
149
        $route = route();
150
151
        foreach ($pattern as $key=>$item) {
152
            if($this->route->isMatchVaribleRegexPattern($item)===false){
153
                if(isset($route[$key]) && $item!==$route[$key]){
154
                    return false;
155
                }
156
            }
157
        }
158
159
        return true;
160
    }
161
}