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 |
||
| 16 | class Snidel |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | const VERSION = '0.4.0'; |
||
| 20 | |||
| 21 | /** @var array */ |
||
| 22 | private $childPids = array(); |
||
| 23 | |||
| 24 | /** @var \Ackintosh\Snidel\ForkContainer */ |
||
| 25 | private $forkContainer; |
||
| 26 | |||
| 27 | /** @var \Ackintosh\Snidel\Error */ |
||
| 28 | private $error; |
||
| 29 | |||
| 30 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
| 31 | private $pcntl; |
||
| 32 | |||
| 33 | /** @var int */ |
||
| 34 | private $concurrency; |
||
| 35 | |||
| 36 | /** @var \Ackintosh\Snidel\Token */ |
||
| 37 | private $token; |
||
| 38 | |||
| 39 | /** @var \Ackintosh\Snidel\Log */ |
||
| 40 | private $log; |
||
| 41 | |||
| 42 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
| 43 | private $dataRepository; |
||
| 44 | |||
| 45 | /** @var bool */ |
||
| 46 | private $joined = false; |
||
| 47 | |||
| 48 | /** @var array */ |
||
| 49 | private $results = array(); |
||
|
|
|||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | private $ownerPid; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $signals = array( |
||
| 56 | SIGTERM, |
||
| 57 | SIGINT, |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** @var int */ |
||
| 61 | private $receivedSignal; |
||
| 62 | |||
| 63 | /** @var \Ackintosh\Snidel\Token */ |
||
| 64 | private $processToken; |
||
| 65 | |||
| 66 | /** @var bool */ |
||
| 67 | private $exceptionHasOccured = false; |
||
| 68 | |||
| 69 | public function __construct($concurrency = 5) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * sets the resource for the log. |
||
| 106 | * |
||
| 107 | * @param resource $resource |
||
| 108 | * @return void |
||
| 109 | * @codeCoverageIgnore |
||
| 110 | */ |
||
| 111 | public function setLoggingDestination($resource) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * fork process |
||
| 118 | * |
||
| 119 | * @param callable $callable |
||
| 120 | * @param mixed $args |
||
| 121 | * @param string $tag |
||
| 122 | * @return int $pid forked PID of forked child process |
||
| 123 | * @throws \RuntimeException |
||
| 124 | */ |
||
| 125 | public function fork($callable, $args = array(), $tag = null, Token $token = null) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * waits until all children are completed |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 184 | */ |
||
| 185 | public function wait() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function hasError() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return \Ackintosh\Snidel\Error |
||
| 228 | */ |
||
| 229 | public function getError() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * gets results |
||
| 236 | * |
||
| 237 | * @param string $tag |
||
| 238 | * @return array $ret |
||
| 239 | * @throws \InvalidArgumentException |
||
| 240 | */ |
||
| 241 | public function get($tag = null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * gets results with tag |
||
| 265 | * |
||
| 266 | * @param string $tag |
||
| 267 | * @return array $results |
||
| 268 | * @throws \InvalidArgumentException |
||
| 269 | */ |
||
| 270 | private function getWithTag($tag) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * sends signal to child |
||
| 286 | * |
||
| 287 | * @param int $sig |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | private function sendSignalToChildren($sig) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * delete shared memory |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 303 | */ |
||
| 304 | private function deleteAllData() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * create map object |
||
| 318 | * |
||
| 319 | * @param array $args |
||
| 320 | * @param callable $callable |
||
| 321 | * @return \Ackintosh\Snidel\MapContainer |
||
| 322 | */ |
||
| 323 | public function map(Array $args, $callable) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * run map object |
||
| 330 | * |
||
| 331 | * @param \Ackintosh\Snidel\MapContainer |
||
| 332 | * @return array |
||
| 333 | * @throws \RuntimeException |
||
| 334 | */ |
||
| 335 | public function run(MapContainer $mapContainer) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * fork the first processing of the map container |
||
| 350 | * |
||
| 351 | * @param \Ackintosh\Snidel\MapContainer |
||
| 352 | * @return void |
||
| 353 | * @throws \RuntimeException |
||
| 354 | */ |
||
| 355 | private function forkTheFirstProcessing(MapContainer $mapContainer) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * waits and connects the process of map container |
||
| 370 | * |
||
| 371 | * @param \Ackintosh\Snidel\MapContainer |
||
| 372 | * @return void |
||
| 373 | * @throws \RuntimeException |
||
| 374 | */ |
||
| 375 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * gets results of map container |
||
| 420 | * |
||
| 421 | * @param \Ackintosh\Snidel\MapContainer |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | private function getResultsOf(MapContainer $mapContainer) |
||
| 433 | |||
| 434 | private function _exit($status = 0) |
||
| 438 | |||
| 439 | public function __destruct() |
||
| 463 | } |
||
| 464 |
This check marks private properties in classes that are never used. Those properties can be removed.