Passed
Pull Request — main (#184)
by Gawain
03:05 queued 01:01
created

StringAgg::parse()   B

Complexity

Conditions 10
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
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
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 BaseFunction
23
{
24
    private bool $isDistinct = false;
25
26
    private Node $expression;
27
28
    private Node $delimiter;
29
30
    private OrderByClause $orderByClause;
31
32
    protected function customiseFunction(): void
33
    {
34
        $this->setFunctionPrototype('string_agg(%s%s, %s%s)');
35
    }
36
37
    public function parse(Parser $parser): void
38
    {
39
        $ormV2 = !\class_exists(TokenType::class);
40
41
        $this->customiseFunction();
42
43
        $parser->match($ormV2 ? 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...
44
        $parser->match($ormV2 ? 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...
45
46
        $lexer = $parser->getLexer();
47
        if ($lexer->isNextToken($ormV2 ? 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 $ormV2 ? Doctrine\ORM\Qu...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

47
        if ($lexer->isNextToken(/** @scrutinizer ignore-type */ $ormV2 ? Lexer::T_DISTINCT : TokenType::T_DISTINCT)) {
Loading history...
48
            $parser->match($ormV2 ? Lexer::T_DISTINCT : TokenType::T_DISTINCT);
49
            $this->isDistinct = true;
50
        }
51
52
        $this->expression = $parser->StringPrimary();
53
        $parser->match($ormV2 ? 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...
54
        $this->delimiter = $parser->StringPrimary();
55
56
        if ($lexer->isNextToken($ormV2 ? 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...
57
            $this->orderByClause = $parser->OrderByClause();
58
        }
59
60
        $parser->match($ormV2 ? 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...
61
    }
62
63
    public function getSql(SqlWalker $sqlWalker): string
64
    {
65
        $dispatched = [
66
            $this->isDistinct ? 'distinct ' : '',
67
            $this->expression->dispatch($sqlWalker),
68
            $this->delimiter->dispatch($sqlWalker),
69
            isset($this->orderByClause) ? $this->orderByClause->dispatch($sqlWalker) : '',
70
        ];
71
72
        return \vsprintf($this->functionPrototype, $dispatched);
73
    }
74
}
75