Completed
Push — master ( 0770f8...218e56 )
by Ryosuke
21:28
created

Co::wait()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 2
eloc 8
nc 4
nop 2
crap 2.0625
1
<?php
2
3
namespace mpyw\Co;
4
use mpyw\Co\Internal\Utils;
5
use mpyw\Co\Internal\CoOption;
6
use mpyw\Co\Internal\GeneratorContainer;
7
use mpyw\Co\Internal\CURLPool;
8
9
use React\Promise\Deferred;
10
use React\Promise\PromiseInterface;
11
use function React\Promise\all;
12
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
    public static function setDefaultOptions(array $options)
38
    {
39
        CoOption::setDefault($options);
40
    }
41
42
    /**
43
     * Get CoOption default as array.
44
     * @return array
45
     */
46
    public static function getDefaultOptions()
47
    {
48
        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
     */
58 1
    public static function wait($value, array $options = [])
59 1
    {
60
        try {
61 1
            if (self::$self) {
62
                throw new \BadMethodCallException('Co::wait() is already running. Use Co::async() instead.');
63
            }
64 1
            self::$self = new self;
65 1
            return self::$self->start($value, new CoOption($options));
66
        } finally {
67 1
            self::$self = null;
68
        }
69
    }
70
71
    /**
72
     * Value is recursively resolved, but we never wait it.
73
     * This function must be called along with Co::wait().
74
     * @param  mixed $value
75
     * @param  array $options
76
     */
77
    public static function async($value, array $options = [])
78
    {
79
        if (!self::$self) {
80
            throw new \BadMethodCallException(
81
                'Co::async() must be called along with Co::wait(). ' .
82
                'This method is mainly expected to be used in CURLOPT_WRITEFUNCTION callback.'
83
            );
84
        }
85
        self::$self->start($value, self::$self->options->reconfigure($options), false);
86
    }
87
88
    /**
89
     * External instantiation is forbidden.
90
     */
91
    private function __construct() {}
92
93
    /**
94
     * Start resovling.
95
     * @param  mixed    $value
96
     * @param  CoOption $options
97
     * @param  bool     $wait
98
     * @param  mixed    If $wait, return resolved value.
99
     */
100 1
    private function start($value, CoOption $options, $wait = true)
101 1
    {
102 1
        $this->options = $options;
103 1
        $this->pool = new CURLPool($options);
104
        // If $wait, final result is stored into referenced $return
105 1
        if ($wait) {
106 1
            $deferred = new Deferred;
107
            $deferred->promise()->done(function ($r) use (&$return) {
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface React\Promise\PromiseInterface as the method done() does only exist in the following implementations of said interface: React\Promise\FulfilledPromise, React\Promise\LazyPromise, React\Promise\Promise, React\Promise\RejectedPromise.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
108 1
                $return = $r;
109 1
            });
110
        }
111
        // For convenience, all values are wrapped into generator
112
        $genfunc = function () use ($value) {
113 1
            yield CoInterface::RETURN_WITH => (yield $value);
114 1
        };
115 1
        $con = Utils::normalize($genfunc, $options);
116
        // We have to provide deferred object only if $wait
117 1
        $this->processGeneratorContainer($con, $wait ? $deferred : null);
1 ignored issue
show
Bug introduced by
The variable $deferred does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
118
        // We have to wait $return only if $wait
119 1
        if ($wait) {
120 1
            $this->pool->wait();
121 1
            return $return;
1 ignored issue
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
122
        }
123
    }
124
125
    /**
126
     * Handle resolving generators.
127
     * @param  GeneratorContainer $gc
128
     * @param  Deferred           $deferred
129
     */
130 1
    private function processGeneratorContainer(GeneratorContainer $gc, Deferred $deferred = null)
131 1
    {
132
        // If generator has no more yields...
133 1
        if (!$gc->valid()) {
134
            // If exception has been thrown in generator, we have to propagate it as rejected value
135 1
            if ($gc->thrown()) {
136
                $deferred->reject($gc->getReturnOrThrown());
0 ignored issues
show
Bug introduced by
It seems like $deferred is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
137
                return;
138
            }
139
            // Now we normalize returned value
140
            try {
141 1
                $returned = Utils::normalize($gc->getReturnOrThrown(), $gc->getOptions());
142 1
                $yieldables = Utils::getYieldables($returned);
143
                // If normalized value contains yieldables, we have to chain resolver
144 1
                if ($yieldables) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $yieldables of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
                    $this->promiseAll($yieldables, true)->then(
0 ignored issues
show
Documentation introduced by
$yieldables is of type array, but the function expects a object<mpyw\Co\arrya>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
                        self::getApplier($returned, $yieldables, [$deferred, 'resolve']),
147
                        [$deferred, 'reject']
148
                    );
149
                    return;
150
                }
151
                // Propagate normalized returned value
152 1
                $deferred && $deferred->resolve($returned);
153
            } catch (\RuntimeException $e) {
154
                // Propagate exception thrown in normalization
155
                $deferred && $deferred->reject($e);
156
            }
157 1
            return;
158
        }
159
160
        // Now we normalize yielded value
161
        try {
162 1
            $yielded = Utils::normalize($gc->current(), $gc->getOptions(), $gc->key());
163
        } catch (\RuntimeException $e) {
164
            // If exception thrown in normalization...
165
            //   - If generator accepts exception, we throw it into generator
166
            //   - If generator does not accept exception, we assume it as non-exception value
167
            $gc->throwAcceptable() ? $gc->throw_($e) : $gc->send($e);
168
            // Continue
169
            $this->processGeneratorContainer($gc, $deferred);
170
            return;
171
        }
172
173
        // Search yieldables from yielded value
174 1
        $yieldables = Utils::getYieldables($yielded);
175 1
        if (!$yieldables) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $yieldables of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
176
            // If there are no yieldables, send yielded value back into generator
177 1
            $gc->send($yielded);
178
            // Continue
179 1
            $this->processGeneratorContainer($gc, $deferred);
180 1
            return;
181
        }
