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 $tagsToPids = array(); |
||
| 56 | |||
| 57 | /** @var array */ |
||
| 58 | private $signals = array( |
||
| 59 | SIGTERM, |
||
| 60 | SIGINT, |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** @var int */ |
||
| 64 | private $receivedSignal; |
||
| 65 | |||
| 66 | /** @var \Ackintosh\Snidel\Token */ |
||
| 67 | private $processToken; |
||
| 68 | |||
| 69 | /** @var array */ |
||
| 70 | private $processInformation = array(); |
||
| 71 | |||
| 72 | /** @var bool */ |
||
| 73 | private $exceptionHasOccured = false; |
||
| 74 | |||
| 75 | public function __construct($concurrency = 5) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * sets the resource for the log. |
||
| 96 | * |
||
| 97 | * @param resource $resource |
||
| 98 | * @return void |
||
| 99 | * @codeCoverageIgnore |
||
| 100 | */ |
||
| 101 | public function setLoggingDestination($resource) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * fork process |
||
| 108 | * |
||
| 109 | * @param callable $callable |
||
| 110 | * @param array $args |
||
| 111 | * @param string $tag |
||
| 112 | * @return int $pid forked PID of forked child process |
||
| 113 | * @throws \RuntimeException |
||
| 114 | */ |
||
| 115 | public function fork($callable, $args = array(), $tag = null, Token $token = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * waits until all children are completed |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 177 | */ |
||
| 178 | public function wait() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function hasError() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return \Ackintosh\Snidel\Error |
||
| 223 | */ |
||
| 224 | public function getError() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * gets results |
||
| 231 | * |
||
| 232 | * @param string $tag |
||
| 233 | * @return array $ret |
||
| 234 | * @throws \InvalidArgumentException |
||
| 235 | */ |
||
| 236 | public function get($tag = null) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * gets results with tag |
||
| 255 | * |
||
| 256 | * @param string $tag |
||
| 257 | * @return array $results |
||
| 258 | * @throws \InvalidArgumentException |
||
| 259 | */ |
||
| 260 | private function getWithTag($tag) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param int $sig |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | public function signalHandler($sig) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * sends signal to child |
||
| 295 | * |
||
| 296 | * @param int $sig |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | private function sendSignalToChildren($sig) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * delete shared memory |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 312 | */ |
||
| 313 | private function deleteAllData() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * create map object |
||
| 327 | * |
||
| 328 | * @param array $args |
||
| 329 | * @param callable $callable |
||
| 330 | * @return \Ackintosh\Snidel\MapContainer |
||
| 331 | */ |
||
| 332 | public function map(Array $args, $callable) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * run map object |
||
| 339 | * |
||
| 340 | * @param \Ackintosh\Snidel\MapContainer |
||
| 341 | * @return array |
||
| 342 | * @throws \RuntimeException |
||
| 343 | */ |
||
| 344 | public function run(MapContainer $mapContainer) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * fork the first processing of the map container |
||
| 359 | * |
||
| 360 | * @param \Ackintosh\Snidel\MapContainer |
||
| 361 | * @return void |
||
| 362 | * @throws \RuntimeException |
||
| 363 | */ |
||
| 364 | private function forkTheFirstProcessing(MapContainer $mapContainer) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * waits and connects the process of map container |
||
| 379 | * |
||
| 380 | * @param \Ackintosh\Snidel\MapContainer |
||
| 381 | * @return void |
||
| 382 | * @throws \RuntimeException |
||
| 383 | */ |
||
| 384 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * gets results of map container |
||
| 431 | * |
||
| 432 | * @param \Ackintosh\Snidel\MapContainer |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | private function getResultsOf(MapContainer $mapContainer) |
||
| 444 | |||
| 445 | private function _exit($status = 0) |
||
| 449 | |||
| 450 | public function __destruct() |
||
| 474 | } |
||
| 475 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.