1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Co\Internal; |
4
|
|
|
use mpyw\RuntimePromise\Deferred; |
5
|
|
|
use mpyw\Co\AllFailedException; |
6
|
|
|
use mpyw\Co\CoInterface; |
7
|
|
|
|
8
|
|
|
class ControlUtils |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Executed by Co::any() or Co::race(). |
12
|
|
|
* @param mixed $value |
13
|
|
|
* @param callable $filter self::reverse or self::fail. |
14
|
|
|
* @param string $message Used for failure. |
15
|
|
|
* @return \Generator |
16
|
|
|
*/ |
17
|
7 |
|
public static function anyOrRace($value, callable $filter, $message) |
18
|
7 |
|
{ |
19
|
7 |
|
$value = YieldableUtils::normalize($value); |
20
|
7 |
|
$yieldables = YieldableUtils::getYieldables($value); |
21
|
7 |
|
$wrapper = self::getWrapperGenerator($yieldables, $filter); |
22
|
|
|
try { |
23
|
7 |
|
$results = (yield $wrapper); |
24
|
4 |
|
} catch (ControlException $e) { |
|
|
|
|
25
|
3 |
|
yield CoInterface::RETURN_WITH => $e->getValue(); |
26
|
|
|
} |
27
|
3 |
|
$apply = YieldableUtils::getApplier($value, $yieldables); |
28
|
3 |
|
throw new AllFailedException($message, 0, $apply($results)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Wrap yieldables with specified filter function. |
33
|
|
|
* @param array $yieldables |
34
|
|
|
* @param callable $filter self::reverse or self::fail. |
35
|
|
|
* @return \Generator |
36
|
|
|
*/ |
37
|
7 |
|
public static function getWrapperGenerator(array $yieldables, callable $filter) |
38
|
7 |
|
{ |
39
|
7 |
|
$gens = []; |
40
|
7 |
|
foreach ($yieldables as $yieldable) { |
41
|
5 |
|
$gens[(string)$yieldable['value']] = $filter($yieldable['value']); |
42
|
|
|
} |
43
|
7 |
|
yield CoInterface::RETURN_WITH => (yield $gens); |
44
|
|
|
// @codeCoverageIgnoreStart |
45
|
|
|
} |
46
|
|
|
// @codeCoverageIgnoreEnd |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Handle success as ControlException, failure as resolved. |
50
|
|
|
* @param mixed $yieldable |
51
|
|
|
* @return \Generator |
52
|
|
|
*/ |
53
|
3 |
|
public static function reverse($yieldable) |
54
|
3 |
|
{ |
55
|
|
|
try { |
56
|
3 |
|
$result = (yield $yieldable); |
57
|
2 |
|
} catch (\RuntimeException $e) { |
|
|
|
|
58
|
2 |
|
yield CoInterface::RETURN_WITH => $e; |
59
|
|
|
} |
60
|
2 |
|
throw new ControlException('', 0, $result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handle success as ControlException. |
65
|
|
|
* @param mixed $yieldable |
66
|
|
|
* @return \Generator |
67
|
|
|
*/ |
68
|
2 |
|
public static function fail($yieldable) |
69
|
2 |
|
{ |
70
|
2 |
|
throw new ControlException('', 0, (yield $yieldable)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.