Cast   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 47
ccs 25
cts 26
cp 0.9615
rs 10
wmc 17

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 36 16
A getSql() 0 3 1
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