Cast   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 63
ccs 32
cts 33
cp 0.9697
rs 10
wmc 21

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSql() 0 3 1
D parse() 0 52 20
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
        // Handle parameterized types (e.g., DECIMAL(10, 2))
49 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

49
        if ($lexer->isNextToken(/** @scrutinizer ignore-type */ $shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS)) {
Loading history...
50 1
            $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS);
51 1
            $parameter = $parser->Literal();
52 1
            $parameters = [$parameter->value];
53 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...
54 1
                while ($lexer->isNextToken($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA)) {
55 1
                    $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
56 1
                    $parameter = $parser->Literal();
57 1
                    $parameters[] = $parameter->value;
58
                }
59
            }
60
61 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...
62 1
            $type .= '('.\implode(', ', $parameters).')';
63
        }
64
65
        // Handle array types by checking if the next token is '['
66
        // Since brackets are not recognized as specific tokens, we need to check the token value
67 1
        $nextTokenValue = DoctrineLexer::getLookaheadValue($lexer);
68 1
        if ($nextTokenValue === '[') {
69
            // Consume the '[' token
70 1
            $parser->match($shouldUseLexer ? Lexer::T_NONE : TokenType::T_NONE);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
72
            // Check for the closing ']' token
73 1
            $nextTokenValue = DoctrineLexer::getLookaheadValue($lexer);
74 1
            if ($nextTokenValue === ']') {
75 1
                $parser->match($shouldUseLexer ? Lexer::T_NONE : TokenType::T_NONE);
76 1
                $type .= '[]';
77
            }
78
        }
79
80 1
        $this->targetType = $type;
81
82 1
        $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS);
83
    }
84
85 1
    public function getSql(SqlWalker $sqlWalker): string
86
    {
87 1
        return \sprintf('cast(%s as %s)', $this->sourceType->dispatch($sqlWalker), $this->targetType);
88
    }
89
}
90