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 |
||
| 14 | abstract class Generic |
||
| 15 | { |
||
| 16 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 17 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Hash of known signal [no => name, ...] |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | public static $signals = [ |
||
| 24 | SIGHUP => 'SIGHUP', |
||
| 25 | SIGSYS => 'SIGSYS', |
||
| 26 | SIGPIPE => 'SIGPIPE', |
||
| 27 | SIGALRM => 'SIGALRM', |
||
| 28 | SIGTERM => 'SIGTERM', |
||
| 29 | SIGSTOP => 'SIGSTOP', |
||
| 30 | SIGINT => 'SIGINT', |
||
| 31 | SIGCHLD => 'SIGCHLD', |
||
| 32 | SIGTTIN => 'SIGTTIN', |
||
| 33 | SIGTTOU => 'SIGTTOU', |
||
| 34 | SIGIO => 'SIGIO', |
||
| 35 | SIGXCPU => 'SIGXCPU', |
||
| 36 | SIGXFSZ => 'SIGXFSZ', |
||
| 37 | SIGVTALRM => 'SIGVTALRM', |
||
| 38 | SIGPROF => 'SIGPROF', |
||
| 39 | SIGWINCH => 'SIGWINCH', |
||
| 40 | SIGUSR1 => 'SIGUSR1', |
||
| 41 | SIGUSR2 => 'SIGUSR2', |
||
| 42 | SIGTSTP => 'SIGTSTP', |
||
| 43 | ]; |
||
| 44 | /** |
||
| 45 | * Process identificator |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | public $id; |
||
| 49 | /** |
||
| 50 | * PID |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $pid; |
||
| 54 | /** |
||
| 55 | * Is this thread shutdown? |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | protected $shutdown = false; |
||
| 59 | /** |
||
| 60 | * Is this thread terminated? |
||
| 61 | * @var boolean |
||
| 62 | */ |
||
| 63 | protected $terminated = false; |
||
| 64 | /** |
||
| 65 | * Collections of childrens |
||
| 66 | * @var array|Collection[] |
||
| 67 | */ |
||
| 68 | protected $collections = []; |
||
| 69 | /** |
||
| 70 | * Storage of signal handler events |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $sigEvents = []; |
||
| 74 | /** |
||
| 75 | * If true, we do not register signals automatically at start |
||
| 76 | * @var boolean |
||
| 77 | */ |
||
| 78 | protected $delayedSigReg = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Checks if given process ID does exist |
||
| 82 | * @static |
||
| 83 | * @param integer PID |
||
| 84 | * @param integer $pid |
||
| 85 | * @return boolean Success |
||
| 86 | */ |
||
| 87 | public static function ifExistsByPid($pid) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get PID of this Thread |
||
| 94 | * @return integer |
||
| 95 | */ |
||
| 96 | public function getPid() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get ID of this Thread |
||
| 103 | * @return integer |
||
| 104 | */ |
||
| 105 | public function getId() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set ID of this Thread |
||
| 112 | * @param integer Id |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function setId($id) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Is this thread terminated? |
||
| 122 | * @return boolean |
||
| 123 | */ |
||
| 124 | public function isTerminated() |
||
| 128 | |||
| 129 | public function setTerminated() |
||
| 134 | |||
| 135 | protected function onTerminated() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Invoke magic method |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | |||
| 144 | public function __invoke() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Run thread process |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | protected function run() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Shutdowns the current process properly |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | protected function shutdown() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Called when a signal caught through libevent. |
||
| 170 | * @param integer Signal's number. |
||
| 171 | * @param integer Events. |
||
| 172 | * @param mixed Argument. |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function eventSighandler($fd, $arg) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Called when the signal is caught |
||
| 182 | * @param integer Signal's number |
||
| 183 | * @return void |
||
| 184 | */ |
||
| 185 | public function sighandler($signo) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Starts the process |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function start($clearstack = true) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Registers signals |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function registerSignals() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Delays the process execution for the given number of seconds |
||
| 244 | * @param integer Sleep time in seconds |
||
| 245 | * @return boolean Success |
||
| 246 | */ |
||
| 247 | public function sleep($s) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Terminates the process |
||
| 265 | * @param boolean Kill? |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function stop($kill = false) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Sends arbitrary signal to the process |
||
| 276 | * @param integer Signal's number |
||
| 277 | * @return boolean Success |
||
| 278 | */ |
||
| 279 | public function signal($sig) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Checks if this process does exist |
||
| 286 | * @return boolean Success |
||
| 287 | */ |
||
| 288 | public function ifExists() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Register signals. |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | protected function registerEventSignals() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Unregister signals. |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | protected function unregisterSignals() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Sends the signal to parent process |
||
| 330 | * @param integer Signal's number |
||
| 331 | * @return boolean Success |
||
| 332 | */ |
||
| 333 | protected function backsig($sig) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Called when the signal SIGCHLD caught |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | protected function sigchld() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Checks for SIGCHLD |
||
| 349 | * @return boolean Success |
||
| 350 | */ |
||
| 351 | protected function waitPid() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Called when the signal SIGTERM caught |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | protected function sigterm() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Called when the signal SIGINT caught |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | protected function sigint() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Called when the signal SIGTERM caught |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | protected function sigquit() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Called when the signal SIGKILL caught |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | protected function sigkill() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Wait until all processes are terminated |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | protected function waitAll() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Waits for signals, with a timeout |
||
| 426 | * @param int Seconds |
||
| 427 | * @param int Nanoseconds |
||
| 428 | * @return boolean Success |
||
| 429 | */ |
||
| 430 | protected function sigwait($sec = 0, $nano = 0.3e9) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Implementation of pcntl_sigtimedwait for Mac. |
||
| 455 | * |
||
| 456 | * @param array Signal |
||
| 457 | * @param null|array SigInfo |
||
| 458 | * @param int Seconds |
||
| 459 | * @param int Nanoseconds |
||
| 460 | * @param integer $sec |
||
| 461 | * @param double $nano |
||
| 462 | * @return boolean Success |
||
| 463 | */ |
||
| 464 | protected function sigtimedwait($signals, $siginfo, $sec, $nano) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Sets a title of the current process |
||
| 476 | * |
||
| 477 | * @param string $title |
||
| 478 | * @return boolean Success |
||
| 479 | */ |
||
| 480 | protected function setTitle($title) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns a title of the current process |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | protected function getTitle() |
||
| 494 | } |
||
| 495 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: