Test Failed
Push — master ( 690100...f55788 )
by Kirill
09:19
created

Builder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Frontend\Builder\Factory;
15
use Railt\SDL\IR\Definition;
16
17
/**
18
 * Class Builder
19
 */
20
class Builder
21
{
22
    /**
23
     * @var Factory
24
     */
25
    private $factory;
26
27
    /**
28
     * Builder constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->factory = new Factory();
33
    }
34
35
    /**
36
     * @param Readable $readable
37
     * @param RuleInterface $ast
38
     * @return \Generator|RuleInterface[]|Definition[]
39
     * @throws \LogicException
40
     */
41
    public function build(Readable $readable, RuleInterface $ast)
42
    {
43
        $iterator = $this->forEach($readable, $ast);
44
45
        while ($iterator->valid()) {
46
            [$key, $value] = [$iterator->key(), $iterator->current()];
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
48
            if ($value instanceof RuleInterface) {
49
                yield from $children = $this->build($readable, $value);
50
                $iterator->send($children->getReturn());
51
                continue;
52
            }
53
54
            $iterator->send(yield $key => $value);
55
        }
56
57
        return $iterator->getReturn();
58
    }
59
60
    /**
61
     * @param Readable $readable
62
     * @param RuleInterface $ast
63
     * @return mixed|null
64
     * @throws \LogicException
65
     */
66
    public function reduce(Readable $readable, RuleInterface $ast)
67
    {
68
        $iterator = $this->build($readable, $ast);
69
70
        while ($iterator->valid()) {
71
            $iterator->send($iterator->current());
72
        }
73
74
        return $iterator->getReturn();
75
    }
76
77
    /**
78
     * @param Readable $readable
79
     * @param RuleInterface $ast
80
     * @return \Generator
81
     * @throws \LogicException
82
     */
83
    private function forEach(Readable $readable, RuleInterface $ast): \Generator
84
    {
85
        $builder = $this->factory->resolve($readable, $ast);
86
        $result = $builder->build($readable, $ast);
87
88
        if (\is_iterable($result)) {
89
            yield from $result;
90
        }
91
92
        return $result instanceof \Generator ? $result->getReturn() : $result;
93
    }
94
}
95