| 1 | <?php |
||
| 13 | class Co implements CoInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Instance of myself. |
||
| 17 | * @var Co |
||
| 18 | */ |
||
| 19 | private static $self; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Options. |
||
| 23 | * @var CoOption |
||
| 24 | */ |
||
| 25 | private $options; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * cURL request pool object. |
||
| 29 | * @var CURLPool |
||
| 30 | */ |
||
| 31 | private $pool; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Overwrite CoOption default. |
||
| 35 | * @param array $options |
||
| 36 | */ |
||
| 37 | 1 | public static function setDefaultOptions(array $options) |
|
| 38 | 1 | { |
|
| 39 | 1 | CoOption::setDefault($options); |
|
| 40 | 1 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Get CoOption default as array. |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | 1 | public static function getDefaultOptions() |
|
| 47 | 1 | { |
|
| 48 | 1 | return CoOption::getDefault(); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Wait until value is recursively resolved to return it. |
||
| 53 | * This function call must be atomic. |
||
| 54 | * @param mixed $value |
||
| 55 | * @param array $options |
||
| 56 | * @return mixed |
||
| 57 | * @codeCoverageIgnore |
||
| 58 | */ |
||
| 59 | public static function wait($value, array $options = []) |
||
| 60 | { |
||
| 61 | // Coverage analyzer does not support... |
||
| 62 | // try { return; } finally { } |
||
|
1 ignored issue
–
show
|
|||
| 63 | try { |
||
| 64 | if (self::$self) { |
||
| 65 | throw new \BadMethodCallException('Co::wait() is already running. Use Co::async() instead.'); |
||
| 66 | } |
||
| 67 | self::$self = new self; |
||
| 68 | self::$self->options = new CoOption($options); |
||
| 69 | self::$self->pool = new CURLPool(self::$self->options); |
||
| 70 | return self::$self->start($value); |
||
| 71 | } finally { |
||
| 72 | self::$self = null; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Value is recursively resolved, but we never wait it. |
||
| 78 | * This function must be called along with Co::wait(). |
||
| 79 | * @param mixed $value |
||
| 80 | * @param array $options |
||
| 81 | */ |
||
| 82 | 3 | public static function async($value, array $options = []) |
|
| 83 | 3 | { |
|
| 84 | 3 | if (!self::$self) { |
|
| 85 | 1 | throw new \BadMethodCallException( |
|
| 86 | 'Co::async() must be called along with Co::wait(). ' . |
||
| 87 | 1 | 'This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback.' |
|
| 88 | ); |
||
| 89 | } |
||
| 90 | 2 | self::$self->start($value, false); |
|
| 91 | 2 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * External instantiation is forbidden. |
||
| 95 | */ |
||
| 96 | private function __construct() {} |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Start resovling. |
||
| 100 | * @param mixed $value |
||
| 101 | * @param bool $wait |
||
| 102 | * @param mixed If $wait, return resolved value. |
||
| 103 | */ |
||
| 104 | 10 | private function start($value, $wait = true) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Handle resolving generators. |
||
| 135 | * @param GeneratorContainer $gc |
||
| 136 | * @param Deferred $deferred |
||
| 137 | */ |
||
| 138 | 10 | private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Return function that apply changes in yieldables. |
||
| 203 | * @param mixed $yielded |
||
| 204 | * @param array $yieldables |
||
| 205 | * @param callable $next |
||
| 206 | */ |
||
| 207 | 7 | private static function getApplier($yielded, $yieldables, callable $next) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Promise all changes in yieldables are prepared. |
||
| 223 | * @param array $yieldables |
||
| 224 | * @param bool $throw_acceptable |
||
| 225 | * @return PromiseInterface |
||
| 226 | */ |
||
| 227 | 8 | private function promiseAll($yieldables, $throw_acceptable) |
|
| 256 | } |
||
| 257 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.