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) |
||
| 133 | { |
||
| 134 | $task = new Task($callable, $args, $tag); |
||
| 135 | |||
| 136 | try { |
||
| 137 | $fork = $this->forkContainer->fork($task); |
||
| 138 | } catch (\RuntimeException $e) { |
||
| 139 | $this->log->error($e->getMessage()); |
||
| 140 | throw $e; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (getmypid() === $this->ownerPid) { |
||
| 144 | // parent |
||
| 145 | $this->log->info('created child process. pid: ' . $fork->getPid()); |
||
| 146 | } else { |
||
| 147 | // @codeCoverageIgnoreStart |
||
| 148 | // child |
||
| 149 | foreach ($this->signals as $sig) { |
||
| 150 | $this->pcntl->signal($sig, SIG_DFL, true); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * in php5.3, we can not use $this in anonymous functions |
||
| 155 | */ |
||
| 156 | $log = $this->log; |
||
| 157 | $resultHasWritten = false; |
||
| 158 | register_shutdown_function(function () use (&$resultHasWritten, $fork, $log, $token) { |
||
| 159 | if (!$resultHasWritten) { |
||
| 160 | $dataRepository = new DataRepository(); |
||
| 161 | $data = $dataRepository->load(getmypid()); |
||
| 162 | $result = new Result(); |
||
| 163 | $result->setFailure(); |
||
| 164 | $result->setFork($fork); |
||
| 165 | try { |
||
| 166 | $data->write($result); |
||
| 167 | } catch (SharedMemoryControlException $e) { |
||
| 168 | throw $e; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $log->info('<-- return token.'); |
||
| 173 | $token->back(); |
||
| 174 | }); |
||
| 175 | |||
| 176 | $log->info('--> waiting for the token come around.'); |
||
| 177 | if ($token->accept()) { |
||
| 178 | $log->info('----> started the function.'); |
||
| 179 | $result = $fork->executeTask(); |
||
| 180 | $log->info('<---- completed the function.'); |
||
| 181 | $dataRepository = new DataRepository(); |
||
| 182 | $data = $dataRepository->load(getmypid()); |
||
| 183 | try { |
||
| 184 | $data->write($result); |
||
| 185 | } catch (SharedMemoryControlException $e) { |
||
| 186 | throw $e; |
||
| 187 | } |
||
| 188 | $resultHasWritten = true; |
||
| 189 | } |
||
| 190 | |||
| 191 | exit; |
||
| 192 | // @codeCoverageIgnoreEnd |
||
| 193 | } |
||
| 194 | |||
| 195 | return $fork->getPid(); |
||
| 196 | } |
||
| 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\Result\Collection |
||
| 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 | public function __destruct() |
||
| 427 | } |
||
| 428 |
This check marks private properties in classes that are never used. Those properties can be removed.