Where::endSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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(
60
                'For `whereMultiple` the condition `' . $condition . '` is not allowed'
61
            );
62
        }
63
64 1
        if (! is_array($values[0])) {
65
            $values = [$values];
66
        }
67
68 1
        $this->whereStartSet();
69 1
        foreach ($values as $value) {
70
            // match ALL of the column-value pairs
71 1
            $this->orWhereStartSet();
72 1
            foreach ($columns as $k => $column) {
73 1
                $this->where($column, $value[$k]);
74
            }
75 1
            $this->endSet();
76
        }
77
78 1
        $this->endSet();
79
80 1
        return $this;
81
    }
82
83 4
    public function orWhere(string $column, $value = null, $condition = '=')
84
    {
85 4
        if (count(func_get_args()) == 1) {
86 1
            $this->where->or($column, null, null);
87
        } else {
88 3
            $this->where->or($column, $value, $condition);
89
        }
90
91 4
        return $this;
92
    }
93
94 1
    public function orWhereSprintf(string $format, ...$bindInline)
95
    {
96 1
        $this->where->orSprintf($format, ...$bindInline);
97
98 1
        return $this;
99
    }
100
101 1
    public function whereAll(array $columnsValues, $isSet = true)
102
    {
103 1
        if ($isSet) {
104 1
            $this->whereStartSet();
105
        }
106
107 1
        foreach ($columnsValues as $key => $val) {
108 1
            if (is_numeric($key)) {
109 1
                $this->where($val);
110 1
            } elseif ($val === null) {
111 1
                $this->where("{$key} IS NULL");
112
            } else {
113 1
                $this->where("{$key}", $val);
114
            }
115
        }
116
117
118 1
        if ($isSet) {
119 1
            $this->endSet();
120
        }
121
122 1
        return $this;
123
    }
124
125 4
    public function whereStartSet()
126
    {
127 4
        $this->where->openGroup();
128
129 4
        return $this;
130
    }
131
132 2
    public function orWhereStartSet()
133
    {
134 2
        $this->where->openGroup('OR');
135
136 2
        return $this;
137
    }
138
139 3
    public function endSet()
140
    {
141 3
        $this->where->closeGroup();
142
143 3
        return $this;
144
145
        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...
146
    }
147
148 1
    public function groupCurrentWhere()
149
    {
150 1
        $this->where->groupCurrent();
151
152 1
        return $this;
153
    }
154
155 19
    public function resetWhere()
156
    {
157 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...
158
159 19
        return $this;
160
    }
161
}
162