Completed
Pull Request — master (#8105)
by
unknown
10:29
created

DateDiffFunction::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\SqlWalker;
11
12
/**
13
 * "DATE_DIFF" "(" ArithmeticPrimary "," ArithmeticPrimary ")"
14
 */
15
class DateDiffFunction extends FunctionNode
16
{
17
    /** @var Node */
18
    public $date1;
19
20
    /** @var Node */
21
    public $date2;
22
23
    /**
24
     * @override
25
     * @inheritdoc
26
     */
27
    public function getSql(SqlWalker $sqlWalker)
28
    {
29
        return $sqlWalker->getConnection()->getDatabasePlatform()->getDateDiffExpression(
30
            $this->date1->dispatch($sqlWalker),
31
            $this->date2->dispatch($sqlWalker)
32
        );
33
    }
34
35
    /**
36
     * @override
37
     * @inheritdoc
38
     */
39
    public function parse(Parser $parser)
40
    {
41
        $parser->match(Lexer::T_IDENTIFIER);
42
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
43
44
        $this->date1 = $parser->ArithmeticPrimary();
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->ArithmeticPrimary() can also be of type string. However, the property $date1 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...
45
        $parser->match(Lexer::T_COMMA);
46
        $this->date2 = $parser->ArithmeticPrimary();
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->ArithmeticPrimary() can also be of type string. However, the property $date2 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...
47
48
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
49
    }
50
}
51