Passed
Pull Request — main (#116)
by Martin
03:46 queued 01:45
created

Cast   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 23
c 2
b 0
f 0
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 26 4
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
13
/**
14
 * Implementation of PostgreSql CAST().
15
 *
16
 * @see https://www.postgresql.org/docs/current/sql-createcast.html
17
 * @see https://github.com/beberlei/DoctrineExtensions/blob/f3536d881637f6ddc7ca1d6595d18c15e06eb1d9/src/Query/Mysql/Cast.php
18
 * @since 2.0.0
19
 *
20
 * @author Mathieu Piot <https://github.com/mpiot>
21
 */
22
class Cast extends FunctionNode
23
{
24
    public Node $sourceType;
25
26
    public string $targetType;
27
28
    public function parse(Parser $parser): void
29
    {
30
        $parser->match(Lexer::T_IDENTIFIER);
0 ignored issues
show
Bug introduced by
Doctrine\ORM\Query\Lexer::T_IDENTIFIER of type integer is incompatible with the type Doctrine\ORM\Query\Lexer expected by parameter $token of Doctrine\ORM\Query\Parser::match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $parser->match(/** @scrutinizer ignore-type */ Lexer::T_IDENTIFIER);
Loading history...
31
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
32
        $this->sourceType = $parser->SimpleArithmeticExpression();
33
        $parser->match(Lexer::T_AS);
34
        $parser->match(Lexer::T_IDENTIFIER);
35
36
        $type = $parser->getLexer()->token['value'];
37
        if ($parser->getLexer()->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
0 ignored issues
show
Bug introduced by
Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS of type integer is incompatible with the type Doctrine\Common\Lexer\T expected by parameter $type of Doctrine\Common\Lexer\AbstractLexer::isNextToken(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        if ($parser->getLexer()->isNextToken(/** @scrutinizer ignore-type */ Lexer::T_OPEN_PARENTHESIS)) {
Loading history...
38
            $parser->match(Lexer::T_OPEN_PARENTHESIS);
39
            $parameter = $parser->Literal();
40
            $parameters = [$parameter->value];
41
            if ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
42
                while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
43
                    $parser->match(Lexer::T_COMMA);
44
                    $parameter = $parser->Literal();
45
                    $parameters[] = $parameter->value;
46
                }
47
            } $parser->match(Lexer::T_CLOSE_PARENTHESIS);
48
            $type .= '('.\implode(', ', $parameters).')';
49
        }
50
51
        $this->targetType = $type;
52
53
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
54
    }
55
56
    public function getSql(SqlWalker $sqlWalker): string
57
    {
58
        return \sprintf('cast(%s as %s)', $this->sourceType->dispatch($sqlWalker), $this->targetType);
59
    }
60
}
61