Completed
Push — master ( 01ab1e...940805 )
by Changwan
04:37
created

ComparisonExpression::__construct()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 7
nop 3
dl 0
loc 20
ccs 14
cts 14
cp 1
crap 8
rs 7.7777
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Query\Expression;
3
4
use Traversable;
5
use Wandu\Database\Contracts\ExpressionInterface;
6
use Wandu\Database\Support\Helper;
7
8
/**
9
 * ComparisonExpression = '`' $name '` ' $operator ' ' $value
10
 * 
11
 * @example `abc` = 30
12
 * @example `foo` > 'foo string'
13
 */
14
class ComparisonExpression implements ExpressionInterface
15
{
16
    /** @var array */
17
    protected static $ops = [
18
        '<',
19
        '>',
20
        '>=',
21
        '<=',
22
        '=',
23
        '!=',
24
        '<>',
25
        '<=>',
26
        'IS',
27
        'IS NOT',
28
        'IS NOT NULL',
29
        'IS NULL',
30
        'LIKE',
31
        'NOT LIKE',
32
    ];
33
    
34
    /** @var string */
35
    protected $name;
36
    
37
    /** @var string */
38
    protected $operator;
39
    
40
    /** @var array|string */
41
    protected $value;
42
    
43
    /**
44
     * WhereStatement constructor.
45
     * @param string $name
46
     * @param string $operator
47
     * @param string|array|\Wandu\Database\Contracts\ExpressionInterface $value
48
     */
49 33
    public function __construct($name, $operator = null, $value = null)
50
    {
51 33
        if (!isset($value)) {
52 27
            if (!isset($operator)) {
53 1
                $operator = 'IS NULL';
54
            } else {
55 26
                $upperOp = strtoupper($operator);
56 26
                if (in_array($upperOp, static::$ops)) {
57 10
                    if ($upperOp === '=' || $upperOp === 'IS') $operator = 'IS NULL';
58 10
                    if ($upperOp === '!=' || $upperOp === 'IS NOT') $operator = 'IS NOT NULL';
59
                } else {
60 16
                    $value = $operator;
61 16
                    $operator = '=';
62
                }
63
            }
64
        }
65 33
        $this->name = $name;
66 33
        $this->operator = strtoupper($operator);
67 33
        $this->value = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type object<Wandu\Database\Co...ts\ExpressionInterface>. However, the property $value is declared as type array|string. 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...
68 33
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 35
    public function toSql()
74
    {
75 35
        $name = Helper::normalizeName($this->name);
76 35
        $operator = $this->operator;
77 35
        $value = $this->value;
78 35
        if ($this->operator === 'IN') {
79 3
            if ($value instanceof ExpressionInterface) {
80 1
                return "{$name} {$operator} (" . $value->toSql() . ")";
81
            }
82 2
            if ($value instanceof Traversable) {
83 1
                $value = iterator_to_array($value);
84
            }
85 2
            return Helper::stringRepeat(', ', '?', count($value), "{$name} {$operator} (", ")");
86
        }
87 34
        if ($value instanceof ExpressionInterface) {
88 1
            return "{$name} {$operator} " . $value->toSql();
89
        }
90 34
        if (isset($value)) {
91 23
            return "{$name} {$operator} ?";
92
        } else {
93 11
            return "{$name} {$operator}";
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 35
    public function getBindings()
101
    {
102
103 35
        if (is_array($this->value) || $this->value instanceof Traversable) {
104 5
            $bindings = [];
105 5
            foreach ($this->value as $value) {
106 5
                if ($value instanceof ExpressionInterface) {
107
                    $bindings = array_merge($bindings, $value->getBindings());
108
                } else {
109 5
                    $bindings[] = $value;
110
                }
111
            }
112 5
            return $bindings;
113
        }
114 34
        if (isset($this->value)) {
115 23
            if ($this->value instanceof ExpressionInterface) {
116 2
                return $this->value->getBindings();
117
            }
118 23
            return [$this->value];
119
        }
120 11
        return [];
121
    }
122
}
123