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