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

StringAgg::parse()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 24
rs 7.6666
cc 10
nc 4
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AST\OrderByClause;
9
use Doctrine\ORM\Query\Lexer;
10
use Doctrine\ORM\Query\Parser;
11
use Doctrine\ORM\Query\SqlWalker;
12
use Doctrine\ORM\Query\TokenType;
13
use MartinGeorgiev\Utils\DoctrineOrm;
14
15
/**
16
 * Implementation of PostgreSql STRING_AGG().
17
 *
18
 * @see https://www.postgresql.org/docs/9.5/functions-aggregate.html
19
 * @since 1.4
20
 *
21
 * @author Martin Georgiev <[email protected]>
22
 */
23
class StringAgg extends BaseFunction
24
{
25
    private bool $isDistinct = false;
26
27
    private Node $expression;
28
29
    private Node $delimiter;
30
31
    private OrderByClause $orderByClause;
32
33
    protected function customiseFunction(): void
34
    {
35
        $this->setFunctionPrototype('string_agg(%s%s, %s%s)');
36
    }
37
38
    public function parse(Parser $parser): void
39
    {
40
        $shouldUseLexer = DoctrineOrm::isPre219();
41
42
        $this->customiseFunction();
43
44
        $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_IDENTIFIER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45
        $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
47
        $lexer = $parser->getLexer();
48
        if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) {
0 ignored issues
show
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

48
        if ($lexer->isNextToken(/** @scrutinizer ignore-type */ $shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) {
Loading history...
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...
49
            $parser->match($shouldUseLexer ? Lexer::T_DISTINCT : TokenType::T_DISTINCT);
50
            $this->isDistinct = true;
51
        }
52
53
        $this->expression = $parser->StringPrimary();
54
        $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...
55
        $this->delimiter = $parser->StringPrimary();
56
57
        if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_ORDER : TokenType::T_ORDER)) {
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_ORDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
            $this->orderByClause = $parser->OrderByClause();
59
        }
60
61
        $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS);
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...
62
    }
63
64
    public function getSql(SqlWalker $sqlWalker): string
65
    {
66
        $dispatched = [
67
            $this->isDistinct ? 'distinct ' : '',
68
            $this->expression->dispatch($sqlWalker),
69
            $this->delimiter->dispatch($sqlWalker),
70
            isset($this->orderByClause) ? $this->orderByClause->dispatch($sqlWalker) : '',
71
        ];
72
73
        return \vsprintf($this->functionPrototype, $dispatched);
74
    }
75
}
76