Cast::parse()   C
last analyzed

Complexity

Conditions 16
Paths 4

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 16.0185

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 23
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 36
ccs 23
cts 24
cp 0.9583
crap 16.0185
rs 5.5666

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\Functions\FunctionNode;
8
use Doctrine\ORM\Query\AST\Node;
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\DoctrineLexer;
14
use MartinGeorgiev\Utils\DoctrineOrm;
15
16
/**
17
 * Implementation of PostgreSQL CAST().
18
 *
19
 * @see https://www.postgresql.org/docs/17/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 1
    public function parse(Parser $parser): void
32
    {
33 1
        $shouldUseLexer = DoctrineOrm::isPre219();
34
35 1
        $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 1
        $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
38 1
        $this->sourceType = $parser->SimpleArithmeticExpression();
39 1
        $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...
40 1
        $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER);
41
42 1
        $lexer = $parser->getLexer();
43 1
        $type = DoctrineLexer::getTokenValue($lexer);
44 1
        if (!\is_string($type)) {
45
            return;
46
        }
47
48 1
        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

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