Passed
Pull Request — master (#7065)
by Michael
11:41
created

DateAddFunction::getSql()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 8.0155

Importance

Changes 0
Metric Value
cc 8
eloc 32
nop 1
dl 0
loc 42
ccs 30
cts 32
cp 0.9375
crap 8.0155
rs 5.3846
c 0
b 0
f 0
nc 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\AST\Node;
8
use Doctrine\ORM\Query\Lexer;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\QueryException;
11
use Doctrine\ORM\Query\SqlWalker;
12
use function strtolower;
13
14
/**
15
 * "DATE_ADD" "(" ArithmeticPrimary "," ArithmeticPrimary "," StringPrimary ")"
16
 */
17
class DateAddFunction extends FunctionNode
18
{
19
    /** @var Node */
20
    public $firstDateExpression;
21
22
    /** @var Node */
23
    public $intervalExpression;
24
25
    /** @var Node */
26
    public $unit;
27
28
    /**
29
     * @override
30
     * @inheritdoc
31
     */
32 8
    public function getSql(SqlWalker $sqlWalker)
33
    {
34 8
        switch (strtolower($this->unit->value)) {
35 8
            case 'second':
36 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
37 1
                    $this->firstDateExpression->dispatch($sqlWalker),
38 1
                    $this->intervalExpression->dispatch($sqlWalker)
39
                );
40 7
            case 'minute':
41 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression(
42 1
                    $this->firstDateExpression->dispatch($sqlWalker),
43 1
                    $this->intervalExpression->dispatch($sqlWalker)
44
                );
45 6
            case 'hour':
46 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
47 1
                    $this->firstDateExpression->dispatch($sqlWalker),
48 1
                    $this->intervalExpression->dispatch($sqlWalker)
49
                );
50 5
            case 'day':
51 2
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
52 2
                    $this->firstDateExpression->dispatch($sqlWalker),
53 2
                    $this->intervalExpression->dispatch($sqlWalker)
54
                );
55 3
            case 'week':
56 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression(
57 1
                    $this->firstDateExpression->dispatch($sqlWalker),
58 1
                    $this->intervalExpression->dispatch($sqlWalker)
59
                );
60 2
            case 'month':
61 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
62 1
                    $this->firstDateExpression->dispatch($sqlWalker),
63 1
                    $this->intervalExpression->dispatch($sqlWalker)
64
                );
65 1
            case 'year':
66 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression(
67 1
                    $this->firstDateExpression->dispatch($sqlWalker),
68 1
                    $this->intervalExpression->dispatch($sqlWalker)
69
                );
70
71
            default:
72
                throw QueryException::semanticalError(
73
                    'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.'
74
                );
75
        }
76
    }
77
78
    /**
79
     * @override
80
     * @inheritdoc
81
     */
82 15
    public function parse(Parser $parser)
83
    {
84 15
        $parser->match(Lexer::T_IDENTIFIER);
85 15
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
86
87 15
        $this->firstDateExpression = $parser->ArithmeticPrimary();
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->ArithmeticPrimary() can also be of type string. However, the property $firstDateExpression is declared as type Doctrine\ORM\Query\AST\Node. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88 15
        $parser->match(Lexer::T_COMMA);
89 15
        $this->intervalExpression = $parser->ArithmeticPrimary();
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->ArithmeticPrimary() can also be of type string. However, the property $intervalExpression is declared as type Doctrine\ORM\Query\AST\Node. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90 15
        $parser->match(Lexer::T_COMMA);
91 15
        $this->unit = $parser->StringPrimary();
92
93 15
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
94 15
    }
95
}
96