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) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * sets the resource for the log. |
||
| 119 | * |
||
| 120 | * @param resource $resource |
||
| 121 | * @return void |
||
| 122 | * @codeCoverageIgnore |
||
| 123 | */ |
||
| 124 | public function setLoggingDestination($resource) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * fork process |
||
| 131 | * |
||
| 132 | * @param callable $callable |
||
| 133 | * @param mixed $args |
||
| 134 | * @param string $tag |
||
| 135 | * @return void |
||
| 136 | * @throws \RuntimeException |
||
| 137 | */ |
||
| 138 | public function fork($callable, $args = array(), $tag = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * fork master process |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | private function forkMaster() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * fork worker process |
||
| 191 | * |
||
| 192 | * @param callable $callable |
||
| 193 | * @param mixed $args |
||
| 194 | * @param string $tag |
||
| 195 | * @return void |
||
| 196 | * @throws \RuntimeException |
||
| 197 | */ |
||
| 198 | private function forkWorker($callable, $args = array(), $tag = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * fork process |
||
| 257 | * this method does't use a master / worker model. |
||
| 258 | * |
||
| 259 | * @param callable $callable |
||
| 260 | * @param mixed $args |
||
| 261 | * @param string $tag |
||
| 262 | * @param \Ackintosh\Snidel\Token $token |
||
| 263 | * @return void |
||
| 264 | * @throws \RuntimeException |
||
| 265 | */ |
||
| 266 | public function forkSimply($callable, $args = array(), $tag = null, Token $token = null) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * waits until all children that has forked by Snidel::forkSimply() are completed |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 333 | */ |
||
| 334 | public function waitSimply() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * waits until all tasks that queued by Snidel::fork() are completed |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | public function wait() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function hasError() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return \Ackintosh\Snidel\Error |
||
| 395 | */ |
||
| 396 | public function getError() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * gets results |
||
| 403 | * |
||
| 404 | * @param string $tag |
||
| 405 | * @return \Ackintosh\Snidel\ForkCollection |
||
| 406 | * @throws \InvalidArgumentException |
||
| 407 | */ |
||
| 408 | public function getSimply($tag = null) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * gets results |
||
| 427 | * |
||
| 428 | * @param string $tag |
||
| 429 | * @return \Ackintosh\Snidel\ForkCollection |
||
| 430 | * @throws \InvalidArgumentException |
||
| 431 | */ |
||
| 432 | public function get($tag = null) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * sends signal to child |
||
| 459 | * |
||
| 460 | * @param int $sig |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | public function sendSignalToChildren($sig) |
||
| 470 | |||
| 471 | public function setReceivedSignal($sig) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * delete shared memory |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 481 | */ |
||
| 482 | private function deleteAllData() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * create map object |
||
| 496 | * |
||
| 497 | * @param array $args |
||
| 498 | * @param callable $callable |
||
| 499 | * @return \Ackintosh\Snidel\MapContainer |
||
| 500 | */ |
||
| 501 | public function map(Array $args, $callable) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * run map object |
||
| 508 | * |
||
| 509 | * @param \Ackintosh\Snidel\MapContainer |
||
| 510 | * @return array |
||
| 511 | * @throws \RuntimeException |
||
| 512 | */ |
||
| 513 | public function run(MapContainer $mapContainer) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * fork the first processing of the map container |
||
| 528 | * |
||
| 529 | * @param \Ackintosh\Snidel\MapContainer |
||
| 530 | * @return void |
||
| 531 | * @throws \RuntimeException |
||
| 532 | */ |
||
| 533 | private function forkTheFirstProcessing(MapContainer $mapContainer) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * waits and connects the process of map container |
||
| 548 | * |
||
| 549 | * @param \Ackintosh\Snidel\MapContainer |
||
| 550 | * @return void |
||
| 551 | * @throws \RuntimeException |
||
| 552 | */ |
||
| 553 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * gets results of map container |
||
| 598 | * |
||
| 599 | * @param \Ackintosh\Snidel\MapContainer |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | private function getResultsOf(MapContainer $mapContainer) |
||
| 611 | |||
| 612 | private function _exit($status = 0) |
||
| 616 | |||
| 617 | public function __destruct() |
||
| 649 | } |
||
| 650 |
This check marks private properties in classes that are never used. Those properties can be removed.