Passed
Pull Request — main (#184)
by Gawain
03:05 queued 01:01
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
13
/**
14
 * @author Martin Georgiev <[email protected]>
15
 */
16
abstract class BaseVariadicFunction extends BaseFunction
17
{
18
    protected string $commonNodeMapping = 'StringPrimary';
19
20
    public function feedParserWithNodes(Parser $parser): void
21
    {
22
        $lexer = $parser->getLexer();
23
24
        $this->nodes[] = $parser->{$this->commonNodeMapping}();
25
        if ($lexer->lookahead?->type === null) {
26
            throw new \RuntimeException('The parser\'s "lookahead" property is not populated with a type');
27
        }
28
29
        $aheadType = $lexer->lookahead->type;
30
        $ormV2 = !\class_exists(TokenType::class);
31
32
        while (($ormV2 ? 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...
33
            if (($ormV2 ? 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...
34
                $parser->match($ormV2 ? Lexer::T_COMMA : TokenType::T_COMMA);
35
                $this->nodes[] = $parser->{$this->commonNodeMapping}();
36
            }
37
            $aheadType = $lexer->lookahead->type;
38
        }
39
    }
40
41
    public function getSql(SqlWalker $sqlWalker): string
42
    {
43
        $dispatched = [];
44
        foreach ($this->nodes as $node) {
45
            $dispatched[] = $node instanceof Node ? $node->dispatch($sqlWalker) : 'null';
46
        }
47
48
        return \sprintf($this->functionPrototype, \implode(', ', $dispatched));
49
    }
50
}
51