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 |
||
18 | class Snidel |
||
19 | { |
||
20 | /** @var string */ |
||
21 | const VERSION = '0.6.0'; |
||
22 | |||
23 | private $masterProcessId = null; |
||
24 | |||
25 | /** @var array */ |
||
26 | private $childPids = array(); |
||
27 | |||
28 | /** @var \Ackintosh\Snidel\ForkContainer */ |
||
29 | private $forkContainer; |
||
30 | |||
31 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
32 | private $pcntl; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $concurrency; |
||
36 | |||
37 | /** @var \Ackintosh\Snidel\Token */ |
||
38 | private $token; |
||
39 | |||
40 | /** @var \Ackintosh\Snidel\Log */ |
||
41 | private $log; |
||
42 | |||
43 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
44 | private $dataRepository; |
||
45 | |||
46 | /** @var bool */ |
||
47 | private $joined = false; |
||
48 | |||
49 | /** @var int */ |
||
50 | private $ownerPid; |
||
51 | |||
52 | /** @var array */ |
||
53 | private $signals = array( |
||
54 | SIGTERM, |
||
55 | SIGINT, |
||
56 | ); |
||
57 | |||
58 | /** @var int */ |
||
59 | private $receivedSignal; |
||
60 | |||
61 | /** @var \Ackintosh\Snidel\Token */ |
||
62 | private $processToken; |
||
63 | |||
64 | /** @var bool */ |
||
65 | private $exceptionHasOccured = false; |
||
66 | |||
67 | 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 | * this method uses master / worker model. |
||
118 | * |
||
119 | * @param callable $callable |
||
120 | * @param mixed $args |
||
121 | * @param string $tag |
||
122 | * @return void |
||
123 | * @throws \RuntimeException |
||
124 | */ |
||
125 | public function fork($callable, $args = array(), $tag = null) |
||
139 | |||
140 | /** |
||
141 | * fork master process |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | private function forkMaster() |
||
174 | |||
175 | /** |
||
176 | * fork worker process |
||
177 | * |
||
178 | * @param \Ackintosh\Snidel\Task |
||
179 | * @return void |
||
180 | * @throws \RuntimeException |
||
181 | */ |
||
182 | private function forkWorker($task) |
||
229 | |||
230 | /** |
||
231 | * fork process |
||
232 | * the processes which forked are wait for token. |
||
233 | * |
||
234 | * @param callable $callable |
||
235 | * @param mixed $args |
||
236 | * @param string $tag |
||
237 | * @param \Ackintosh\Snidel\Token $token |
||
238 | * @return void |
||
239 | * @throws \RuntimeException |
||
240 | */ |
||
241 | private function prefork($callable, $args = array(), $tag = null, Token $token = null) |
||
296 | |||
297 | /** |
||
298 | * waits until all tasks that queued by Snidel::fork() are completed |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | public function wait() |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | public function hasError() |
||
315 | |||
316 | /** |
||
317 | * @return \Ackintosh\Snidel\Error |
||
318 | */ |
||
319 | public function getError() |
||
323 | |||
324 | /** |
||
325 | * gets results |
||
326 | * |
||
327 | * @param string $tag |
||
328 | * @return \Ackintosh\Snidel\ForkCollection |
||
329 | * @throws \InvalidArgumentException |
||
330 | */ |
||
331 | public function get($tag = null) |
||
343 | |||
344 | /** |
||
345 | * sends signal to child |
||
346 | * |
||
347 | * @param int $sig |
||
348 | * @return void |
||
349 | */ |
||
350 | public function sendSignalToChildren($sig) |
||
357 | |||
358 | public function setReceivedSignal($sig) |
||
362 | |||
363 | /** |
||
364 | * delete shared memory |
||
365 | * |
||
366 | * @return void |
||
367 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
368 | */ |
||
369 | private function deleteAllData() |
||
380 | |||
381 | /** |
||
382 | * create map object |
||
383 | * |
||
384 | * @param array $args |
||
385 | * @param callable $callable |
||
386 | * @return \Ackintosh\Snidel\MapContainer |
||
387 | */ |
||
388 | public function map(Array $args, $callable) |
||
392 | |||
393 | /** |
||
394 | * run map object |
||
395 | * |
||
396 | * @param \Ackintosh\Snidel\MapContainer |
||
397 | * @return array |
||
398 | * @throws \RuntimeException |
||
399 | */ |
||
400 | public function run(MapContainer $mapContainer) |
||
412 | |||
413 | /** |
||
414 | * fork the first processing of the map container |
||
415 | * |
||
416 | * @param \Ackintosh\Snidel\MapContainer |
||
417 | * @return void |
||
418 | * @throws \RuntimeException |
||
419 | */ |
||
420 | private function forkTheFirstProcessing(MapContainer $mapContainer) |
||
432 | |||
433 | /** |
||
434 | * waits and connects the process of map container |
||
435 | * |
||
436 | * @param \Ackintosh\Snidel\MapContainer |
||
437 | * @return void |
||
438 | * @throws \RuntimeException |
||
439 | */ |
||
440 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
482 | |||
483 | /** |
||
484 | * gets results of map container |
||
485 | * |
||
486 | * @param \Ackintosh\Snidel\MapContainer |
||
487 | * @return array |
||
488 | */ |
||
489 | private function getResultsOf(MapContainer $mapContainer) |
||
498 | |||
499 | private function _exit($status = 0) |
||
503 | |||
504 | public function __destruct() |
||
535 | } |
||
536 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.