Completed
Push — master ( 87b7b8...308e52 )
by Adrian
01:24
created

Where::whereMultiple()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 14
cp 0.8571
rs 9.2088
c 0
b 0
f 0
cc 5
nc 7
nop 3
crap 5.0729
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Sql\Clause;
5
6
use Sirius\Sql\Component\Conditions;
7
8
trait Where
9
{
10
    /**
11
     * @var Conditions
12
     */
13
    protected $where;
14
15
    /**
16
     * Adds a column based condition condition
17
     *
18
     * @param string $column
19
     * @param null $value
20
     * @param string $condition
21
     *
22
     * @return $this
23
     */
24 14
    public function where($column, $value = null, $condition = '=')
25
    {
26 14
        if (is_array($column) && is_array($value)) {
27 1
            return $this->whereMultiple($column, $value, $condition);
28
        }
29 14
        if (count(func_get_args()) == 1) {
30 4
            $this->where->and($column, null, null);
31
        } else {
32 11
            $this->where->and($column, $value, $condition);
33
        }
34
35 14
        return $this;
36
    }
37
38 1
    public function whereSprintf(string $format, ...$bindInline)
39
    {
40 1
        $this->where->andSprintf($format, ...$bindInline);
41
42 1
        return $this;
43
    }
44
45
    /**
46
     * Helper method for situation when you need to query by primary key
47
     * which consists of multiple columns. Only works for `=` and `IN`
48
     *
49
     * @param array $columns
50
     * @param array $values
51
     * @param $condition
52
     *
53
     * @return $this
54
     */
55 1
    public function whereMultiple(array $columns, array $values, $condition)
56
    {
57 1
        $condition = strtoupper($condition);
58 1
        if ( ! in_array($condition, ['=', 'IN'])) {
59
            throw new \InvalidArgumentException('For `whereMultiple` the condition `' . $condition . '` is not allowed');
60
        }
61
62 1
        if ( ! is_array($values[0])) {
63
            $values = [$values];
64
        }
65
66 1
        $this->whereStartSet();
67 1
        foreach ($values as $value) {
68
            // match ALL of the column-value pairs
69 1
            $this->orWhereStartSet();
70 1
            foreach ($columns as $k => $column) {
71 1
                $this->where($column, $value[$k]);
72
            }
73 1
            $this->endSet();
74
        }
75
76 1
        $this->endSet();
77
78 1
        return $this;
79
    }
80
81 4
    public function orWhere(string $column, $value = null, $condition = '=')
82
    {
83 4
        if (count(func_get_args()) == 1) {
84 1
            $this->where->or($column, null, null);
85
        } else {
86 3
            $this->where->or($column, $value, $condition);
87
        }
88
89 4
        return $this;
90
    }
91
92 1
    public function orWhereSprintf(string $format, ...$bindInline)
93
    {
94 1
        $this->where->orSprintf($format, ...$bindInline);
95
96 1
        return $this;
97
    }
98
99 1
    public function whereAll(array $columnsValues, $isSet = true)
100
    {
101 1
        if ($isSet) {
102 1
            $this->whereStartSet();
103
        }
104
105 1
        foreach ($columnsValues as $key => $val) {
106 1
            if (is_numeric($key)) {
107 1
                $this->where($val);
108 1
            } elseif ($val === null) {
109 1
                $this->where("{$key} IS NULL");
110
            } else {
111 1
                $this->where("{$key}", $val);
112
            }
113
        }
114
115
116 1
        if ($isSet) {
117 1
            $this->endSet();
118
        }
119
120 1
        return $this;
121
    }
122
123 4
    public function whereStartSet()
124
    {
125 4
        $this->where->openGroup();
126
127 4
        return $this;
128
    }
129
130 2
    public function orWhereStartSet()
131
    {
132 2
        $this->where->openGroup('OR');
133
134 2
        return $this;
135
    }
136
137 3
    public function endSet()
138
    {
139 3
        $this->where->closeGroup();
140
141 3
        return $this;
142
143
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
144
    }
145
146 1
    public function groupCurrentWhere()
147
    {
148 1
        $this->where->groupCurrent();
149
150 1
        return $this;
151
    }
152
153 19
    public function resetWhere()
154
    {
155 19
        $this->where = new Conditions($this->bindings, 'WHERE');
0 ignored issues
show
Bug introduced by
The property bindings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
156
157 19
        return $this;
158
    }
159
}
160