GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch dev (718e6b)
by Jérémy
04:44 queued 02:58
created

Router::matchRoutes()   C

Complexity

Conditions 10
Paths 51

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
rs 6.8372
cc 10
eloc 29
nc 51
nop 2

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 CapMousse\ReactRestify\Routing;
4
5
use CapMousse\ReactRestify\Evenement\EventEmitter;
6
use CapMousse\ReactRestify\Http\Request;
7
use CapMousse\ReactRestify\Http\Response;
8
use CapMousse\ReactRestify\Traits\WaterfallTrait;
9
10
class Router extends EventEmitter
11
{
12
    use WaterfallTrait;
13
    /**
14
     * The current routes list
15
     * @var array
16
     */
17
    public $routes = [];
18
19
    /**
20
     * The current asked uri
21
     * @var string|boolean
22
     */
23
    private $uri = false;
24
25
    /**
26
     * Create a new routing element
27
     *
28
     * @param array $routes a route array
29
     *
30
     * @throws \InvalidArgumentException
31
     * @return Router
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
32
     */
33
    public function __construct($routes = [])
34
    {
35
        if (!is_array($routes)) {
36
            throw new \InvalidArgumentException("Routes must be an array");
37
        }
38
39
        $this->addRoutes($routes);
40
    }
41
42
    /**
43
     * Add routes
44
     *
45
     * @param array $routes a route array
46
     *
47
     * @throws \InvalidArgumentException
48
     * @return Void
49
     */
50
    public function addRoutes($routes)
51
    {
52
        if (!is_array($routes)) {
53
            throw new \InvalidArgumentException("Routes must be an array");
54
        }
55
56
        $routes = array_filter($routes, function ($route) {
57
            return is_a('Route', $route);
58
        });
59
60
        $this->routes = array_merge($this->routes, $routes);
61
    }
62
63
    /**
64
     * Add a new route
65
     *
66
     * @param String   $method   type of route
67
     * @param String   $route    uri to catch
68
     * @param Callable $callback
69
     */
70
    public function addRoute($method, $route, $callback)
71
    {
72
        return $this->routes[] = new Route(strtoupper($method), $route, $callback);
73
    }
74
75
    /**
76
     * Create a new group of routes
77
     *
78
     * @param String $prefix prefix of thes routes
79
     *
80
     * @return \CapMousse\ReactRestify\Routing\Group
81
     */
82
    public function addGroup($prefix, $callback)
83
    {
84
        $group = new Routes($this, $prefix, $callback);
85
86
        return $group;
87
    }
88
89
    /**
90
     * Launch the route parsing
91
     *
92
     * @param \React\Http\Request     $request
93
     * @param \React\Restify\Response $response
94
     *
95
     * @throws \RuntimeException
96
     * @return Void
97
     */
98
    public function launch(Request $request, Response $response)
99
    {
100
        if (count($this->routes) === 0) {
101
            throw new \RuntimeException("No routes defined");
102
        }
103
104
        $this->uri = $request->httpRequest->getPath();
105
106
        if ($this->uri = null) {
107
            $this->uri = "/";
108
        }
109
110
        $request->on('end', function () use (&$request, &$response) {
111
            $this->matchRoutes($request, $response);  
112
        });
113
114
        $request->on('error', function ($error) use (&$request, &$response) {
115
            $this->emit('error', [$request, $response, $error]);
116
        });
117
118
        $request->parseData();
119
    }
120
121
    /**
122
     * Try to match the current uri with all routes
123
     *
124
     *
125
     * @param \React\Http\Request     $request
126
     * @param \React\Restify\Response $response
127
     *
128
     * @throws \RuntimeException
129
     * @return Void
130
     */
131
    private function matchRoutes(Request $request, Response $response)
132
    {
133
        $stack = [];
134
        $badMethod = false;
135
136
        foreach ($this->routes as $route) {
137
            if (!$route->isParsed()) {
138
                $route->parse();
139
            }
140
141
            if (preg_match('#'.$route->parsed.'$#', $request->httpRequest->getPath(), $array)) {
142
                if ($route->method != strtoupper($request->httpRequest->getMethod())) {
143
                    $badMethod = true;
144
                    continue;
145
                }
146
147
                $methodArgs = [];
148
149
                foreach ($array as $name => $value) {
150
                    if (!is_int($name)) {
151
                      $methodArgs[$name] = $value;
152
                    }
153
                }
154
155
                if (count($methodArgs) > 0) {
156
                    $request->setData($methodArgs);
157
                }
158
159
                $route->on('error', function () {
160
                    $this->emit('error', func_get_args());
161
                });
162
163
                $stack[] = function ($next) use ($route, $request, $response) {
164
                    $route->run($request, $response, $next);
165
                };
166
            }
167
        }
168
169
        if (count($stack)) {
170
            $stack[] = function () use ($response) {
171
                $response->end();
172
            };
173
174
            $this->waterfall($stack);
175
            return;
176
        }
177
178
        if ($badMethod) {
179
            $this->emit('MethodNotAllowed', array(&$request, &$response, $next));
0 ignored issues
show
Bug introduced by
The variable $next does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
180
            return;
181
        }
182
183
        $this->emit('NotFound', array($request, $response, $next));
184
    }
185
}
186