Passed
Push — array_agg-and-order_by ( 4a69e9 )
by Martin
03:10
created

StringAgg::parseFunction()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 2
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
 * Implementation of PostgreSql STRING_AGG().
16
 *
17
 * @see https://www.postgresql.org/docs/9.5/functions-aggregate.html
18
 * @since 1.4
19
 *
20
 * @author Martin Georgiev <[email protected]>
21
 */
22
class StringAgg extends BaseOrderableFunction
23
{
24
    private bool $isDistinct = false;
25
    private Node $delimiter;
26
27
    protected function customiseFunction(): void
28
    {
29
        $this->setFunctionPrototype('string_agg(%s%s, %s%s)');
30
    }
31
32
    protected function parseFunction(Parser $parser): void
33
    {
34
        $shouldUseLexer = DoctrineOrm::isPre219();
35
        $lexer = $parser->getLexer();
36
37
        if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) {
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_DISTINCT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like $shouldUseLexer ? Doctri...y\TokenType::T_DISTINCT can also be of type Doctrine\ORM\Query\TokenType; however, parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken() does only seem to accept UnitEnum|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        if ($lexer->isNextToken(/** @scrutinizer ignore-type */ $shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) {
Loading history...
38
            $parser->match($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT);
39
            $this->isDistinct = true;
40
        }
41
42
        $this->expression = $parser->StringPrimary();
43
44
        $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
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...
45
46
        $this->delimiter = $parser->StringPrimary();
47
    }
48
49
    public function getSql(SqlWalker $sqlWalker): string
50
    {
51
        $dispatched = [
52
            $this->isDistinct ? 'distinct ' : '',
53
            $this->expression->dispatch($sqlWalker),
54
            $this->delimiter->dispatch($sqlWalker),
55
            $this->getOptionalOrderByClause($sqlWalker),
56
        ];
57
58
        return \vsprintf($this->functionPrototype, $dispatched);
59
    }
60
}
61