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<cURL>> |
||
| 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<string, mixed> $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 | * |
||
| 87 | * @access public |
||
| 88 | * @static |
||
| 89 | * @param mixed $value |
||
| 90 | * @param array<string, mixed> $options |
||
| 91 | * @see self::__construct() |
||
| 92 | */ |
||
| 93 | public static function wait($value, array $options = array()) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Parallel execution along with Co::async(). |
||
| 123 | * This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback. |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | * @static |
||
| 127 | * @param mixed $value |
||
| 128 | * @see self::__construct() |
||
| 129 | */ |
||
| 130 | public static function async($value) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Internal constructor. |
||
| 144 | * |
||
| 145 | * @access private |
||
| 146 | * @param array<string, mixed> $options |
||
| 147 | * @see self::initialize(), self::run() |
||
| 148 | */ |
||
| 149 | 2 | private function __construct(array $options) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Call curl_multi_add_handle or push into waiting queue. |
||
| 161 | * |
||
| 162 | * @access private |
||
| 163 | * @param resource<cURL> $curl |
||
|
1 ignored issue
–
show
|
|||
| 164 | */ |
||
| 165 | 1 | private function enqueue($curl) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Set or overwrite tree of return values. |
||
| 191 | * |
||
| 192 | * @access private |
||
| 193 | * @param mixed $value mixed |
||
| 194 | * @param string $parent_hash *Stack ID* |
||
| 195 | * @param array<string>? $keylist Queue of keys for its hierarchy. |
||
|
1 ignored issue
–
show
|
|||
| 196 | */ |
||
| 197 | private function setTree($value, $parent_hash, array $keylist = array()) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Unset tree of return values. |
||
| 211 | * |
||
| 212 | * @access private |
||
| 213 | * @param string $hash *Stack ID* or *cURL ID* |
||
| 214 | */ |
||
| 215 | private function unsetTree($hash) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set table of dependencies. |
||
| 229 | * |
||
| 230 | * @access private |
||
| 231 | * @param Generator|resource<cURL> $value |
||
|
1 ignored issue
–
show
|
|||
| 232 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
| 233 | * @param array? $keylist Queue of keys for its hierarchy. |
||
|
1 ignored issue
–
show
|
|||
| 234 | */ |
||
| 235 | private function setTable($value, $parent_hash, array $keylist = array()) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Unset table of dependencies. |
||
| 246 | * |
||
| 247 | * @access private |
||
| 248 | * @param string $hash *Stack ID* or *cURL ID* |
||
| 249 | */ |
||
| 250 | private function unsetTable($hash) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Run curl_multi_exec() loop. |
||
| 277 | * |
||
| 278 | * @access private |
||
| 279 | * @see self::updateCurl(), self::enqueue() |
||
| 280 | */ |
||
| 281 | private function run() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Unset table of dependencies. |
||
| 314 | * |
||
| 315 | * @access private |
||
| 316 | * @param mixed $value |
||
| 317 | * @param string $parent_hash *Stack ID* or *cURL ID* |
||
| 318 | * @param array? $keylist Queue of keys for its hierarchy. |
||
|
1 ignored issue
–
show
|
|||
| 319 | * @return bool Enqueued? |
||
| 320 | */ |
||
| 321 | private function initialize($value, $parent_hash, array $keylist = array()) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Update tree with cURL result. |
||
| 373 | * |
||
| 374 | * @access private |
||
| 375 | * @param resource<cURL> $value |
||
|
1 ignored issue
–
show
|
|||
| 376 | * @param int $errno |
||
| 377 | * @see self::updateGenerator() |
||
| 378 | */ |
||
| 379 | private function updateCurl($value, $errno) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Check current Generator can throw a CURLException. |
||
| 414 | * |
||
| 415 | * @access private |
||
| 416 | * @param Generator $value |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | private function canThrow(\Generator $value) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Update tree with updateCurl() result. |
||
| 439 | * |
||
| 440 | * @access private |
||
| 441 | * @param Generator $value |
||
| 442 | */ |
||
| 443 | private function updateGenerator(\Generator $value) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Validate options. |
||
| 476 | * |
||
| 477 | * @access private |
||
| 478 | * @static |
||
| 479 | * @param array<string, mixed> $options |
||
| 480 | * @return array<string, mixed> |
||
|
1 ignored issue
–
show
|
|||
| 481 | */ |
||
| 482 | 1 | private static function validateOptions(array $options) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Normalize value. |
||
| 512 | * |
||
| 513 | * @access private |
||
| 514 | * @static |
||
| 515 | * @param mixed $value |
||
| 516 | * @return miexed |
||
| 517 | */ |
||
| 518 | 1 | private static function normalize($value) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Check if a Generator is running. |
||
| 533 | * This method supports psuedo return with Co::RETURN_WITH. |
||
| 534 | * |
||
| 535 | * @access private |
||
| 536 | * @static |
||
| 537 | * @param Generator $value |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | 1 | private static function isGeneratorRunning(\Generator $value) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Get return value from a Generator. |
||
| 548 | * This method supports psuedo return with Co::RETURN_WITH. |
||
| 549 | * |
||
| 550 | * @access private |
||
| 551 | * @static |
||
| 552 | * @param Generator $value |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | 1 | private static function getGeneratorReturn(\Generator $value) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Check if value is a valid cURL resource. |
||
| 569 | * |
||
| 570 | * @access private |
||
| 571 | * @static |
||
| 572 | * @param mixed $value |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | 1 | private static function isCurl($value) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Check if value is a valid Generator. |
||
| 582 | * |
||
| 583 | * @access private |
||
| 584 | * @static |
||
| 585 | * @param mixed $value |
||
| 586 | * @return bool |
||
| 587 | */ |
||
| 588 | 1 | private static function isGenerator($value) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Check if value is a valid array or Traversable, not a Generator. |
||
| 595 | * |
||
| 596 | * @access private |
||
| 597 | * @static |
||
| 598 | * @param mixed $value |
||
| 599 | * @return bool |
||
| 600 | */ |
||
| 601 | 3 | private static function isArrayLike($value) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Flatten an array or a Traversable. |
||
| 609 | * |
||
| 610 | * @access private |
||
| 611 | * @static |
||
| 612 | * @param mixed $value |
||
| 613 | * @param array &$carry |
||
| 614 | * @return array<mixed> |
||
| 615 | */ |
||
| 616 | 1 | private static function flatten($value, array &$carry = array()) |
|
| 627 | |||
| 628 | } |
||
| 629 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.