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()) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Parallel execution along with Co::async(). |
||
| 122 | * This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback. |
||
| 123 | * |
||
| 124 | * @access public |
||
| 125 | * @static |
||
| 126 | * @param mixed $value |
||
| 127 | * @see self::__construct() |
||
| 128 | */ |
||
| 129 | public static function async($value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Internal constructor. |
||
| 143 | * |
||
| 144 | * @access private |
||
| 145 | * @param array $options |
||
| 146 | * @see self::initialize(), self::run() |
||
| 147 | */ |
||
| 148 | 4 | private function __construct(array $options) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Call curl_multi_add_handle or push into waiting queue. |
||
| 160 | * |
||
| 161 | * @access private |
||
| 162 | * @param resource $curl |
||
| 163 | */ |
||
| 164 | 1 | private function enqueue($curl) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Set or overwrite tree of return values. |
||
| 190 | * |
||
| 191 | * @access private |
||
| 192 | * @param mixed $value mixed |
||
| 193 | * @param string $parent_hash *Stack ID* |
||
| 194 | * @param array $keylist Queue of keys for its hierarchy. |
||
| 195 | */ |
||
| 196 | 1 | private function setTree($value, $parent_hash, array $keylist = array()) |
|
| 197 | 1 | { |
|
| 198 | 1 | $current = &$this->tree[$parent_hash]; |
|
| 199 | 1 | while (null !== $key = array_shift($keylist)) { |
|
| 200 | 1 | if (!is_array($current)) { |
|
| 201 | 1 | $current = array(); |
|
| 202 | } |
||
| 203 | 1 | $current = &$current[$key]; |
|
| 204 | } |
||
| 205 | 1 | $current = $value; |
|
| 206 | 1 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Unset tree of return values. |
||
| 210 | * |
||
| 211 | * @access private |
||
| 212 | * @param string $hash *Stack ID* or *cURL ID* |
||
| 213 | */ |
||
| 214 | 1 | private function unsetTree($hash) |
|
| 215 | 1 | { |
|
| 216 | 1 | if (isset($this->tree[$hash])) { |
|
| 217 | 1 | foreach (self::flatten($this->tree[$hash]) as $v) { |
|
| 218 | 1 | if (self::isGenerator($v)) { |
|
| 219 | 1 | $this->unsetTree(spl_object_hash($v)); |
|
| 220 | } |
||
| 221 | } |
||
| 222 | 1 | unset($this->tree[$hash]); |
|
| 223 | } |
||
| 224 | 1 | } |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Set table of dependencies. |
||
| 228 | * |
||
| 229 | * @access private |
||
| 230 | * @param Generator|resource $value |
||
| 231 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
| 232 | * @param array $keylist Queue of keys for its hierarchy. |
||
| 233 | */ |
||
| 234 | 1 | private function setTable($value, $parent_hash, array $keylist = array()) |
|
| 235 | 1 | { |
|
| 236 | 1 | $hash = is_object($value) ? spl_object_hash($value) : (string)$value; |
|
| 237 | 1 | $this->values[$hash] = $value; |
|
| 238 | 1 | $this->value_to_parent[$hash] = $parent_hash; |
|
| 239 | 1 | $this->value_to_children[$parent_hash][$hash] = true; |
|
| 240 | 1 | $this->value_to_keylist[$hash] = $keylist; |
|
| 241 | 1 | } |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Unset table of dependencies. |
||
| 245 | * |
||
| 246 | * @access private |
||
| 247 | * @param string $hash *Stack ID* or *cURL ID* |
||
| 248 | */ |
||
| 249 | private function unsetTable($hash) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Run curl_multi_exec() loop. |
||
| 276 | * |
||
| 277 | * @access private |
||
| 278 | * @see self::updateCurl(), self::enqueue() |
||
| 279 | */ |
||
| 280 | private function run() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Unset table of dependencies. |
||
| 313 | * |
||
| 314 | * @access private |
||
| 315 | * @param mixed $value |
||
| 316 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
| 317 | * @param array $keylist Queue of keys for its hierarchy. |
||
| 318 | * @return bool Enqueued? |
||
| 319 | */ |
||
| 320 | private function initialize($value, $parent_hash, array $keylist = array()) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Update tree with cURL result. |
||
| 372 | * |
||
| 373 | * @access private |
||
| 374 | * @param resource $value |
||
| 375 | * @param int $errno |
||
| 376 | * @see self::updateGenerator() |
||
| 377 | */ |
||
| 378 | private function updateCurl($value, $errno) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Check current Generator can throw a CURLException. |
||
| 413 | * |
||
| 414 | * @access private |
||
| 415 | * @param Generator $value |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | private function canThrow(\Generator $value) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Update tree with updateCurl() result. |
||
| 438 | * |
||
| 439 | * @access private |
||
| 440 | * @param Generator $value |
||
| 441 | */ |
||
| 442 | private function updateGenerator(\Generator $value) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Validate options. |
||
| 475 | * |
||
| 476 | * @access private |
||
| 477 | * @static |
||
| 478 | * @param array $options |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | 1 | private static function validateOptions(array $options) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Normalize value. |
||
| 511 | * |
||
| 512 | * @access private |
||
| 513 | * @static |
||
| 514 | * @param mixed $value |
||
| 515 | * @return miexed |
||
| 516 | */ |
||
| 517 | 1 | private static function normalize($value) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Check if a Generator is running. |
||
| 532 | * This method supports psuedo return with Co::RETURN_WITH. |
||
| 533 | * |
||
| 534 | * @access private |
||
| 535 | * @static |
||
| 536 | * @param Generator $value |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | 1 | private static function isGeneratorRunning(\Generator $value) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Get return value from a Generator. |
||
| 547 | * This method supports psuedo return with Co::RETURN_WITH. |
||
| 548 | * |
||
| 549 | * @access private |
||
| 550 | * @static |
||
| 551 | * @param Generator $value |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | 1 | private static function getGeneratorReturn(\Generator $value) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Check if value is a valid cURL resource. |
||
| 568 | * |
||
| 569 | * @access private |
||
| 570 | * @static |
||
| 571 | * @param mixed $value |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | 1 | private static function isCurl($value) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Check if value is a valid Generator. |
||
| 581 | * |
||
| 582 | * @access private |
||
| 583 | * @static |
||
| 584 | * @param mixed $value |
||
| 585 | * @return bool |
||
| 586 | */ |
||
| 587 | 2 | private static function isGenerator($value) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Check if value is a valid array or Traversable, not a Generator. |
||
| 594 | * |
||
| 595 | * @access private |
||
| 596 | * @static |
||
| 597 | * @param mixed $value |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | 4 | private static function isArrayLike($value) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Flatten an array or a Traversable. |
||
| 608 | * |
||
| 609 | * @access private |
||
| 610 | * @static |
||
| 611 | * @param mixed $value |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | 2 | private static function flatten($value, array &$carry = array()) |
|
| 625 | |||
| 626 | } |
||
| 627 |