Completed
Push — master ( 8a48e7...a0ab2a )
by Changwan
03:17
created

ComparisonExpression   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B toSql() 0 19 5
B getBindings() 0 13 5
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
    const OPERATOR_LT = '<';
17
    const OPERATOR_LTE = '<=';
18
    const OPERATOR_GT = '>';
19
    const OPERATOR_GTE = '>=';
20
    const OPERATOR_EQ = '=';
21
    
22
    /** @var string */
23
    protected $name;
24
    
25
    /** @var string */
26
    protected $operator;
27
    
28
    /** @var array|string */
29
    protected $value;
30
    
31
    /**
32
     * WhereStatement constructor.
33
     * @param string $name
34
     * @param string $operator
35
     * @param string|array|\Wandu\Database\Contracts\ExpressionInterface $value
36
     */
37 24
    public function __construct($name, $operator, $value)
38
    {
39 24
        $this->name = $name;
40 24
        $this->operator = strtoupper($operator);
41 24
        $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...
42 24
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 26
    public function toSql()
48
    {
49 26
        $name = Helper::normalizeName($this->name);
50 26
        $operator = $this->operator;
51 26
        $value = $this->value;
52 26
        if ($this->operator === 'IN') {
53 3
            if ($value instanceof ExpressionInterface) {
54 1
                return "{$name} {$operator} (" . $value->toSql() . ")";
55
            }
56 2
            if ($value instanceof Traversable) {
57 1
                $value = iterator_to_array($value);
58
            }
59 2
            return Helper::stringRepeat(', ', '?', count($value), "{$name} {$operator} (", ")");
60
        }
61 25
        if ($value instanceof ExpressionInterface) {
62 1
            return "{$name} {$operator} " . $value->toSql();
63
        }
64 25
        return "{$name} {$operator} ?";
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 26
    public function getBindings()
71
    {
72 26
        $bindings = [];
73 26
        $values = (is_array($this->value) || $this->value instanceof Traversable) ? $this->value : [$this->value];
74 26
        foreach ($values as $value) {
75 26
            if ($value instanceof ExpressionInterface) {
76 2
                $bindings = array_merge($bindings, $value->getBindings());
77
            } else {
78 26
                $bindings[] = $value;
79
            }
80
        }
81 26
        return $bindings;
82
    }
83
}
84