Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Generic often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Generic, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class Generic |
||
| 9 | { |
||
| 10 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 11 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 12 | use \PHPDaemon\Traits\EventHandlers; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * State: finished. |
||
| 16 | * @var integer |
||
| 17 | */ |
||
| 18 | const STATE_FINISHED = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * State: waiting. |
||
| 22 | * @var integer |
||
| 23 | */ |
||
| 24 | const STATE_WAITING = 2; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * State: running. |
||
| 28 | * @var integer |
||
| 29 | */ |
||
| 30 | const STATE_RUNNING = 3; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Related Application instance |
||
| 34 | * @var \PHPDaemon\Core\AppInstance |
||
| 35 | */ |
||
| 36 | public $appInstance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Is this request aborted? |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | protected $aborted = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * State |
||
| 46 | * @var integer (self::STATE_*) |
||
| 47 | */ |
||
| 48 | protected $state = self::STATE_WAITING; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Attributes |
||
| 52 | * @var \StdCLass |
||
| 53 | */ |
||
| 54 | public $attrs; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Registered shutdown functions |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $shutdownFuncs = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Is this request running? |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | protected $running = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Upstream |
||
| 70 | * @var object |
||
| 71 | */ |
||
| 72 | protected $upstream; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Event |
||
| 76 | * @var object |
||
| 77 | */ |
||
| 78 | protected $ev; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Current sleep() time |
||
| 82 | * @var float |
||
| 83 | */ |
||
| 84 | protected $sleepTime = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Priority |
||
| 88 | * @var integer |
||
| 89 | */ |
||
| 90 | protected $priority = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Current code point |
||
| 94 | * @var mixed |
||
| 95 | */ |
||
| 96 | protected $codepoint; |
||
| 97 | |||
| 98 | protected $autoinit = false; // @TODO: remove this option in future version |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Log |
||
| 102 | * @param string $msg Message |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function log($msg) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Constructor |
||
| 112 | * @param AppInstance $appInstance Parent AppInstance. |
||
| 113 | * @param IRequestUpstream $upstream Upstream. |
||
| 114 | * @param object $parent Source request. |
||
|
|
|||
| 115 | */ |
||
| 116 | public function __construct($appInstance, IRequestUpstream $upstream, $parent = null) |
||
| 130 | |||
| 131 | public function callInit() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Uncaught exception handler |
||
| 146 | * @param $e |
||
| 147 | * @return boolean|null Handled? |
||
| 148 | */ |
||
| 149 | public function handleException($e) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Is this request aborted? |
||
| 155 | * @return boolean |
||
| 156 | */ |
||
| 157 | public function isAborted() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Is this request finished? |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | public function isFinished() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Is this request running? |
||
| 173 | * @return boolean |
||
| 174 | */ |
||
| 175 | public function isRunning() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Output some data |
||
| 182 | * @param string $s String to out |
||
| 183 | * @param bool $flush |
||
| 184 | * @return boolean|null Success |
||
| 185 | */ |
||
| 186 | public function out($s, $flush = true) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Called when request iterated. |
||
| 192 | * @return integer Status. |
||
| 193 | */ |
||
| 194 | protected function run() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Event handler of Request, called by Evtimer |
||
| 200 | * @param $arg |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function eventCall($arg) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Frees the request |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function free() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sets the priority |
||
| 265 | * @param integer Priority |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function setPriority($p) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Preparing before init |
||
| 278 | * @param object Source request |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | protected function preinit($req) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * This magic method called when the object casts to string |
||
| 293 | * @return string Description |
||
| 294 | */ |
||
| 295 | public function __toString() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Called when request constructs |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | protected function init() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get string value from the given variable |
||
| 310 | * @param Reference of variable. |
||
| 311 | * @param array Optional. Possible values. |
||
| 312 | * @return string Value. |
||
| 313 | */ |
||
| 314 | public static function getString(&$var, $values = null) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get string value from the given variable |
||
| 328 | * @param Reference of variable. |
||
| 329 | * @return string Value. |
||
| 330 | */ |
||
| 331 | public static function getMixed(&$var) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get array value from the given variable |
||
| 338 | * @param Reference of variable. |
||
| 339 | * @param array Optional. Filter callback. |
||
| 340 | * @return string Value. |
||
| 341 | */ |
||
| 342 | public static function getArray(&$var, $filter = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get integer value from the given variable |
||
| 356 | * @param Reference of variable. |
||
| 357 | * @param array Optional. Possible values. |
||
| 358 | * @return string Value. |
||
| 359 | */ |
||
| 360 | public static function getInteger(&$var, $values = null) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Called when the connection is ready to accept new data |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function onWrite() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Adds new callback called before the request finished |
||
| 385 | * @param callable $callback |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | public function registerShutdownFunction($callback) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Remove the given callback |
||
| 395 | * @param callable $callback |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | public function unregisterShutdownFunction($callback) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Helper for easy switching between several interruptable stages of request's execution |
||
| 407 | * @param string Name |
||
| 408 | * @return boolean Execute |
||
| 409 | */ |
||
| 410 | public function codepoint($p) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Delays the request execution for the given number of seconds |
||
| 422 | * |
||
| 423 | * @param integer $time Time to sleep in seconds |
||
| 424 | * @param boolean $set Set this parameter to true when use call it outside of Request->run() or if you don't want to interrupt execution now |
||
| 425 | * @throws RequestSleep |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function sleep($time = 0, $set = false) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Throws terminating exception |
||
| 451 | * @param $s |
||
| 452 | * @throws RequestTerminated |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | public function terminate($s = null) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Cancel current sleep |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | public function wakeup() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Called to check if Request is ready |
||
| 478 | * @return boolean Ready? |
||
| 479 | */ |
||
| 480 | public function checkIfReady() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Called when the request aborted |
||
| 487 | * @return void |
||
| 488 | */ |
||
| 489 | public function onAbort() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Called when the request finished |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function onFinish() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Called when the request wakes up |
||
| 503 | * @return void |
||
| 504 | */ |
||
| 505 | public function onWakeup() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Called when the request starts sleep |
||
| 515 | * @return void |
||
| 516 | */ |
||
| 517 | public function onSleep() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Aborts the request |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function abort() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Finish the request |
||
| 553 | * @param integer Optional. Status. 0 - normal, -1 - abort, -2 - termination |
||
| 554 | * @param boolean Optional. Zombie. Default is false |
||
| 555 | * @return void |
||
| 556 | */ |
||
| 557 | public function finish($status = 0, $zombie = false) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Called after request finish |
||
| 615 | * @param callable $cb |
||
| 616 | * @return void |
||
| 617 | */ |
||
| 618 | protected function postFinishHandler($cb = null) |
||
| 624 | } |
||
| 625 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.