Completed
Push — 161-add-gedmo-doctrine-extensi... ( cd7ad0...9dbae5 )
by jelmer
22:18 queued 09:17
created

MatchAgainst::getSql()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 8.6845
cc 4
eloc 14
nc 6
nop 1
crap 20
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\Extensions\Doctrine;
4
5
use Doctrine\ORM\Query\AST\InputParameter;
6
use Doctrine\ORM\Query\AST\Literal;
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\SqlWalker;
11
12
/**
13
 * "MATCH_AGAINST" "(" {StateFieldPathExpression ","}* InParameter {Literal}? ")"
14
 */
15
class MatchAgainst extends FunctionNode
16
{
17
    /** @var array */
18
    private $columns = [];
19
20
    /** @var string|InputParameter */
21
    private $needle;
22
23
    /** @var Literal */
24
    private $mode;
25
26
    public function parse(Parser $parser)
27
    {
28
        $parser->match(Lexer::T_IDENTIFIER);
29
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
30
31
        while ($parser->getLexer()->isNextToken(Lexer::T_IDENTIFIER)) {
32
            $this->columns[] = $parser->StateFieldPathExpression();
33
            $parser->match(Lexer::T_COMMA);
34
        };
35
36
        $this->needle = $parser->InParameter();
0 ignored issues
show
Documentation Bug introduced by
It seems like $parser->InParameter() can also be of type object<Doctrine\ORM\Query\AST\Literal>. However, the property $needle is declared as type string|object<Doctrine\O...ery\AST\InputParameter>. 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...
37
38
        while ($parser->getLexer()->isNextToken(Lexer::T_STRING)) {
39
            $this->mode = $parser->Literal();
40
        }
41
42
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
43
    }
44
45
    public function getSql(SqlWalker $sqlWalker)
46
    {
47
        $haystack = null;
48
        $first = true;
49
        foreach ($this->columns as $column) {
50
            if (!$first) {
51
                $haystack .= ', ';
52
            }
53
54
            $first = false;
55
56
            $haystack .= $column->dispatch($sqlWalker);
57
        }
58
59
        $query = "MATCH(" . $haystack . ") AGAINST (" . $this->needle->dispatch($sqlWalker);
60
61
        if ($this->mode) {
62
            $query .= " " . $this->mode->value . " )";
63
        } else {
64
            $query .= " )";
65
        }
66
67
        return $query;
68
    }
69
}
70