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.6.3'; |
||
| 20 | |||
| 21 | /** @var \Ackintosh\Snidel\ForkContainer */ |
||
| 22 | private $forkContainer; |
||
| 23 | |||
| 24 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
| 25 | private $pcntl; |
||
| 26 | |||
| 27 | /** @var int */ |
||
| 28 | private $concurrency; |
||
| 29 | |||
| 30 | /** @var \Ackintosh\Snidel\Log */ |
||
| 31 | private $log; |
||
| 32 | |||
| 33 | /** @var bool */ |
||
| 34 | private $joined = false; |
||
| 35 | |||
| 36 | /** @var int */ |
||
| 37 | private $ownerPid; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | private $signals = array( |
||
| 41 | SIGTERM, |
||
| 42 | SIGINT, |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | private $receivedSignal; |
||
| 47 | |||
| 48 | /** @var \Ackintosh\Snidel\Token */ |
||
| 49 | private $processToken; |
||
|
|
|||
| 50 | |||
| 51 | /** @var bool */ |
||
| 52 | private $exceptionHasOccured = false; |
||
| 53 | |||
| 54 | public function __construct($concurrency = 5) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * sets the resource for the log. |
||
| 85 | * |
||
| 86 | * @param resource $resource |
||
| 87 | * @return void |
||
| 88 | * @codeCoverageIgnore |
||
| 89 | */ |
||
| 90 | public function setLoggingDestination($resource) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * this method uses master / worker model. |
||
| 97 | * |
||
| 98 | * @param callable $callable |
||
| 99 | * @param mixed $args |
||
| 100 | * @param string $tag |
||
| 101 | * @return void |
||
| 102 | * @throws \RuntimeException |
||
| 103 | */ |
||
| 104 | public function fork($callable, $args = array(), $tag = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * fork process |
||
| 123 | * the processes which forked are wait for token. |
||
| 124 | * |
||
| 125 | * @param callable $callable |
||
| 126 | * @param mixed $args |
||
| 127 | * @param string $tag |
||
| 128 | * @param \Ackintosh\Snidel\Token $token |
||
| 129 | * @return void |
||
| 130 | * @throws \RuntimeException |
||
| 131 | */ |
||
| 132 | private function forkChild(Token $token, $callable, $args = array(), $tag = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * waits until all tasks that queued by Snidel::fork() are completed |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function wait() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function hasError() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return \Ackintosh\Snidel\Error |
||
| 219 | */ |
||
| 220 | public function getError() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * gets results |
||
| 227 | * |
||
| 228 | * @param string $tag |
||
| 229 | * @return \Ackintosh\Snidel\ForkCollection |
||
| 230 | * @throws \InvalidArgumentException |
||
| 231 | */ |
||
| 232 | public function get($tag = null) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * sends signal to child |
||
| 246 | * |
||
| 247 | * @param int $sig |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function sendSignalToChildren($sig) |
||
| 257 | |||
| 258 | public function setReceivedSignal($sig) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * delete shared memory |
||
| 265 | * |
||
| 266 | * @return void |
||
| 267 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
| 268 | */ |
||
| 269 | private function deleteAllData() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * create map object |
||
| 281 | * |
||
| 282 | * @param array $args |
||
| 283 | * @param callable $callable |
||
| 284 | * @return \Ackintosh\Snidel\MapContainer |
||
| 285 | */ |
||
| 286 | public function map(Array $args, $callable) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * run map object |
||
| 293 | * |
||
| 294 | * @param \Ackintosh\Snidel\MapContainer |
||
| 295 | * @return array |
||
| 296 | * @throws \RuntimeException |
||
| 297 | */ |
||
| 298 | public function run(MapContainer $mapContainer) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * fork the first processing of the map container |
||
| 314 | * |
||
| 315 | * @param \Ackintosh\Snidel\MapContainer |
||
| 316 | * @return void |
||
| 317 | * @throws \RuntimeException |
||
| 318 | */ |
||
| 319 | private function forkTheFirstProcessing(MapContainer $mapContainer, $token) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * waits and connects the process of map container |
||
| 334 | * |
||
| 335 | * @param \Ackintosh\Snidel\MapContainer |
||
| 336 | * @return void |
||
| 337 | * @throws \RuntimeException |
||
| 338 | */ |
||
| 339 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * gets results of map container |
||
| 382 | * |
||
| 383 | * @param \Ackintosh\Snidel\MapContainer |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | private function getResultsOf(MapContainer $mapContainer) |
||
| 395 | |||
| 396 | private function _exit($status = 0) |
||
| 400 | |||
| 401 | public function __destruct() |
||
| 432 | } |
||
| 433 |
This check marks private properties in classes that are never used. Those properties can be removed.