Complex classes like Co 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 Co, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Co |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Special constants used for Generator yielding keys. |
||
18 | * |
||
19 | * @const Co::RETURN_WITH Treat yielded value as returned value. |
||
20 | * This is for PHP 5.5 ~ 5.6. |
||
21 | * @const Co::UNSAFE Allow current yield to throw Exceptions. |
||
22 | * @const Co::SAFE Forbid current yield to throw Exceptions. |
||
23 | * Exceptions are just to be returned. |
||
24 | */ |
||
25 | const RETURN_WITH = '__RETURN_WITH__'; |
||
26 | const RETURN_ = '__RETURN_WITH__'; // alias |
||
27 | const RET = '__RETURN_WITH__'; // alias |
||
28 | const RTN = '__RETURN_WITH__'; // alias |
||
29 | const UNSAFE = '__UNSAFE__'; |
||
30 | const SAFE = '__SAFE__'; |
||
31 | |||
32 | /** |
||
33 | * Static default options. |
||
34 | */ |
||
35 | private static $defaults = array( |
||
36 | 'throw' => true, // Throw CURLExceptions? |
||
37 | 'pipeline' => false, // Use HTTP/1.1 pipelining? |
||
38 | 'multiplex' => true, // Use HTTP/2 multiplexing? |
||
39 | 'interval' => 0.5, // curl_multi_select() timeout |
||
40 | 'concurrency' => 6, // Limit of TCP connections |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Execution instance is stored here. |
||
45 | */ |
||
46 | private static $self; |
||
47 | |||
48 | /** |
||
49 | * Instance properties |
||
50 | * |
||
51 | * *Stack ID* means... |
||
52 | * - Generator ID |
||
53 | * - "wait" (Co::wait calls) |
||
54 | * - "async" (Co::async calls) |
||
55 | */ |
||
56 | private $options = array(); |
||
57 | private $mh; // curl_multi_init() |
||
58 | private $count = 0; // count(curl_multi_add_handle called) |
||
59 | private $queue = array(); // cURL resources over concurrency limits are temporalily stored here |
||
60 | private $tree = array(); // array<*Stack ID*, mixed> |
||
61 | private $values = array(); // array<*Stack ID*|*cURL ID*, Generator|resource> |
||
62 | private $value_to_parent = array(); // array<*Stack ID*|*cURL ID*, *Stack ID*> |
||
63 | private $value_to_children = array(); // array<*Stack ID*, array<*Stack ID*|*cURL ID*, true>> |
||
64 | private $value_to_keylist = array(); // array<*Stack ID*|*cURL ID*, array<mixed>> |
||
65 | |||
66 | /** |
||
67 | * Override or get default settings. |
||
68 | * |
||
69 | * @access public |
||
70 | * @static |
||
71 | * @param array $options |
||
72 | */ |
||
73 | public static function setDefaultOptions(array $options) |
||
81 | |||
82 | /** |
||
83 | * Wait all cURL requests to be completed. |
||
84 | * Options override static defaults. |
||
85 | * |
||
86 | * @access public |
||
87 | * @static |
||
88 | * @param mixed $value |
||
89 | * @param array $options |
||
90 | * @see self::__construct() |
||
91 | */ |
||
92 | public static function wait($value, array $options = array()) |
||
113 | |||
114 | /** |
||
115 | * Parallel execution along with Co::async(). |
||
116 | * This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback. |
||
117 | * |
||
118 | * @access public |
||
119 | * @static |
||
120 | * @param mixed $value |
||
121 | * @see self::__construct() |
||
122 | */ |
||
123 | public static function async($value) |
||
134 | |||
135 | /** |
||
136 | * Internal constructor. |
||
137 | * |
||
138 | * @access private |
||
139 | * @param array $options |
||
140 | * @see self::initialize(), self::run() |
||
141 | */ |
||
142 | 4 | private function __construct(array $options) |
|
151 | |||
152 | /** |
||
153 | * Call curl_multi_add_handle or push into waiting queue. |
||
154 | * |
||
155 | * @access private |
||
156 | * @param resource $curl |
||
157 | */ |
||
158 | 1 | private function enqueue($curl) |
|
179 | |||
180 | /** |
||
181 | * Set or overwrite tree of return values. |
||
182 | * |
||
183 | * @access private |
||
184 | * @param mixed $value mixed |
||
185 | * @param string $parent_hash *Stack ID* |
||
186 | * @param array $keylist Queue of keys for its hierarchy. |
||
187 | */ |
||
188 | 1 | private function setTree($value, $parent_hash, array $keylist = array()) |
|
199 | |||
200 | /** |
||
201 | * Unset tree of return values. |
||
202 | * |
||
203 | * @access private |
||
204 | * @param string $hash *Stack ID* or *cURL ID* |
||
205 | */ |
||
206 | 1 | private function unsetTree($hash) |
|
217 | |||
218 | /** |
||
219 | * Set table of dependencies. |
||
220 | * |
||
221 | * @access private |
||
222 | * @param Generator|resource $value |
||
223 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
224 | * @param array $keylist Queue of keys for its hierarchy. |
||
225 | */ |
||
226 | 1 | private function setTable($value, $parent_hash, array $keylist = array()) |
|
234 | |||
235 | /** |
||
236 | * Unset table of dependencies. |
||
237 | * |
||
238 | * @access private |
||
239 | * @param string $hash *Stack ID* or *cURL ID* |
||
240 | */ |
||
241 | private function unsetTable($hash) |
||
263 | |||
264 | /** |
||
265 | * Run curl_multi_exec() loop. |
||
266 | * |
||
267 | * @access private |
||
268 | * @see self::updateCurl(), self::enqueue() |
||
269 | */ |
||
270 | private function run() |
||
300 | |||
301 | /** |
||
302 | * Unset table of dependencies. |
||
303 | * |
||
304 | * @access private |
||
305 | * @param mixed $value |
||
306 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
307 | * @param array $keylist Queue of keys for its hierarchy. |
||
308 | * @return bool Enqueued? |
||
309 | */ |
||
310 | private function initialize($value, $parent_hash, array $keylist = array()) |
||
359 | |||
360 | /** |
||
361 | * Update tree with cURL result. |
||
362 | * |
||
363 | * @access private |
||
364 | * @param resource $value |
||
365 | * @param int $errno |
||
366 | * @see self::updateGenerator() |
||
367 | */ |
||
368 | private function updateCurl($value, $errno) |
||
400 | |||
401 | /** |
||
402 | * Check current Generator can throw a CURLException. |
||
403 | * |
||
404 | * @access private |
||
405 | * @param Generator $value |
||
406 | * @return bool |
||
407 | */ |
||
408 | private function canThrow(\Generator $value) |
||
425 | |||
426 | /** |
||
427 | * Update tree with updateCurl() result. |
||
428 | * |
||
429 | * @access private |
||
430 | * @param Generator $value |
||
431 | */ |
||
432 | private function updateGenerator(\Generator $value) |
||
462 | |||
463 | /** |
||
464 | * Validate options. |
||
465 | * |
||
466 | * @access private |
||
467 | * @static |
||
468 | * @param array $options |
||
469 | * @return array |
||
470 | */ |
||
471 | 1 | private static function validateOptions(array $options) |
|
498 | |||
499 | /** |
||
500 | * Normalize value. |
||
501 | * |
||
502 | * @access private |
||
503 | * @static |
||
504 | * @param mixed $value |
||
505 | * @return miexed |
||
506 | */ |
||
507 | 1 | private static function normalize($value) |
|
519 | |||
520 | /** |
||
521 | * Check if a Generator is running. |
||
522 | * This method supports psuedo return with Co::RETURN_WITH. |
||
523 | * |
||
524 | * @access private |
||
525 | * @static |
||
526 | * @param Generator $value |
||
527 | * @return bool |
||
528 | */ |
||
529 | 1 | private static function isGeneratorRunning(\Generator $value) |
|
534 | |||
535 | /** |
||
536 | * Get return value from a Generator. |
||
537 | * This method supports psuedo return with Co::RETURN_WITH. |
||
538 | * |
||
539 | * @access private |
||
540 | * @static |
||
541 | * @param Generator $value |
||
542 | * @return bool |
||
543 | */ |
||
544 | 1 | private static function getGeneratorReturn(\Generator $value) |
|
555 | |||
556 | /** |
||
557 | * Check if value is a valid cURL resource. |
||
558 | * |
||
559 | * @access private |
||
560 | * @static |
||
561 | * @param mixed $value |
||
562 | * @return bool |
||
563 | */ |
||
564 | 1 | private static function isCurl($value) |
|
568 | |||
569 | /** |
||
570 | * Check if value is a valid Generator. |
||
571 | * |
||
572 | * @access private |
||
573 | * @static |
||
574 | * @param mixed $value |
||
575 | * @return bool |
||
576 | */ |
||
577 | 2 | private static function isGenerator($value) |
|
581 | |||
582 | /** |
||
583 | * Check if value is a valid array or Traversable, not a Generator. |
||
584 | * |
||
585 | * @access private |
||
586 | * @static |
||
587 | * @param mixed $value |
||
588 | * @return bool |
||
589 | */ |
||
590 | 4 | private static function isArrayLike($value) |
|
595 | |||
596 | /** |
||
597 | * Flatten an array or a Traversable. |
||
598 | * |
||
599 | * @access private |
||
600 | * @static |
||
601 | * @param mixed $value |
||
602 | * @return array |
||
603 | */ |
||
604 | 2 | private static function flatten($value, array &$carry = array()) |
|
615 | |||
616 | } |
||
617 |