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 Extender 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 Extender, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\Extender; |
||
| 38 | class Extender { |
||
| 39 | |||
| 40 | // configurable things |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Max result lenght (in bytes) retrieved from parent in miltithread mode |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $max_result_bytes_in_multithread = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | private $max_childs_runtime = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Multithread mode |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $multithread_mode = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Verbose mode, if requested via command line arg -v |
||
| 65 | * |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | private $verbose_mode = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Debug mode, if requested via command line arg -V |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private $debug_mode = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Summary mode, if requested via command line arg -s |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $summary_mode = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Daemon mode, if requested via command line arg -d |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $daemon_mode = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Timestamp, relative, of current extend() cycle |
||
| 93 | * |
||
| 94 | * @var float |
||
| 95 | */ |
||
| 96 | private $timestamp = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Timestamp, absolute, sinnce extender was initiated |
||
| 100 | * |
||
| 101 | * @var float |
||
| 102 | */ |
||
| 103 | private $timestamp_absolute = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * PID of the parent extender process |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | private $parent_pid = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set exender in paused mode (no job will be processed) |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | private $paused = false; |
||
| 118 | |||
| 119 | // Helper classes |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Events manager instance |
||
| 123 | * |
||
| 124 | * @var \Comodojo\Extender\Events |
||
| 125 | */ |
||
| 126 | private $events = null; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Console_Color2 instance |
||
| 130 | * |
||
| 131 | * @var \Console_Color2 |
||
| 132 | */ |
||
| 133 | private $color = null; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Logger instance |
||
| 137 | * |
||
| 138 | * @var \Monolog\Logger |
||
| 139 | */ |
||
| 140 | private $logger = null; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * JobsRunner instance |
||
| 144 | * |
||
| 145 | * @var \Comodojo\Extender\Runner\JobsRunner |
||
| 146 | */ |
||
| 147 | private $runner = null; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * TasksTable instance |
||
| 151 | * |
||
| 152 | * @var \Comodojo\Extender\TasksTable |
||
| 153 | */ |
||
| 154 | private $tasks = null; |
||
| 155 | |||
| 156 | // checks and locks are static! |
||
| 157 | |||
| 158 | // local archives |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Failed processes, refreshed each cycle (in daemon mode) |
||
| 162 | * |
||
| 163 | * @var int |
||
| 164 | */ |
||
| 165 | private $failed_processes = 0; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Completed processes |
||
| 169 | * |
||
| 170 | * @var int |
||
| 171 | */ |
||
| 172 | private $completed_processes = 0; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Constructor method |
||
| 176 | * |
||
| 177 | * Prepare extender environment, do checks and fire extender.ready event |
||
| 178 | */ |
||
| 179 | 12 | final public function __construct() { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Set max result length (in bytes) that should be read from child tasks |
||
| 289 | * |
||
| 290 | 3 | * @param int $bytes Maximum length (bytes) |
|
| 291 | * |
||
| 292 | 3 | * @return Extender $this |
|
| 293 | */ |
||
| 294 | 3 | final public function setMaxResultLength($bytes) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | 3 | * Get max result length (in bytes) |
|
| 304 | * |
||
| 305 | 3 | * @return int Bytes parent should read (max) |
|
| 306 | */ |
||
| 307 | final public function getMaxResultLength() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
||
| 315 | * |
||
| 316 | * After $time seconds, parent will start killing tasks |
||
| 317 | * |
||
| 318 | 3 | * @param int $time Maximum time (seconds) |
|
| 319 | * |
||
| 320 | 3 | * @return Extender $this |
|
| 321 | */ |
||
| 322 | 3 | final public function setMaxChildsRuntime($time) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | 3 | * Get maximum time (in seconds) the parent will wait for child tasks to be completed (in miltithread mode) |
|
| 332 | * |
||
| 333 | 3 | * @return int Time parent will wait for childs to be completed |
|
| 334 | */ |
||
| 335 | final public function getMaxChildsRuntime() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set working mode (single or multithread) |
||
| 343 | * |
||
| 344 | * If multithread enabled, extender will use pcntl to fork child tasks |
||
| 345 | * |
||
| 346 | 3 | * @param bool $mode Enable/disable multithread |
|
| 347 | * |
||
| 348 | 3 | * @return Extender $this |
|
| 349 | */ |
||
| 350 | 3 | final public function setMultithreadMode($mode) { |
|
| 357 | |||
| 358 | /** |
||
| 359 | 12 | * Get multithread mode status |
|
| 360 | * |
||
| 361 | 12 | * @return bool True if enabled, false if disabled |
|
| 362 | */ |
||
| 363 | final public function getMultithreadMode() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | 6 | * Get daemon mode status |
|
| 371 | * |
||
| 372 | 6 | * @return bool True if enabled, false if disabled |
|
| 373 | */ |
||
| 374 | final public function getDaemonMode() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | 3 | * Get the number of completed processes |
|
| 382 | * |
||
| 383 | 3 | * @return int |
|
| 384 | */ |
||
| 385 | final public function getCompletedProcesses() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | 3 | * Get the number of failed processes |
|
| 393 | * |
||
| 394 | 3 | * @return int |
|
| 395 | */ |
||
| 396 | final public function getFailedProcesses() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | 3 | * Get current version |
|
| 404 | * |
||
| 405 | 3 | * @return string |
|
| 406 | */ |
||
| 407 | final public function getVersion() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | 6 | * Get events manager |
|
| 415 | * |
||
| 416 | 6 | * @return \Comodojo\Extender\Events |
|
| 417 | */ |
||
| 418 | final public function events() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | 3 | * Get console color instance |
|
| 426 | * |
||
| 427 | 3 | * @return \Console_Color2 |
|
| 428 | */ |
||
| 429 | final public function color() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | 3 | * Get internal logger |
|
| 437 | * |
||
| 438 | 3 | * @return \Monolog\Logger |
|
| 439 | */ |
||
| 440 | final public function logger() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | 3 | * Get jobs' runner |
|
| 448 | * |
||
| 449 | 3 | * @return JobsRunner |
|
| 450 | */ |
||
| 451 | final public function runner() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | 6 | * Get the tasks' table |
|
| 459 | * |
||
| 460 | 6 | * @return TasksTable |
|
| 461 | */ |
||
| 462 | final public function tasks() { |
||
| 467 | |||
| 468 | 3 | /** |
|
| 469 | * Do extend! |
||
| 470 | 3 | * |
|
| 471 | */ |
||
| 472 | public function extend() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Change parent process priority according to EXTENDER_NICENESS |
||
| 505 | * |
||
| 506 | */ |
||
| 507 | final public function adjustNiceness() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Register signals |
||
| 521 | * |
||
| 522 | */ |
||
| 523 | final public function registerSignals() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Delete all status file after exit() called |
||
| 563 | * |
||
| 564 | */ |
||
| 565 | final public function shutdown($force = false) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * The sigTerm handler. |
||
| 597 | * |
||
| 598 | * It kills everything and then exit with status 1 |
||
| 599 | */ |
||
| 600 | final public function sigTermHandler() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * The sigStop handler. |
||
| 616 | * |
||
| 617 | * It just pauses extender execution |
||
| 618 | */ |
||
| 619 | final public function sigStopHandler() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * The sigCont handler. |
||
| 633 | * |
||
| 634 | * It just resume extender execution |
||
| 635 | */ |
||
| 636 | final public function sigContHandler() { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * The generig signal handler. |
||
| 650 | * |
||
| 651 | * It can be used to handle custom signals |
||
| 652 | */ |
||
| 653 | final public function genericSignalHandler($signal) { |
||
| 664 | |||
| 665 | private function cycle() { |
||
| 838 | |||
| 839 | private static function showHelp($color) { |
||
| 862 | 12 | ||
| 863 | private static function getCommandlineOptions() { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @param double $timestamp |
||
| 879 | */ |
||
| 880 | private static function showSummary($timestamp, $completed_processes, $color) { |
||
| 927 | |||
| 928 | /** |
||
| 929 | * @param integer $returnCode |
||
| 930 | */ |
||
| 931 | View Code Duplication | private static function end($returnCode) { |
|
| 946 | |||
| 947 | } |
||
| 948 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.