182
183
        // Chain resolver
184 1
        $this->promiseAll($yieldables, $gc->throwAcceptable())->then(
1 ignored issue
show
Documentation introduced by
$yieldables is of type array, but the function expects a object<mpyw\Co\arrya>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface React\Promise\PromiseInterface as the method always() does only exist in the following implementations of said interface: React\Promise\FulfilledPromise, React\Promise\LazyPromise, React\Promise\Promise, React\Promise\RejectedPromise.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
185 1
            self::getApplier($yielded, $yieldables, [$gc, 'send']),
186 1
            [$gc, 'throw_']
187
        )->always(function () use ($gc, $deferred) {
188
            // Continue
189 1
            $this->processGeneratorContainer($gc, $deferred);
190 1
        });
191 1
    }
192
193
    /**
194
     * Return function that apply changes in yieldables.
195
     * @param  mixed    $yielded
196
     * @param  array    $yieldables
197
     * @param  callable $next
198
     */
199 1
    private static function getApplier($yielded, $yieldables, callable $next)
200 1
    {
201
        return function (array $results) use ($yielded, $yieldables, $next) {
202 1
            foreach ($results as $hash => $resolved) {
203 1
                $current = &$yielded;
204 1
                foreach ($yieldables[$hash]['keylist'] as $key) {
205 1
                    $current = &$current[$key];
206
                }
207 1
                $current = $resolved;
208
            }
209 1
            $next($yielded);
210 1
        };
211
    }
212
213
    /**
214
     * Promise all changes in yieldables are prepared.
215
     * @param  arrya $yieldables
216
     * @param  bool  $throw_acceptable
217
     * @return PromiseInterface
218
     */
219 1
    private function promiseAll($yieldables, $throw_acceptable)
220 1
    {
221 1
        $promises = [];
222 1
        foreach ($yieldables as $yieldable) {
223 1
            $dfd = new Deferred;
224 1
            $promises[(string)$yieldable['value']] = $dfd->promise();
225
            // If caller cannot accept exception,
226
            // we handle rejected value as resolved.
227 1
            if (!$throw_acceptable) {
228
                $original_dfd = $dfd;
229
                $dfd = new Deferred;
230
                $absorber = function ($any) use ($original_dfd) {
231
                    $original_dfd->resolve($any);
232
                };
233
                $dfd->promise()->then($absorber, $absorber);
234
            }
235
            // Add or enqueue cURL handles
236 1
            if (Utils::isCurl($yieldable['value'])) {
237
                $this->pool->addOrEnqueue($yieldable['value'], $dfd);
238
                continue;
239
            }
240
            // Process generators
241 1
            if (Utils::isGeneratorContainer($yieldable['value'])) {
242 1
                $this->processGeneratorContainer($yieldable['value'], $dfd);
243 1
                continue;
244
            }
245
        }
246 1
        return all($promises);
247
    }
248
}
249