Conditions | 46 |
Paths | 76 |
Total Lines | 143 |
Lines | 7 |
Ratio | 4.9 % |
Changes | 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 |
||
249 | public function onCommand($from, $cmd, $args) |
||
250 | { |
||
251 | $this->event('command', $from, $cmd, $args); |
||
252 | if ($cmd === 'RPL_WELCOME') { |
||
253 | $this->servername = $from['orig']; |
||
254 | if ($this->onConnected) { |
||
255 | $this->connected = true; |
||
256 | $this->onConnected->executeAll($this); |
||
257 | $this->onConnected = null; |
||
258 | } |
||
259 | } elseif ($cmd === 'NOTICE') { |
||
260 | list($target, $text) = $args; |
||
261 | $this->event('notice', $target, $text); |
||
262 | } elseif ($cmd === 'RPL_YOURHOST') { |
||
263 | } elseif ($cmd === 'RPL_MOTDSTART') { |
||
264 | $this->motd = $args[1]; |
||
265 | return; |
||
266 | } elseif ($cmd === 'RPL_MOTD') { |
||
267 | $this->motd .= "\r\n" . $args[1]; |
||
268 | return; |
||
269 | } elseif ($cmd === 'RPL_ENDOFMOTD') { |
||
270 | $this->motd .= "\r\n"; // . $args[1]; |
||
271 | $this->event('motd', $this->motd); |
||
272 | return; |
||
273 | } elseif ($cmd === 'JOIN') { |
||
274 | list($channel) = $args; |
||
275 | $chan = $this->channel($channel); |
||
276 | ChannelParticipant::instance($chan, $from['nick'])->setUsermask($from); |
||
277 | } elseif ($cmd === 'NICK') { |
||
278 | list($newNick) = $args; |
||
279 | foreach ($this->channels as $channel) { |
||
280 | if (isset($channel->nicknames[$from['nick']])) { |
||
281 | $channel->nicknames[$from['nick']]->setNick($newNick); |
||
282 | } |
||
283 | } |
||
284 | } elseif ($cmd === 'QUIT') { |
||
285 | $args[] = null; |
||
286 | list($msg) = $args; |
||
287 | foreach ($this->channels as $channel) { |
||
288 | if (isset($channel->nicknames[$from['nick']])) { |
||
289 | $channel->nicknames[$from['nick']]->destroy(); |
||
290 | } |
||
291 | } |
||
292 | } elseif ($cmd === 'PART') { |
||
293 | $args[] = null; |
||
294 | list($channel, $msg) = $args; |
||
295 | if ($chan = $this->channelIfExists($channel)) { |
||
296 | $chan->onPart($from, $msg); |
||
297 | } |
||
298 | } elseif ($cmd === 'RPL_NAMREPLY') { |
||
299 | $bufName = 'RPL_NAMREPLY'; |
||
300 | list(/*$myNick*/, $chanType, $channelName) = $args; |
||
301 | $this->channel($channelName)->setChanType($chanType); |
||
302 | if (!isset($this->buffers[$bufName])) { |
||
303 | $this->buffers[$bufName] = []; |
||
304 | } |
||
305 | if (!isset($this->buffers[$bufName][$channelName])) { |
||
306 | $this->buffers[$bufName][$channelName] = new \SplStack; |
||
307 | } |
||
308 | $this->buffers[$bufName][$channelName]->push($args); |
||
309 | } elseif ($cmd === 'RPL_ENDOFNAMES') { |
||
310 | $bufName = 'RPL_NAMREPLY'; |
||
311 | list(/*$nick*/, $channelName, /*$text*/) = $args; |
||
312 | if (!isset($this->buffers[$bufName][$channelName])) { |
||
313 | return; |
||
314 | } |
||
315 | $buf = $this->buffers[$bufName][$channelName]; |
||
316 | $chan = null; |
||
317 | while (!$buf->isEmpty()) { |
||
318 | $shift = $buf->shift(); |
||
319 | list($nick, $chantype, $channelName, $participants) = $shift; |
||
320 | if (!$chan) { |
||
321 | $chan = $this->channel($channelName)->setType($chantype); |
||
322 | $chan->each('destroy'); |
||
323 | } |
||
324 | preg_match_all('~([\+%@]?)(\S+)~', $participants, $matches, PREG_SET_ORDER); |
||
325 | |||
326 | foreach ($matches as $m) { |
||
327 | list(, $flag, $nickname) = $m; |
||
328 | ChannelParticipant::instance($chan, $nickname)->setFlag($flag); |
||
329 | } |
||
330 | } |
||
331 | } elseif ($cmd === 'RPL_WHOREPLY') { |
||
332 | if (sizeof($args) < 7) { |
||
333 | } |
||
334 | list(/*$myNick*/, $channelName, $user, /*$host*/, $server, $nick, $mode, $hopCountRealName) = $args; |
||
335 | list($hopCount, $realName) = explode("\x20", $hopCountRealName); |
||
336 | if ($channel = $this->channelIfExists($channelName)) { |
||
337 | ChannelParticipant::instance($channel, $nick) |
||
338 | ->setUsermask($nick . '!' . $user . '@' . $server) |
||
339 | ->setFlag($mode); |
||
340 | } |
||
341 | } elseif ($cmd === 'RPL_TOPIC') { |
||
342 | list(/*$myNick*/, $channelName, $text) = $args; |
||
343 | if ($channel = $this->channelIfExists($channelName)) { |
||
344 | $channel->setTopic($text); |
||
345 | } |
||
346 | } elseif ($cmd === 'RPL_ENDOFWHO') { |
||
347 | /*list($myNick, $channelName, $text) = $args;*/ |
||
348 | } elseif ($cmd === 'MODE') { |
||
349 | if (sizeof($args) === 3) { |
||
350 | list($channel, $mode, $target) = $args; |
||
351 | } else { |
||
352 | $channel = null; |
||
353 | list($target, $mode) = $args; |
||
354 | } |
||
355 | if (mb_orig_strlen($mode)) { |
||
356 | if ($mode[0] === '+') { |
||
357 | $this->addMode($channel, $target, mb_orig_substr($mode, 1)); |
||
358 | } elseif ($mode[0] === '-') { |
||
359 | $this->removeMode($channel, $target, mb_orig_substr($mode, 1)); |
||
360 | } |
||
361 | } |
||
362 | } elseif ($cmd === 'RPL_CREATED') { |
||
363 | list(/*$to*/, $this->created) = $args; |
||
364 | } elseif ($cmd === 'RPL_MYINFO') { |
||
365 | list(/*$to*/, $this->servername, $this->serverver, $this->availUserModes, $this->availChanModes) = $args; |
||
366 | } elseif ($cmd === 'PRIVMSG') { |
||
367 | list($target, $body) = $args; |
||
368 | $msg = [ |
||
369 | 'from' => $from, |
||
370 | 'to' => $target, |
||
371 | 'body' => $body, |
||
372 | 'private' => mb_orig_substr($target, 0, 1) !== '#', |
||
373 | ]; |
||
374 | $this->event($msg['private'] ? 'privateMsg' : 'channelMsg', $msg); |
||
375 | $this->event('msg', $msg); |
||
376 | if (!$msg['private']) { |
||
377 | $this->channel($target)->event('msg', $msg); |
||
378 | } |
||
379 | } elseif ($cmd === 'PING') { |
||
380 | $this->writeln(isset($args[0]) ? 'PONG :' . $args[0] : 'PONG'); |
||
381 | View Code Duplication | } elseif ($cmd === 'PONG') { |
|
382 | if ($this->lastPingTS) { |
||
383 | $this->latency = microtime(true) - $this->lastPingTS; |
||
384 | $this->lastPingTS = null; |
||
385 | } |
||
386 | return; |
||
387 | } |
||
388 | if ($this->pool->protologging) { |
||
389 | Daemon::$process->log('<-<-<-< ' . $cmd . ': ' . json_encode($args) . ' (' . json_encode($from['orig']) . ') (' . json_encode($this->lastLine) . ')'); |
||
390 | } |
||
391 | } |
||
392 | |||
464 |