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 master (a445f1)
by Jérémy
03:02
created

Route::run()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 61
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 6.4757
c 0
b 0
f 0
cc 10
eloc 36
nc 4
nop 3

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
9
class Route extends EventEmitter
10
{
11
    /**
12
     * Regexp ready route
13
     * @var String
14
     */
15
    public $parsedRoute;
16
17
    /**
18
     * Route method type
19
     * @var String
20
     */
21
    public $method;
22
23
    /**
24
     * Route action
25
     * @var Callable
26
     */
27
    public $action;
28
29
    /**
30
     * Route uri
31
     * @var String
32
     */
33
    private $uri;
34
35
    /**
36
     * Route filters
37
     * @var array
38
     */
39
    private $filters = array();
40
41
    /**
42
     * @param String   $method
43
     * @param String   $uri
44
     * @param Callable $action
45
     */
46
    public function __construct ($method, $uri, $action)
47
    {
48
        $this->method = $method;
49
        $this->uri = $uri;
50
        $this->action = $action;
51
    }
52
53
    /**
54
     * Create a new filter for current route
55
     *
56
     * @param String $param  parameter to filter
57
     * @param String $filter regexp to execute
58
     *
59
     * @return void
60
     */
61
    public function where($param, $filter)
62
    {
63
        if (is_array($param)) {
64
            $this->filters = array_merge($this->filters, $param);
65
66
            return;
67
        }
68
69
        $this->filters[$param] = $filter;
70
    }
71
72
    /**
73
     * Helper to listing to after event
74
     *
75
     * @param  Callable $callback
76
     * @return Void
77
     */
78
    public function after($callback)
79
    {
80
        $this->on('after', $callback);
81
    }
82
83
    /**
84
     * Parse route uri
85
     *
86
     * @return void
87
     */
88
    public function parse()
89
    {
90
        preg_match_all("#\{(\w+)\}#", $this->uri, $params);
91
        $replace = array();
92
93
        foreach ($params[1] as $param) {
94
            $replace['{'.$param.'}'] = '(?<'.$param.'>'. (isset($this->filters[$param]) ? $this->filters[$param] : '[a-zA-Z+0-9-.]+') .')';
95
        }
96
97
        $this->parsed = str_replace(array_keys($replace), array_values($replace), $this->uri);
0 ignored issues
show
Bug introduced by
The property parsed does not seem to exist. Did you mean parsedRoute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
    }
99
100
    /**
101
     * Check if uri is parsed
102
     *
103
     * @return boolean
104
     */
105
    public function isParsed()
106
    {
107
        return !empty($this->parsed);
0 ignored issues
show
Bug introduced by
The property parsed does not seem to exist. Did you mean parsedRoute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108
    }
109
110
    /**
111
     * Run the current route
112
     *
113
     * @param \React\Http\Request     $request
114
     * @param \React\Restify\Response $response
115
     * @param Callable                $next
116
     *
117
     * @return Void
118
     */
119
    public function run(Request $request, Response $response, $next)
120
    {
121
        if (is_string($this->action)) {
122
            $this->action = explode(':', $this->action);
123
            $this->action[0] = new $action[0]();
0 ignored issues
show
Bug introduced by
The variable $action 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...
124
        }
125
126
        if (!in_array($this->method, array('PUT', 'POST'))) {
127
            call_user_func_array($this->action, array($request, $response, $next));
128
            $this->emit('after', [$request, $response, $this]);
129
130
            return;
131
        }
132
133
        $dataResult = "";
134
        $headers = $request->getHeaders();
135
136
        //Get data chunck by chunk
137
        $request->httpRequest->on('data', function($data) use ($headers, $request, &$dataResult) {
138
            $dataResult .= $data;
139
140
            if (isset($headers["Content-Length"])) {
141
                if (strlen($dataResult) == $headers["Content-Length"]) {
142
                    $request->httpRequest->close();
143
                }
144
            } else {
145
                $request->httpRequest->close();
146
            }
147
        });
148
149
        //Wait request end to launch route
150
        $request->httpRequest->on('end', function() use ($headers, $request, $response, &$dataResult, $next) {
151
            $error = null;
152
            $data = [];
153
154
            if ($dataResult !== null) {
155
                if (isset($headers['content-type']) && $headers['content-type'] == 'application/json') {
156
                    $jsonData = json_decode($dataResult, true);
157
158
                    if ($jsonData === null) {
159
                        $error = json_last_error_msg();
160
                    } else {
161
                        $data = $jsonData;
162
                    }
163
                } else {
164
                    parse_str($dataResult, $data);
165
                }
166
            }
167
168
            $request->setContent($dataResult);
169
            $request->setData($data);
170
171
            if (null === $error) {
172
                call_user_func_array($this->action, array($request, $response, $next));
173
                $this->emit('after', [$request, $response, $this]);
174
            } else {
175
                $this->emit('error', [$request, $response, $error, $next]);
176
            }
177
178
        });
179
    }
180
}
181