Passed
Branch master (e86b58)
by Martin
02:18
created

BaseVariadicFunction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 16
c 1
b 0
f 1
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A feedParserWithNodes() 0 17 4
A getSql() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\Parser;
9
use Doctrine\ORM\Query\SqlWalker;
10
11
/**
12
 * @author Martin Georgiev <[email protected]>
13
 */
14
abstract class BaseVariadicFunction extends BaseFunction
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $commonNodeMapping = 'StringPrimary';
20
21
    public function feedParserWithNodes(Parser $parser): void
22
    {
23
        $lexer = $parser->getLexer();
24
25
        $this->nodes[] = $parser->{$this->commonNodeMapping}();
26
        if (!isset($lexer->lookahead['type'])) {
27
            throw new \RuntimeException('The parser\'s "lookahead" property is not populated with a type');
28
        }
29
30
        $aheadType = $lexer->lookahead['type'];
31
32
        while (Lexer::T_CLOSE_PARENTHESIS !== $aheadType) {
33
            if (Lexer::T_COMMA === $aheadType) {
34
                $parser->match(Lexer::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->dispatch($sqlWalker);
46
        }
47
48
        return \sprintf($this->functionPrototype, \implode(', ', $dispatched));
49
    }
50
}
51