Conditions | 38 |
Paths | 0 |
Total Lines | 182 |
Code Lines | 125 |
Lines | 16 |
Ratio | 8.79 % |
Changes | 6 | ||
Bugs | 2 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | public function onRead() |
||
95 | { |
||
96 | start: |
||
97 | if ($this->state === self::STATE_ROOT) { |
||
98 | $header = $this->readExact(8); |
||
99 | |||
100 | if ($header === false) { |
||
101 | return; |
||
102 | } |
||
103 | |||
104 | $this->header = unpack('Cver/Ctype/nreqid/nconlen/Cpadlen/Creserved', $header); |
||
105 | |||
106 | if ($this->header['conlen'] > 0) { |
||
107 | $this->setWatermark($this->header['conlen'], $this->header['conlen']); |
||
108 | } |
||
109 | $type = $this->header['type']; |
||
110 | $this->header['ttype'] = isset(self::$requestTypes[$type]) ? self::$requestTypes[$type] : $type; |
||
111 | $rid = $this->header['reqid']; |
||
112 | $this->state = self::STATE_CONTENT; |
||
113 | } else { |
||
114 | $type = $this->header['type']; |
||
115 | $rid = $this->header['reqid']; |
||
116 | } |
||
117 | if ($this->state === self::STATE_CONTENT) { |
||
118 | $this->content = $this->readExact($this->header['conlen']); |
||
119 | |||
120 | if ($this->content === false) { |
||
121 | $this->setWatermark($this->header['conlen'], $this->header['conlen']); |
||
122 | return; |
||
123 | } |
||
124 | |||
125 | if ($this->header['padlen'] > 0) { |
||
126 | $this->setWatermark($this->header['padlen'], $this->header['padlen']); |
||
127 | } |
||
128 | |||
129 | $this->state = self::STATE_PADDING; |
||
130 | } |
||
131 | |||
132 | if ($this->state === self::STATE_PADDING) { |
||
133 | $pad = $this->readExact($this->header['padlen']); |
||
134 | |||
135 | if ($pad === false) { |
||
136 | return; |
||
137 | } |
||
138 | } |
||
139 | $this->setWatermark(8, 0xFFFFFF); |
||
140 | $this->state = self::STATE_ROOT; |
||
141 | |||
142 | // /*Daemon::log('[DEBUG] FastCGI-record ' . $this->header['ttype'] . '). Request ID: ' . $rid |
||
143 | // . '. Content length: ' . $this->header['conlen'] . ' (' . mb_orig_strlen($this->content) . ') Padding length: ' . $this->header['padlen'] |
||
144 | // . ' (' . mb_orig_strlen($pad) . ')');*/ |
||
145 | |||
146 | if ($type === self::FCGI_BEGIN_REQUEST) { |
||
147 | $u = unpack('nrole/Cflags', $this->content); |
||
148 | |||
149 | $req = new \stdClass(); |
||
150 | $req->attrs = new \stdClass; |
||
151 | $req->attrs->request = []; |
||
152 | $req->attrs->get = []; |
||
153 | $req->attrs->post = []; |
||
154 | $req->attrs->cookie = []; |
||
155 | $req->attrs->server = []; |
||
156 | $req->attrs->files = []; |
||
157 | $req->attrs->session = null; |
||
158 | $req->attrs->role = self::$roles[$u['role']]; |
||
159 | $req->attrs->flags = $u['flags']; |
||
160 | $req->attrs->id = $this->header['reqid']; |
||
161 | $req->attrs->paramsDone = false; |
||
162 | $req->attrs->inputDone = false; |
||
163 | $req->attrs->input = new \PHPDaemon\HTTPRequest\Input(); |
||
164 | $req->attrs->chunked = false; |
||
165 | $req->attrs->noHttpVer = true; |
||
166 | $req->queueId = $rid; |
||
167 | $this->requests[$rid] = $req; |
||
168 | } elseif (isset($this->requests[$rid])) { |
||
169 | $req = $this->requests[$rid]; |
||
170 | } else { |
||
171 | Daemon::log('Unexpected FastCGI-record #' . $this->header['type'] . ' (' . $this->header['ttype'] . '). Request ID: ' . $rid . '.'); |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | if ($type === self::FCGI_ABORT_REQUEST) { |
||
176 | $req->abort(); |
||
177 | } elseif ($type === self::FCGI_PARAMS) { |
||
178 | if ($this->content === '') { |
||
179 | if (!isset($req->attrs->server['REQUEST_TIME'])) { |
||
180 | $req->attrs->server['REQUEST_TIME'] = time(); |
||
181 | } |
||
182 | if (!isset($req->attrs->server['REQUEST_TIME_FLOAT'])) { |
||
183 | $req->attrs->server['REQUEST_TIME_FLOAT'] = microtime(true); |
||
184 | } |
||
185 | $req->attrs->paramsDone = true; |
||
186 | |||
187 | $req = Daemon::$appResolver->getRequest($req, $this); |
||
188 | |||
189 | if ($req instanceof \stdClass) { |
||
190 | $this->endRequest($req, 0, 0); |
||
191 | unset($this->requests[$rid]); |
||
192 | } else { |
||
193 | if ($this->pool->config->sendfile->value |
||
194 | && ( |
||
195 | !$this->pool->config->sendfileonlybycommand->value |
||
196 | || isset($req->attrs->server['USE_SENDFILE']) |
||
197 | ) |
||
198 | && !isset($req->attrs->server['DONT_USE_SENDFILE']) |
||
199 | ) { |
||
200 | $fn = tempnam( |
||
201 | $this->pool->config->sendfiledir->value, |
||
202 | $this->pool->config->sendfileprefix->value |
||
203 | ); |
||
204 | |||
205 | $req->sendfp = fopen($fn, 'wb'); |
||
206 | $req->header('X-Sendfile: ' . $fn); |
||
207 | } |
||
208 | |||
209 | $this->requests[$rid] = $req; |
||
210 | |||
211 | $req->callInit(); |
||
212 | } |
||
213 | } else { |
||
214 | $p = 0; |
||
215 | |||
216 | while ($p < $this->header['conlen']) { |
||
217 | View Code Duplication | if (($namelen = ord($this->content{$p})) < 128) { |
|
218 | ++$p; |
||
219 | } else { |
||
220 | $u = unpack( |
||
221 | 'Nlen', |
||
222 | chr(ord($this->content{$p}) & 0x7f) . mb_orig_substr($this->content, $p + 1, 3) |
||
223 | ); |
||
224 | $namelen = $u['len']; |
||
225 | $p += 4; |
||
226 | } |
||
227 | |||
228 | View Code Duplication | if (($vlen = ord($this->content{$p})) < 128) { |
|
229 | ++$p; |
||
230 | } else { |
||
231 | $u = unpack( |
||
232 | 'Nlen', |
||
233 | chr(ord($this->content{$p}) & 0x7f) . mb_orig_substr($this->content, $p + 1, 3) |
||
234 | ); |
||
235 | $vlen = $u['len']; |
||
236 | $p += 4; |
||
237 | } |
||
238 | |||
239 | $req->attrs->server[mb_orig_substr($this->content, $p, $namelen)] = mb_orig_substr( |
||
240 | $this->content, |
||
241 | $p + $namelen, |
||
242 | $vlen |
||
243 | ); |
||
244 | $p += $namelen + $vlen; |
||
245 | } |
||
246 | } |
||
247 | } elseif ($type === self::FCGI_STDIN) { |
||
248 | if (!$req->attrs->input) { |
||
249 | goto start; |
||
250 | } |
||
251 | if ($this->content === '') { |
||
252 | $req->attrs->input->sendEOF(); |
||
253 | } else { |
||
254 | $req->attrs->input->readFromString($this->content); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | if ($req->attrs->inputDone && $req->attrs->paramsDone) { |
||
259 | $order = $this->pool->variablesOrder ?: 'GPC'; |
||
260 | for ($i = 0, $s = mb_orig_strlen($order); $i < $s; ++$i) { |
||
261 | $char = $order[$i]; |
||
262 | |||
263 | if ($char === 'G' && is_array($req->attrs->get)) { |
||
264 | $req->attrs->request += $req->attrs->get; |
||
265 | } elseif ($char === 'P' && is_array($req->attrs->post)) { |
||
266 | $req->attrs->request += $req->attrs->post; |
||
267 | } elseif ($char === 'C' && is_array($req->attrs->cookie)) { |
||
268 | $req->attrs->request += $req->attrs->cookie; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | Daemon::$process->timeLastActivity = time(); |
||
273 | } |
||
274 | goto start; |
||
275 | } |
||
276 | |||
371 |