Completed
Push — master ( ebcbe7...06aa7a )
by Ryosuke
04:11
created

ControlUtils::fail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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) {
1 ignored issue
show
Unused Code introduced by
catch (\mpyw\Co\Internal...H => $e->getValue()); } does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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) {
1 ignored issue
show
Unused Code introduced by
catch (\RuntimeException...::RETURN_WITH => $e); } does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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