Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Container |
||
| 14 | { |
||
| 15 | /** @var int */ |
||
| 16 | private $ownerPid; |
||
| 17 | |||
| 18 | /** @var int */ |
||
| 19 | private $masterPid; |
||
| 20 | |||
| 21 | /** @var \Ackintosh\Snidel\Result\Result[] */ |
||
| 22 | private $results = []; |
||
| 23 | |||
| 24 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
| 25 | private $pcntl; |
||
| 26 | |||
| 27 | /** @var \Ackintosh\Snidel\Error */ |
||
| 28 | private $error; |
||
| 29 | |||
| 30 | /** @var \Ackintosh\Snidel\Task\QueueInterface */ |
||
| 31 | private $taskQueue; |
||
| 32 | |||
| 33 | /** @var \Ackintosh\Snidel\Result\QueueInterface */ |
||
| 34 | private $resultQueue; |
||
| 35 | |||
| 36 | /** @var \Ackintosh\Snidel\Log */ |
||
| 37 | private $log; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | private $signals = [ |
||
| 41 | SIGTERM, |
||
| 42 | SIGINT, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** @var \Ackintosh\Snidel\Config */ |
||
| 46 | private $config; |
||
| 47 | |||
| 48 | /** @var \Ackintosh\Snidel\QueueFactory */ |
||
| 49 | private $queueFactory; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | private $receivedSignal; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param int $ownerPid |
||
| 56 | */ |
||
| 57 | public function __construct($ownerPid, $log, Config $config) |
||
| 58 | { |
||
| 59 | $this->ownerPid = $ownerPid; |
||
| 60 | $this->log = $log; |
||
| 61 | $this->config = $config; |
||
| 62 | $this->pcntl = new Pcntl(); |
||
| 63 | $this->error = new Error(); |
||
| 64 | $this->queueFactory = new QueueFactory($config); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param \Ackintosh\Snidel\Task |
||
| 69 | * @return void |
||
| 70 | * @throws \RuntimeException |
||
| 71 | */ |
||
| 72 | public function enqueue($task) |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $this->taskQueue->enqueue($task); |
||
| 76 | } catch (\RuntimeException $e) { |
||
| 77 | throw $e; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | public function queuedCount() |
||
| 85 | { |
||
| 86 | if (is_null($this->taskQueue)) { |
||
| 87 | return 0; |
||
| 88 | } |
||
| 89 | |||
| 90 | return $this->taskQueue->queuedCount(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return \Ackintosh\Snidel\Result\Result |
||
| 95 | */ |
||
| 96 | private function dequeue() |
||
| 97 | { |
||
| 98 | return $this->resultQueue->dequeue(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return int |
||
| 103 | */ |
||
| 104 | public function dequeuedCount() |
||
| 105 | { |
||
| 106 | if (is_null($this->resultQueue)) { |
||
| 107 | return 0; |
||
| 108 | } |
||
| 109 | |||
| 110 | return $this->resultQueue->dequeuedCount(); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * fork process |
||
| 115 | * |
||
| 116 | * @return \Ackintosh\Snidel\Fork\Fork |
||
| 117 | * @throws \RuntimeException |
||
| 118 | */ |
||
| 119 | private function fork() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * fork master process |
||
| 133 | * |
||
| 134 | * @return int $masterPid |
||
| 135 | */ |
||
| 136 | public function forkMaster() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * fork worker process |
||
| 196 | * |
||
| 197 | * @return \Ackintosh\Snidel\Worker |
||
| 198 | * @throws \RuntimeException |
||
| 199 | */ |
||
| 200 | private function forkWorker() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function existsMaster() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * send signal to master process |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function sendSignalToMaster($sig = SIGTERM) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * |
||
| 281 | * @param string $tag |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function hasTag($tag) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function wait() |
||
| 310 | |||
| 311 | public function getCollection($tag = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * return results |
||
| 325 | * |
||
| 326 | * @param string $tag |
||
| 327 | * @return \Ackintosh\Snidel\Result\Collection |
||
| 328 | */ |
||
| 329 | private function getCollectionWithTag($tag) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function hasError() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return \Ackintosh\Snidel\Error |
||
| 354 | */ |
||
| 355 | public function getError() |
||
| 359 | |||
| 360 | public function __destruct() |
||
| 365 | } |
||
| 366 |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope