Test Setup Failed
Push — master ( d9069c...7f5209 )
by Php Easy Api
04:48
created

RouteMatching::getPatternResolve()   C

Complexity

Conditions 13
Paths 28

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 1
b 0
f 0
nc 28
nop 0
dl 0
loc 52
rs 6.6166

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\Support\Arr;
6
use Resta\Contracts\ApplicationContracts;
7
use Resta\Foundation\ApplicationProvider;
8
9
class RouteMatching extends ApplicationProvider
10
{
11
    /**
12
     * @var null|object
13
     */
14
    protected $route;
15
16
    /**
17
     * @var bool
18
     */
19
    private $unset = false;
20
21
    /**
22
     * RouteMatching constructor.
23
     * @param ApplicationContracts $app
24
     * @param null|object $route
25
     */
26
    public function __construct(ApplicationContracts $app, $route=null)
27
    {
28
        parent::__construct($app);
29
30
        $this->route = $route;
31
    }
32
33
    /**
34
     * get route pattern resolve
35
     *
36
     * @return array|int|string
37
     */
38
    public function getPatternResolve()
39
    {
40
        $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

40
        /** @scrutinizer ignore-call */ 
41
        $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...
41
42
        if(!isset($routes['pattern'])){
43
            return [];
44
        }
45
46
        $patterns = $routes['pattern'];
47
        $urlRoute = array_filter(route(),'strlen');
48
49
        $patternList = [];
50
51
        foreach($routes['data'] as $patternKey=>$routeData){
52
            if($routeData['http']==httpMethod()){
53
                $patternList[$patternKey]=$patterns[$patternKey];
54
            }
55
        }
56
57
        $patternList = $this->matchingUrlMethod($patternList,$urlRoute);
58
59
        foreach ($patternList as $key=>$pattern){
60
61
            $pattern = array_filter($pattern,'strlen');
62
            $diff = Arr::arrayDiffKey($pattern,$urlRoute);
63
64
            if($diff){
65
66
                $matches = true;
67
68
                foreach ($pattern as $patternKey=>$patternValue){
69
                    if(!$this->route->isMatchVaribleRegexPattern($patternValue)){
70
                        if($patternValue!==$urlRoute[$patternKey]){
71
                            $matches = false;
72
                        }
73
                    }
74
                }
75
76
                if($matches){
77
78
                    $isArrayEqual = $this->route->checkArrayEqual($patternList,$urlRoute);
79
80
                    if($isArrayEqual===null){
81
                        return $key;
82
                    }
83
                    return $isArrayEqual;
84
                }
85
            }
86
87
            if(count($pattern)-1 == count(route())){
88
                if(preg_match('@\{[a-z]+\?\}@is',end($pattern))){
89
                    return $key;
90
                }
91
            }
92
        }
93
    }
94
95
    /**
96
     * matching url method
97
     *
98
     * @param $patterns
99
     * @param $urlMethod
100
     * @return array
101
     */
102
    public function matchingUrlMethod($patterns,$urlMethod)
103
    {
104
        if(isset($urlMethod[0])){
105
106
            $list = [];
107
108
            foreach ($patterns as $key=>$pattern){
109
110
                // if the initial value of the pattern data is present
111
                // and the first value from urlmethod does not match
112
                // and does not match the custom regex variable,
113
                // we empty the contents of the data.
114
                if(isset($pattern[0])){
115
                    if($pattern[0] !== $urlMethod[0] && !$this->route->isMatchVaribleRegexPattern($pattern[0])){
116
                        $list[$key] = [];
117
                    }
118
                }
119
120
                // if the contents of the directory are not normally emptied,
121
                // we continue to save the list according to keyin status.
122
                if(!isset($list[$key])){
123
                    $list[$key] = $pattern;
124
                }
125
126
                // This is very important.
127
                // Route matches can be variable-based or static string-based.
128
                // In this case, we remove the other matches based on the static string match.
129
                if(isset($pattern[0]) && $pattern[0]==$urlMethod[0]){
130
131
                    // static matches will not be deleted retrospectively.
132
                    // this condition will check this.
133
                    if($this->unset===false){
134
                        unset($list);
135
                        $this->unset = true;
136
                    }
137
138
                    $list[$key] = $pattern;
139
                }
140
            }
141
142
            $patterns = $list;
143
            $this->unset = true;
144
        }
145
146
        return $patterns;
147
    }
148
}