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

Cast::parse()   C

Complexity

Conditions 17
Paths 5

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 38
rs 5.2166
cc 17
nc 5
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\Common\Lexer\Token;
8
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
9
use Doctrine\ORM\Query\AST\Node;
10
use Doctrine\ORM\Query\Lexer;
11
use Doctrine\ORM\Query\Parser;
12
use Doctrine\ORM\Query\SqlWalker;
13
use Doctrine\ORM\Query\TokenType;
14
use MartinGeorgiev\Utils\DoctrineOrm;
15
16
/**
17
 * Implementation of PostgreSql CAST().
18
 *
19
 * @see https://www.postgresql.org/docs/current/sql-createcast.html
20
 * @see https://github.com/beberlei/DoctrineExtensions/blob/f3536d881637f6ddc7ca1d6595d18c15e06eb1d9/src/Query/Mysql/Cast.php
21
 * @since 2.0
22
 *
23
 * @author Mathieu Piot <https://github.com/mpiot>
24
 */
25
class Cast extends FunctionNode
26
{
27
    public Node $sourceType;
28
29
    public string $targetType;
30
31
    public function parse(Parser $parser): void
32
    {
33
        $shouldUseLexer = DoctrineOrm::isPre219();
34
35
        $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...
36
        $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...
37
        $this->sourceType = $parser->SimpleArithmeticExpression();
38
        $parser->match($shouldUseLexer ? Lexer::T_AS : TokenType::T_AS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_AS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER);
40
41
        $lexer = $parser->getLexer();
42
        $token = $lexer->token;
43
        if (!$token instanceof Token) {
44
            return;
45
        }
46
        if (!\is_string($token->value)) {
47
            return;
48
        }
49
50
        $type = $token->value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $token->value of type string is incompatible with the declared type Doctrine\Common\Lexer\V of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        if ($lexer->isNextToken($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS)) {
0 ignored issues
show
Bug introduced by
It seems like $shouldUseLexer ? Doctri...ype::T_OPEN_PARENTHESIS 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

51
        if ($lexer->isNextToken(/** @scrutinizer ignore-type */ $shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS)) {
Loading history...
52
            $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS);
53
            $parameter = $parser->Literal();
54
            $parameters = [$parameter->value];
55
            if ($lexer->isNextToken($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...
56
                while ($lexer->isNextToken($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA)) {
57
                    $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
58
                    $parameter = $parser->Literal();
59
                    $parameters[] = $parameter->value;
60
                }
61
            }
62
            $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...
63
            $type .= '('.\implode(', ', $parameters).')';
64
        }
65
66
        $this->targetType = $type;
67
68
        $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS);
69
    }
70
71
    public function getSql(SqlWalker $sqlWalker): string
72
    {
73
        return \sprintf('cast(%s as %s)', $this->sourceType->dispatch($sqlWalker), $this->targetType);
74
    }
75
}
76