Passed
Push — renovate/major-composer-qa-too... ( 788b30...dda69d )
by
unknown
04:22 queued 01:40
created

BaseVariadicFunction::feedParserWithNodes()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 18
rs 8.8333
cc 7
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\AST\Node;
8
use Doctrine\ORM\Query\Lexer;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\SqlWalker;
11
use Doctrine\ORM\Query\TokenType;
12
use MartinGeorgiev\Utils\DoctrineOrm;
13
14
/**
15
 * @author Martin Georgiev <[email protected]>
16
 */
17
abstract class BaseVariadicFunction extends BaseFunction
18
{
19
    protected string $commonNodeMapping = 'StringPrimary';
20
21
    public function feedParserWithNodes(Parser $parser): void
22
    {
23
        $lexer = $parser->getLexer();
24
25
        $this->nodes[] = $parser->{$this->commonNodeMapping}();
26
        if ($lexer->lookahead?->type === null) {
27
            throw new \RuntimeException('The parser\'s "lookahead" property is not populated with a type');
28
        }
29
30
        $aheadType = $lexer->lookahead->type;
31
        $shouldUseLexer = DoctrineOrm::isPre219();
32
33
        while (($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS) !== $aheadType) {
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
            if (($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA) === $aheadType) {
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_COMMA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
                $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
36
                $this->nodes[] = $parser->{$this->commonNodeMapping}();
37
            }
38
            $aheadType = $lexer->lookahead->type;
39
        }
40
    }
41
42
    public function getSql(SqlWalker $sqlWalker): string
43
    {
44
        $dispatched = [];
45
        foreach ($this->nodes as $node) {
46
            $dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null';
47
        }
48
49
        return \sprintf($this->functionPrototype, \implode(', ', $dispatched));
50
    }
51
}
52