Complex classes like Snidel 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 Snidel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Snidel |
||
| 20 | { |
||
| 21 | /** @var string */ |
||
| 22 | const VERSION = '0.5.0'; |
||
| 23 | |||
| 24 | private $masterProcessId = null; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | private $childPids = array(); |
||
| 28 | |||
| 29 | /** @var \Ackintosh\Snidel\ForkContainer */ |
||
| 30 | private $forkContainer; |
||
| 31 | |||
| 32 | /** @var array */ |
||
| 33 | private $forks = array(); |
||
| 34 | |||
| 35 | /** @var \Ackintosh\Snidel\Error */ |
||
| 36 | private $error; |
||
| 37 | |||
| 38 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
| 39 | private $pcntl; |
||
| 40 | |||
| 41 | /** @var int */ |
||
| 42 | private $concurrency; |
||
| 43 | |||
| 44 | /** @var \Ackintosh\Snidel\Token */ |
||
| 45 | private $token; |
||
| 46 | |||
| 47 | /** @var \Ackintosh\Snidel\Log */ |
||
| 48 | private $log; |
||
| 49 | |||
| 50 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
| 51 | private $dataRepository; |
||
| 52 | |||
| 53 | /** @var bool */ |
||
| 54 | private $joined = false; |
||
| 55 | |||
| 56 | /** @var array */ |
||
| 57 | private $results = array(); |
||
|
|
|||
| 58 | |||
| 59 | /** @var int */ |
||
| 60 | private $ownerPid; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | private $signals = array( |
||
| 64 | SIGTERM, |
||
| 65 | SIGINT, |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** @var int */ |
||
| 69 | private $receivedSignal; |
||
| 70 | |||
| 71 | /** @var \Ackintosh\Snidel\Token */ |
||
| 72 | private $processToken; |
||
| 73 | |||
| 74 | /** @var bool */ |
||
| 75 | private $exceptionHasOccured = false; |
||
| 76 | |||
| 77 | public function __construct($concurrency = 5) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * sets the resource for the log. |
||
| 116 | * |
||
| 117 | * @param resource $resource |
||
| 118 | * @return void |
||
| 119 | * @codeCoverageIgnore |
||
| 120 | */ |
||
| 121 | public function setLoggingDestination($resource) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * fork process |
||
| 128 | * |
||
| 129 | * @param callable $callable |
||
| 130 | * @param mixed $args |
||
| 131 | * @param string $tag |
||
| 132 | * @return void |
||
| 133 | * @throws \RuntimeException |
||
| 134 | */ |
||
| 135 | public function fork($callable, $args = array(), $tag = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * fork master process |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | private function forkMaster() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * fork worker process |
||
| 188 | * |
||
| 189 | * @param callable $callable |
||
| 190 | * @param mixed $args |
||
| 191 | * @param string $tag |
||
| 192 | * @return void |
||
| 193 | * @throws \RuntimeException |
||
| 194 | */ |
||
| 195 | private function forkWorker($callable, $args = array(), $tag = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * fork process |
||
| 252 | * this method does't use a master / worker model. |
||
| 253 | * |
||
| 254 | * @param callable $callable |
||
| 255 | * @param mixed $args |
||
| 256 | * @param string $tag |
||
| 257 | * @param \Ackintosh\Snidel\Token $token |
||
| 258 | * @return void |
||
| 259 | * @throws \RuntimeException |
||
| 260 | */ |
||
| 261 | public function forkSimply($callable, $args = array(), $tag = null, Token $token = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * waits until all children that has forked by Snidel::forkSimply() are completed |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 328 | */ |
||
| 329 | public function waitSimply() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * waits until all tasks that queued by Snidel::fork() are completed |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | public function wait() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function hasError() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return \Ackintosh\Snidel\Error |
||
| 390 | */ |
||
| 391 | public function getError() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * gets results |
||
| 398 | * |
||
| 399 | * @param string $tag |
||
| 400 | * @return \Ackintosh\Snidel\ForkCollection |
||
| 401 | * @throws \InvalidArgumentException |
||
| 402 | */ |
||
| 403 | public function getSimply($tag = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * gets results |
||
| 422 | * |
||
| 423 | * @param string $tag |
||
| 424 | * @return \Ackintosh\Snidel\ForkCollection |
||
| 425 | * @throws \InvalidArgumentException |
||
| 426 | */ |
||
| 427 | public function get($tag = null) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * sends signal to child |
||
| 454 | * |
||
| 455 | * @param int $sig |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | private function sendSignalToChildren($sig) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * delete shared memory |
||
| 468 | * |
||
| 469 | * @return void |
||
| 470 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 471 | */ |
||
| 472 | private function deleteAllData() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * create map object |
||
| 486 | * |
||
| 487 | * @param array $args |
||
| 488 | * @param callable $callable |
||
| 489 | * @return \Ackintosh\Snidel\MapContainer |
||
| 490 | */ |
||
| 491 | public function map(Array $args, $callable) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * run map object |
||
| 498 | * |
||
| 499 | * @param \Ackintosh\Snidel\MapContainer |
||
| 500 | * @return array |
||
| 501 | * @throws \RuntimeException |
||
| 502 | */ |
||
| 503 | public function run(MapContainer $mapContainer) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * fork the first processing of the map container |
||
| 518 | * |
||
| 519 | * @param \Ackintosh\Snidel\MapContainer |
||
| 520 | * @return void |
||
| 521 | * @throws \RuntimeException |
||
| 522 | */ |
||
| 523 | private function forkTheFirstProcessing(MapContainer $mapContainer) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * waits and connects the process of map container |
||
| 538 | * |
||
| 539 | * @param \Ackintosh\Snidel\MapContainer |
||
| 540 | * @return void |
||
| 541 | * @throws \RuntimeException |
||
| 542 | */ |
||
| 543 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * gets results of map container |
||
| 588 | * |
||
| 589 | * @param \Ackintosh\Snidel\MapContainer |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | private function getResultsOf(MapContainer $mapContainer) |
||
| 601 | |||
| 602 | private function _exit($status = 0) |
||
| 606 | |||
| 607 | public function __destruct() |
||
| 639 | } |
||
| 640 |
This check marks private properties in classes that are never used. Those properties can be removed.