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 Worker 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 Worker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Worker extends Generic |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Update? |
||
| 24 | * @var boolean |
||
| 25 | */ |
||
| 26 | public $update = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Reload? |
||
| 30 | * @var boolean |
||
| 31 | */ |
||
| 32 | public $reload = false; |
||
| 33 | |||
| 34 | protected $graceful = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Reload time |
||
| 38 | * @var integer |
||
| 39 | */ |
||
| 40 | protected $reloadTime = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Reload delay |
||
| 44 | * @var integer |
||
| 45 | */ |
||
| 46 | protected $reloadDelay = 2; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Reloaded? |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | public $reloaded = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Time of last activity |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | public $timeLastActivity = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Last time of auto reload |
||
| 62 | * @var integer |
||
| 63 | */ |
||
| 64 | protected $autoReloadLast = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Event base |
||
| 68 | * @var EventBase |
||
| 69 | */ |
||
| 70 | public $eventBase; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * DNS base |
||
| 74 | * @var EventDnsBase |
||
| 75 | */ |
||
| 76 | public $dnsBase; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * State |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | public $state = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Request counter |
||
| 86 | * @var integer |
||
| 87 | */ |
||
| 88 | public $reqCounter = 0; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Break main loop? |
||
| 92 | * @var boolean |
||
| 93 | */ |
||
| 94 | public $breakMainLoop = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Reload ready? |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | public $reloadReady = false; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * If true, we do not register signals automatically at start |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $delayedSigReg = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Instances count |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | public $instancesCount = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Connection |
||
| 116 | * @var Connection |
||
| 117 | */ |
||
| 118 | public $connection; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Counter GC |
||
| 122 | * @var integer |
||
| 123 | */ |
||
| 124 | public $counterGC = 0; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Stack of callbacks to execute |
||
| 128 | * @var \PHPDaemon\Structures\StackCallbacks |
||
| 129 | */ |
||
| 130 | public $callbacks; |
||
| 131 | |||
| 132 | /** @var \PHPDaemon\IPCManager\IPCManager */ |
||
| 133 | public $IPCManager; |
||
| 134 | |||
| 135 | public $lambdaCache; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Runtime of Worker process. |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | protected function run() |
||
| 142 | { |
||
| 143 | $this->lambdaCache = new CappedStorageHits; |
||
| 144 | $this->lambdaCache->setMaxCacheSize(Daemon::$config->lambdacachemaxsize->value); |
||
| 145 | $this->lambdaCache->setCapWindow(Daemon::$config->lambdacachecapwindow->value); |
||
| 146 | |||
| 147 | $this->callbacks = new StackCallbacks(); |
||
| 148 | if (Daemon::$process instanceof Master) { |
||
| 149 | Daemon::$process->unregisterSignals(); |
||
|
|
|||
| 150 | } |
||
| 151 | if (Daemon::$process && Daemon::$process->eventBase) { |
||
| 152 | Daemon::$process->eventBase->reinit(); |
||
| 153 | $this->eventBase = Daemon::$process->eventBase; |
||
| 154 | } else { |
||
| 155 | $this->eventBase = new \EventBase(); |
||
| 156 | } |
||
| 157 | Daemon::$process = $this; |
||
| 158 | if (Daemon::$logpointerAsync) { |
||
| 159 | $oldfd = Daemon::$logpointerAsync->fd; |
||
| 160 | Daemon::$logpointerAsync->fd = null; |
||
| 161 | Daemon::$logpointerAsync = null; |
||
| 162 | } |
||
| 163 | class_exists('Timer'); |
||
| 164 | $this->autoReloadLast = time(); |
||
| 165 | $this->reloadDelay = Daemon::$config->mpmdelay->value + 2; |
||
| 166 | $this->setState(Daemon::WSTATE_PREINIT); |
||
| 167 | |||
| 168 | if (Daemon::$config->autogc->value > 0) { |
||
| 169 | gc_enable(); |
||
| 170 | gc_collect_cycles(); |
||
| 171 | } else { |
||
| 172 | gc_disable(); |
||
| 173 | } |
||
| 174 | |||
| 175 | if (Daemon::$runworkerMode) { |
||
| 176 | View Code Duplication | if (!Daemon::$config->verbosetty->value) { |
|
| 177 | fclose(STDIN); |
||
| 178 | fclose(STDOUT); |
||
| 179 | fclose(STDERR); |
||
| 180 | } |
||
| 181 | |||
| 182 | Daemon::$appResolver->preload(true); |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->prepareSystemEnv(); |
||
| 186 | $this->overrideNativeFuncs(); |
||
| 187 | |||
| 188 | $this->setState(Daemon::WSTATE_INIT); |
||
| 189 | ; |
||
| 190 | $this->dnsBase = new \EventDnsBase($this->eventBase, false); // @TODO: test with true |
||
| 191 | $this->registerEventSignals(); |
||
| 192 | |||
| 193 | FileSystem::init(); |
||
| 194 | FileSystem::initEvent(); |
||
| 195 | Daemon::openLogs(); |
||
| 196 | |||
| 197 | $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\PHPDaemon\IPCManager\IPCManager'); |
||
| 198 | if (!$this->IPCManager) { |
||
| 199 | $this->log('cannot instantiate IPCManager'); |
||
| 200 | } |
||
| 201 | |||
| 202 | Daemon::$appResolver->preload(); |
||
| 203 | |||
| 204 | foreach (Daemon::$appInstances as $app) { |
||
| 205 | foreach ($app as $appInstance) { |
||
| 206 | if (!$appInstance->ready) { |
||
| 207 | $appInstance->ready = true; |
||
| 208 | $appInstance->onReady(); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->setState(Daemon::WSTATE_IDLE); |
||
| 214 | |||
| 215 | Timer::add(function ($event) { |
||
| 216 | |||
| 217 | if (!Daemon::$runworkerMode) { |
||
| 218 | if ($this->IPCManager) { |
||
| 219 | $this->IPCManager->ensureConnection(); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | $this->breakMainLoopCheck(); |
||
| 224 | if ($this->breakMainLoop) { |
||
| 225 | $this->eventBase->exit(); |
||
| 226 | return; |
||
| 227 | } |
||
| 228 | |||
| 229 | if (Daemon::checkAutoGC()) { |
||
| 230 | $this->callbacks->push(function ($thread) { |
||
| 231 | gc_collect_cycles(); |
||
| 232 | }); |
||
| 233 | $this->eventBase->exit(); |
||
| 234 | } |
||
| 235 | |||
| 236 | $event->timeout(); |
||
| 237 | }, 1e6 * 1, 'breakMainLoopCheck'); |
||
| 238 | if (Daemon::$config->autoreload->value > 0) { |
||
| 239 | Timer::add(function ($event) { |
||
| 240 | static $n = 0; |
||
| 241 | $list = get_included_files(); |
||
| 242 | $s = sizeof($list); |
||
| 243 | if ($s > $n) { |
||
| 244 | $slice = array_map('realpath', array_slice($list, $n)); |
||
| 245 | Daemon::$process->IPCManager->sendPacket(['op' => 'addIncludedFiles', 'files' => $slice]); |
||
| 246 | $n = $s; |
||
| 247 | } |
||
| 248 | $event->timeout(); |
||
| 249 | }, 1e6 * Daemon::$config->autoreload->value, 'watchIncludedFiles'); |
||
| 250 | } |
||
| 251 | |||
| 252 | while (!$this->breakMainLoop) { |
||
| 253 | $this->callbacks->executeAll($this); |
||
| 254 | if (!$this->eventBase->dispatch()) { |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | $this->shutdown(); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Override a standard PHP function |
||
| 263 | * @param string $camelCase e.g. isUploadedFile |
||
| 264 | * @param string $real e.g. is_uploaded_file |
||
| 265 | */ |
||
| 266 | protected function override($camelCase, $real) |
||
| 267 | { |
||
| 268 | runkit_function_rename($real, $real . '_native'); |
||
| 269 | runkit_function_rename('PHPDaemon\\Thread\\' . $camelCase, $real); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Overrides native PHP functions. |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | protected function overrideNativeFuncs() |
||
| 277 | { |
||
| 278 | if (Daemon::supported(Daemon::SUPPORT_RUNKIT_INTERNAL_MODIFY)) { |
||
| 279 | function define($k, $v) |
||
| 287 | $this->override('define'); |
||
| 288 | |||
| 289 | function header() |
||
| 296 | $this->override('header'); |
||
| 297 | |||
| 298 | function isUploadedFile() |
||
| 305 | $this->override('isUploadedFile', 'is_uploaded_file'); |
||
| 306 | |||
| 307 | function moveUploadedFile() |
||
| 314 | $this->override('moveUploadedFile', 'move_uploaded_file'); |
||
| 315 | |||
| 316 | function headersSent(&$file = null, &$line = null) |
||
| 323 | |||
| 324 | //$this->override('headersSent', 'headers_sent); // Commented out due to a runkit bug |
||
| 325 | |||
| 326 | function headersList() |
||
| 333 | $this->override('headersList', 'headers_list'); |
||
| 334 | |||
| 335 | function setcookie() |
||
| 342 | $this->override('setcookie'); |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param callable $cb |
||
| 346 | */ |
||
| 347 | function registerShutdownFunction($cb) |
||
| 354 | $this->override('registerShutdownFunction', 'register_shutdown_function'); |
||
| 355 | |||
| 356 | runkit_function_copy('create_function', 'create_function_native'); |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Creates anonymous function (old-fashioned) like create_function() |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | public function createFunction($args, $body, $ttl = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Setup settings on start. |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | protected function prepareSystemEnv() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Log something |
||
| 451 | * @param string - Message. |
||
| 452 | * @param string $message |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | public function log($message) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Reloads additional config-files on-the-fly. |
||
| 462 | * @return void |
||
| 463 | */ |
||
| 464 | protected function update() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Check if we should break main loop |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | protected function breakMainLoopCheck() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Graceful restart |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function gracefulRestart() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Graceful stop |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | View Code Duplication | public function gracefulStop() |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Asks the running applications the whether we can go to shutdown current (old) worker. |
||
| 551 | * @return boolean - Ready? |
||
| 552 | */ |
||
| 553 | protected function appInstancesReloadReady() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Shutdown this worker |
||
| 570 | * @param boolean Hard? If hard, we shouldn't wait for graceful shutdown of the running applications. |
||
| 571 | * @return boolean|null Ready? |
||
| 572 | */ |
||
| 573 | protected function shutdown($hard = false) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Set current status of worker |
||
| 641 | * @param int Constant |
||
| 642 | * @return boolean Success. |
||
| 643 | */ |
||
| 644 | public function setState($int) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Handler of the SIGINT (hard shutdown) signal in worker process. |
||
| 668 | * @return void |
||
| 669 | */ |
||
| 670 | protected function sigint() |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Handler of the SIGTERM (graceful shutdown) signal in worker process. |
||
| 681 | * @return void |
||
| 682 | */ |
||
| 683 | protected function sigterm() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Handler of the SIGQUIT (graceful shutdown) signal in worker process. |
||
| 696 | * @return void |
||
| 697 | */ |
||
| 698 | protected function sigquit() |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Handler of the SIGHUP (reload config) signal in worker process. |
||
| 709 | * @return void |
||
| 710 | */ |
||
| 711 | View Code Duplication | protected function sighup() |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Handler of the SIGUSR1 (re-open log-file) signal in worker process. |
||
| 726 | * @return void |
||
| 727 | */ |
||
| 728 | protected function sigusr1() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Handler of the SIGUSR2 (graceful shutdown for update) signal in worker process. |
||
| 739 | * @return void |
||
| 740 | */ |
||
| 741 | protected function sigusr2() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Handler of the SIGTSTP (graceful stop) signal in worker process. |
||
| 752 | * @return void |
||
| 753 | */ |
||
| 754 | protected function sigtstp() |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Handler of the SIGTTIN signal in worker process. |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | protected function sigttin() |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Handler of the SIGPIPE signal in worker process. |
||
| 773 | * @return void |
||
| 774 | */ |
||
| 775 | protected function sigpipe() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Handler of non-known signals. |
||
| 781 | * @return void |
||
| 782 | */ |
||
| 783 | View Code Duplication | protected function sigunknown($signo) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Called (in master) when process is terminated |
||
| 796 | * @return void |
||
| 797 | */ |
||
| 798 | public function onTerminated() |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Destructor of worker thread. |
||
| 805 | * @return void |
||
| 806 | */ |
||
| 807 | public function __destruct() |
||
| 813 | } |
||
| 814 |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.