Test Failed
Push — master ( e081e8...a6938b )
by Kirill
02:31
created

Deferred::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend\IR;
11
12
use Railt\Parser\Ast\RuleInterface;
13
14
/**
15
 * Class Deferred
16
 */
17
class Deferred
18
{
19
    /**
20
     * @var RuleInterface
21
     */
22
    private $rule;
23
24
    /**
25
     * @var callable
26
     */
27
    private $callback;
28
29
    /**
30
     * Deferred constructor.
31
     * @param RuleInterface $rule
32
     * @param callable $callback
33
     */
34
    public function __construct(RuleInterface $rule, callable $callback)
35
    {
36
        $this->rule = $rule;
37
        $this->callback = $callback;
38
    }
39
40
    /**
41
     * @param mixed ...$args
42
     * @return \Generator
43
     */
44
    public function __invoke(...$args)
45
    {
46
        yield from $this->resolve(...$args);
47
    }
48
49
    /**
50
     * @param mixed ...$args
51
     * @return \Generator
52
     */
53
    public function resolve(...$args): \Generator
54
    {
55
        $result = ($this->callback)(...$args);
56
57
        if ($result instanceof \Generator) {
58
            yield from $result;
59
        } else {
60
            yield $this->rule => $result;
61
        }
62
    }
63
}
64