| Conditions | 18 |
| Paths | 57 |
| Total Lines | 108 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 127 | public function where($column, $operator = null, $value = null, $boolean = 'and') |
||
| 128 | { |
||
| 129 | if ($column instanceof ConditionExpression) { |
||
| 130 | $type = 'Expression'; |
||
| 131 | |||
| 132 | $this->wheres[] = compact('type', 'column', 'boolean'); |
||
| 133 | |||
| 134 | return $this; |
||
| 135 | } |
||
| 136 | |||
| 137 | // If the column is an array, we will assume it is an array of key-value pairs |
||
| 138 | // and can add them each as a where clause. We will maintain the boolean we |
||
| 139 | // received when the method was called and pass it into the nested where. |
||
| 140 | if (is_array($column)) { |
||
| 141 | return $this->addArrayOfWheres($column, $boolean); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Here we will make some assumptions about the operator. If only 2 values are |
||
| 145 | // passed to the method, we will assume that the operator is an equals sign |
||
| 146 | // and keep going. Otherwise, we'll require the operator to be passed in. |
||
| 147 | [$value, $operator] = $this->prepareValueAndOperator( |
||
| 148 | $value, |
||
| 149 | $operator, |
||
| 150 | func_num_args() === 2 |
||
| 151 | ); |
||
| 152 | |||
| 153 | // If the column is actually a Closure instance, we will assume the developer |
||
| 154 | // wants to begin a nested where statement which is wrapped in parentheses. |
||
| 155 | // We will add that Closure to the query and return back out immediately. |
||
| 156 | if ($column instanceof Closure && is_null($operator)) { |
||
| 157 | return $this->whereNested($column, $boolean); |
||
| 158 | } |
||
| 159 | |||
| 160 | // If the column is a Closure instance and there is an operator value, we will |
||
| 161 | // assume the developer wants to run a subquery and then compare the result |
||
| 162 | // of that subquery with the given value that was provided to the method. |
||
| 163 | if ($this->isQueryable($column) && !is_null($operator)) { |
||
| 164 | [$sub, $bindings] = $this->createSub($column); |
||
| 165 | |||
| 166 | //FIXME: Check for limit in query, if limit 1, surround subquery with first(()); |
||
| 167 | |||
| 168 | if (!empty($bindings)) { |
||
| 169 | $this->addBinding($bindings, 'where'); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $this->where(new Expression('(' . $sub . ')'), $operator, $value, $boolean); |
||
| 173 | } |
||
| 174 | |||
| 175 | // If the given operator is not found in the list of valid operators we will |
||
| 176 | // assume that the developer is just short-cutting the '==' operators and |
||
| 177 | // we will set the operators to '==' and set the values appropriately. |
||
| 178 | if ($this->invalidOperator($operator)) { |
||
| 179 | [$value, $operator] = [$operator, '==']; |
||
| 180 | } |
||
| 181 | |||
| 182 | // If the value is a Closure, it means the developer is performing an entire |
||
| 183 | // sub-select within the query and we will need to compile the sub-select |
||
| 184 | // within the where clause to get the appropriate query record results. |
||
| 185 | if ($this->isQueryable($value)) { |
||
| 186 | return $this->whereSub($column, $operator, $value, $boolean); |
||
| 187 | } |
||
| 188 | |||
| 189 | // If the value is "null", we will just assume the developer wants to add a |
||
| 190 | // where null clause to the query. So, we will allow a short-cut here to |
||
| 191 | // that method for convenience so the developer doesn't have to check. |
||
| 192 | if (is_null($value)) { |
||
| 193 | return $this->whereNull($column, $boolean, $operator !== '=='); |
||
| 194 | } |
||
| 195 | |||
| 196 | $type = 'Basic'; |
||
| 197 | |||
| 198 | $columnString = ($column instanceof ExpressionContract) |
||
| 199 | ? $this->grammar->getValue($column) |
||
| 200 | : $column; |
||
| 201 | |||
| 202 | // If the column is making a JSON reference we'll check to see if the value |
||
| 203 | // is a boolean. If it is, we'll add the raw boolean string as an actual |
||
| 204 | // value to the query to ensure this is properly handled by the query. |
||
| 205 | if (str_contains($columnString, '->') && is_bool($value)) { |
||
| 206 | $value = new Expression($value ? 'true' : 'false'); |
||
| 207 | |||
| 208 | if (is_string($column)) { |
||
| 209 | $type = 'JsonBoolean'; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($this->isBitwiseOperator($operator)) { |
||
| 214 | $type = 'Bitwise'; |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!$value instanceof ExpressionContract) { |
||
| 218 | $value = $this->bindValue($this->flattenValue($value)); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Now that we are working with just a simple query we can put the elements |
||
| 222 | // in our array and add the query binding to our array of bindings that |
||
| 223 | // will be bound to each SQL statements when it is finally executed. |
||
| 224 | $this->wheres[] = compact( |
||
| 225 | 'type', |
||
| 226 | 'column', |
||
| 227 | 'operator', |
||
| 228 | 'value', |
||
| 229 | 'boolean' |
||
| 230 | ); |
||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 391 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.