Conditions | 16 |
Paths | 265 |
Total Lines | 85 |
Code Lines | 52 |
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 |
||
117 | protected function manyFilter(DataQuery $query, $inclusive) |
||
118 | { |
||
119 | $this->model = $query->applyRelation($this->relation); |
||
120 | $caseSensitive = $this->getCaseSensitive(); |
||
121 | |||
122 | // Check values for null |
||
123 | $field = $this->getDbName(); |
||
124 | $values = $this->getValue(); |
||
125 | if (empty($values)) { |
||
126 | throw new \InvalidArgumentException("Cannot filter {$field} against an empty set"); |
||
127 | } |
||
128 | $hasNull = in_array(null, $values, true); |
||
129 | if ($hasNull) { |
||
130 | $values = array_filter($values, function ($value) { |
||
131 | return $value !== null; |
||
132 | }); |
||
133 | } |
||
134 | |||
135 | $connective = ''; |
||
136 | if (empty($values)) { |
||
137 | $predicate = ''; |
||
138 | } elseif ($caseSensitive === null) { |
||
139 | // For queries using the default collation (no explicit case) we can use the WHERE .. NOT IN .. syntax, |
||
140 | // providing simpler SQL than many WHERE .. AND .. fragments. |
||
141 | $column = $this->getDbName(); |
||
142 | $placeholders = DB::placeholders($values); |
||
143 | if ($inclusive) { |
||
144 | $predicate = "$column IN ($placeholders)"; |
||
145 | } else { |
||
146 | $predicate = "$column NOT IN ($placeholders)"; |
||
147 | } |
||
148 | } else { |
||
149 | // Generate reusable comparison clause |
||
150 | $comparisonClause = DB::get_conn()->comparisonClause( |
||
151 | $this->getDbName(), |
||
152 | null, |
||
153 | true, // exact? |
||
154 | !$inclusive, // negate? |
||
155 | $this->getCaseSensitive(), |
||
156 | true |
||
157 | ); |
||
158 | $count = count($values); |
||
159 | if ($count > 1) { |
||
160 | $connective = $inclusive ? ' OR ' : ' AND '; |
||
161 | $conditions = array_fill(0, $count, $comparisonClause); |
||
162 | $predicate = implode($connective, $conditions); |
||
163 | } else { |
||
164 | $predicate = $comparisonClause; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // Always check for null when doing exclusive checks (either AND IS NOT NULL / OR IS NULL) |
||
169 | // or when including the null value explicitly (OR IS NULL) |
||
170 | if ($hasNull || !$inclusive) { |
||
171 | // If excluding values which don't include null, or including |
||
172 | // values which include null, we should do an `OR IS NULL`. |
||
173 | // Otherwise we are excluding values that do include null, so `AND IS NOT NULL`. |
||
174 | // Simplified from (!$inclusive && !$hasNull) || ($inclusive && $hasNull); |
||
175 | $isNull = !$hasNull || $inclusive; |
||
176 | $nullCondition = DB::get_conn()->nullCheckClause($field, $isNull); |
||
177 | |||
178 | // Determine merge strategy |
||
179 | if (empty($predicate)) { |
||
180 | $predicate = $nullCondition; |
||
181 | } else { |
||
182 | // Merge null condition with predicate |
||
183 | if ($isNull) { |
||
184 | $nullCondition = " OR {$nullCondition}"; |
||
185 | } else { |
||
186 | $nullCondition = " AND {$nullCondition}"; |
||
187 | } |
||
188 | // If current predicate connective doesn't match the same as the null connective |
||
189 | // make sure to group the prior condition |
||
190 | if ($connective && (($connective === ' OR ') !== $isNull)) { |
||
191 | $predicate = "({$predicate})"; |
||
192 | } |
||
193 | $predicate .= $nullCondition; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | $clause = [$predicate => $values]; |
||
198 | |||
199 | return $this->aggregate ? |
||
200 | $this->applyAggregate($query, $clause) : |
||
201 | $query->where($clause); |
||
202 | } |
||
209 |