Complex classes like WhereStatement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WhereStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WhereStatement extends Statement |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var bool |
||
| 19 | */ |
||
| 20 | protected $having; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $conditions = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param bool $having when true, statement becomes a having statement |
||
| 29 | */ |
||
| 30 | public function __construct($having = false) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Tells whether this statement is a HAVING statement. |
||
| 37 | * |
||
| 38 | * @return bool true: is HAVING, false: is WHERE |
||
| 39 | */ |
||
| 40 | public function isHaving() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Adds a condition to the statement. |
||
| 47 | * |
||
| 48 | * Accepts the following forms: |
||
| 49 | * 1. Equality comparison: |
||
| 50 | * addCondition('username', 'john') |
||
| 51 | * 2. Comparison with custom operator: |
||
| 52 | * addCondition('balance', 100, '>') |
||
| 53 | * 3. IN statement: |
||
| 54 | * addCondition('group', ['admin', 'owner']) |
||
| 55 | * 4. SQL fragment: |
||
| 56 | * addCondition('name LIKE "%john%"') |
||
| 57 | * 5. Subquery: |
||
| 58 | * addCondition(function(SelectQuery $query) {}) |
||
| 59 | * 6. List of conditions to add: |
||
| 60 | * addCondition([['balance', 100, '>'], |
||
| 61 | * ['user_id', 5]]) |
||
| 62 | * 7. Map of equality comparisons: |
||
| 63 | * addCondition(['username' => 'john', |
||
| 64 | * 'user_id' => 5]) |
||
| 65 | * 8. List of SQL fragments: |
||
| 66 | * addCondition(['first_name LIKE "%john%"', |
||
| 67 | * 'last_name LIKE "%doe%"']) |
||
| 68 | * |
||
| 69 | * @param array|string $field |
||
| 70 | * @param string|bool $value condition value (optional) |
||
| 71 | * @param string $operator operator (optional) |
||
| 72 | * |
||
| 73 | * @return self |
||
| 74 | */ |
||
| 75 | public function addCondition($field, $value = false, $operator = '=') |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Adds an OR condition. Uses same arguments as |
||
| 117 | * adding AND conditions. |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | public function addOrCondition() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Adds a between condition to the query. |
||
| 130 | * |
||
| 131 | * @param string $field |
||
| 132 | * @param mixed $a first between value |
||
| 133 | * @param mixed $b second between value |
||
| 134 | * |
||
| 135 | * @return self |
||
| 136 | */ |
||
| 137 | public function addBetweenCondition($field, $a, $b) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Adds a not between condition to the query. |
||
| 146 | * |
||
| 147 | * @param string $field |
||
| 148 | * @param mixed $a first between value |
||
| 149 | * @param mixed $b second between value |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | public function addNotBetweenCondition($field, $a, $b) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Adds an exists condition to the query. |
||
| 162 | * |
||
| 163 | * @param callable $f |
||
| 164 | * |
||
| 165 | * @return self |
||
| 166 | */ |
||
| 167 | public function addExistsCondition(callable $f) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds a not exists condition to the query. |
||
| 176 | * |
||
| 177 | * @param callable $f |
||
| 178 | * |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | public function addNotExistsCondition(callable $f) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Gets the conditions for this statement. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getConditions() |
||
| 197 | |||
| 198 | public function build() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Builds a parameterized and escaped SQL fragment |
||
| 221 | * for a condition that uses our own internal |
||
| 222 | * representation. |
||
| 223 | * |
||
| 224 | * A condition is represented by an array, and can be |
||
| 225 | * have one of the following forms: |
||
| 226 | * 1. ['SQL fragment'] |
||
| 227 | * 2. ['identifier', '=', 'value'] |
||
| 228 | * 3. ['BETWEEN', 'identifier', 'value', 'value', true] |
||
| 229 | * 4. ['EXISTS', function(SelectQuery $query) {}, true] |
||
| 230 | * 5. [function(SelectQuery $query) {}] |
||
| 231 | * 6. [function(SelectQuery $query) {}, '=', 'value'] |
||
| 232 | * |
||
| 233 | * @param array $cond |
||
| 234 | * |
||
| 235 | * @return string generated SQL fragment |
||
| 236 | */ |
||
| 237 | protected function buildClause(array $cond) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Builds a subquery. |
||
| 287 | * |
||
| 288 | * @param callable $f |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function buildSubquery(callable $f) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Builds an EXISTS clause. |
||
| 305 | * |
||
| 306 | * @param callable $f |
||
| 307 | * @param bool $isExists |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | protected function buildExists(callable $f, $isExists) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Builds a BETWEEN clause. |
||
| 320 | * |
||
| 321 | * @param string $field |
||
| 322 | * @param mixed $value1 |
||
| 323 | * @param mixed $value2 |
||
| 324 | * @param bool $isBetween |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | protected function buildBetween($field, $value1, $value2, $isBetween) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Builds a NULL clause. |
||
| 337 | * |
||
| 338 | * @param string $field |
||
| 339 | * @param bool $isEqual |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | protected function buildNull($field, $isEqual) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Implodes a list of WHERE clauses. |
||
| 352 | * |
||
| 353 | * @param array $clauses |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | protected function implodeClauses(array $clauses) |
||
| 382 | } |
||
| 383 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: