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 Master 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 Master, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Master extends Generic |
||
| 19 | { |
||
| 20 | |||
| 21 | /** @var bool */ |
||
| 22 | public $delayedSigReg = true; |
||
| 23 | /** @var bool */ |
||
| 24 | public $breakMainLoop = false; |
||
| 25 | /** @var bool */ |
||
| 26 | public $reload = false; |
||
| 27 | /** @var int */ |
||
| 28 | public $connCounter = 0; |
||
| 29 | /** @var Collection */ |
||
| 30 | public $workers; |
||
| 31 | /** @var Collection */ |
||
| 32 | public $ipcthreads; |
||
| 33 | /** @var */ |
||
| 34 | public $lastMpmActionTs; |
||
| 35 | /** @var int */ |
||
| 36 | public $minMpmActionInterval = 1; // in seconds |
||
| 37 | protected $timerCb; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var StackCallbacks |
||
| 41 | */ |
||
| 42 | protected $callbacks; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Runtime of Master process |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | protected function run() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Log something |
||
| 124 | * @param string - Message. |
||
| 125 | * @param string $message |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | public function log($message) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return int |
||
|
|
|||
| 135 | */ |
||
| 136 | protected function callMPM() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Setup settings on start. |
||
| 183 | * @return void |
||
| 184 | */ |
||
| 185 | protected function prepareSystemEnv() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Reload worker by internal id |
||
| 214 | * @param integer - Id of worker |
||
| 215 | * @param integer $id |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function reloadWorker($id) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Spawn new worker processes |
||
| 231 | * @param $n - integer - number of workers to spawn |
||
| 232 | * @return boolean - success |
||
| 233 | */ |
||
| 234 | protected function spawnWorkers($n) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Spawn IPC process |
||
| 267 | * @param $n - integer - number of workers to spawn |
||
| 268 | * @return boolean - success |
||
| 269 | */ |
||
| 270 | protected function spawnIPCThread() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Stop the workers |
||
| 296 | * @param $n - integer - number of workers to stop |
||
| 297 | * @return boolean - success |
||
| 298 | */ |
||
| 299 | protected function stopWorkers($n = 1) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Called when master is going to shutdown |
||
| 329 | * @param integer System singal's number |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | protected function shutdown(int $signo = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Handler for the SIGCHLD (child is dead) signal in master process. |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | protected function sigchld() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Handler for the SIGINT (shutdown) signal in master process. Shutdown. |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | protected function sigint() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param $signo |
||
| 372 | */ |
||
| 373 | public function signalToChildren($signo) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Handler for the SIGTERM (shutdown) signal in master process |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | protected function sigterm() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Handler for the SIGQUIT signal in master process |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | protected function sigquit() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Handler for the SIGTSTP (graceful stop all workers) signal in master process |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | protected function sigtstp() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Handler for the SIGHUP (reload config) signal in master process |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | View Code Duplication | protected function sighup() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Handler for the SIGUSR1 (re-open log-file) signal in master process |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | protected function sigusr1() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Handler for the SIGUSR2 (graceful restart all workers) signal in master process |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | protected function sigusr2() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Handler for the SIGTTIN signal in master process |
||
| 464 | * Used as "ping" signal |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | protected function sigttin() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Handler for the SIGXSFZ signal in master process |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | protected function sigxfsz() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Handler for non-known signals |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | View Code Duplication | protected function sigunknown($signo) |
|
| 493 | } |
||
| 494 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.