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 = '=') |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Adds an OR condition. Uses same arguments as |
||
| 110 | * adding AND conditions. |
||
| 111 | * |
||
| 112 | * @return self |
||
| 113 | */ |
||
| 114 | public function addOrCondition() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Adds a between condition to the query. |
||
| 123 | * |
||
| 124 | * @param string $field |
||
| 125 | * @param mixed $a first between value |
||
| 126 | * @param mixed $b second between value |
||
| 127 | * |
||
| 128 | * @return self |
||
| 129 | */ |
||
| 130 | public function addBetweenCondition($field, $a, $b) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a not between condition to the query. |
||
| 139 | * |
||
| 140 | * @param string $field |
||
| 141 | * @param mixed $a first between value |
||
| 142 | * @param mixed $b second between value |
||
| 143 | * |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | public function addNotBetweenCondition($field, $a, $b) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Adds an exists condition to the query. |
||
| 155 | * |
||
| 156 | * @param callable $f |
||
| 157 | * |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | public function addExistsCondition(callable $f) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Adds a not exists condition to the query. |
||
| 169 | * |
||
| 170 | * @param callable $f |
||
| 171 | * |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | public function addNotExistsCondition(callable $f) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the conditions for this statement. |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function getConditions() |
||
| 190 | |||
| 191 | public function build() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Builds a parameterized and escaped SQL fragment |
||
| 214 | * for a condition that uses our own internal |
||
| 215 | * representation. |
||
| 216 | * |
||
| 217 | * A condition is represented by an array, and can be |
||
| 218 | * have one of the following forms: |
||
| 219 | * 1. ['SQL fragment'] |
||
| 220 | * 2. ['identifier', '=', 'value'] |
||
| 221 | * 3. ['BETWEEN', 'identifier', 'value', 'value', true] |
||
| 222 | * 4. ['EXISTS', function(SelectQuery $query) {}, true] |
||
| 223 | * 5. [function(SelectQuery $query) {}] |
||
| 224 | * 6. [function(SelectQuery $query) {}, '=', 'value'] |
||
| 225 | * |
||
| 226 | * @param array $cond |
||
| 227 | * |
||
| 228 | * @return string generated SQL fragment |
||
| 229 | */ |
||
| 230 | protected function buildClause(array $cond) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Builds a subquery. |
||
| 280 | * |
||
| 281 | * @param callable $f |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function buildSubquery(callable $f) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Builds an EXISTS clause. |
||
| 298 | * |
||
| 299 | * @param callable $f |
||
| 300 | * @param bool $isExists |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function buildExists(callable $f, $isExists) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Builds a BETWEEN clause. |
||
| 313 | * |
||
| 314 | * @param string $field |
||
| 315 | * @param mixed $value1 |
||
| 316 | * @param mixed $value2 |
||
| 317 | * @param bool $isBetween |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | protected function buildBetween($field, $value1, $value2, $isBetween) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Builds a NULL clause. |
||
| 330 | * |
||
| 331 | * @param string $field |
||
| 332 | * @param bool $isEqual |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | protected function buildNull($field, $isEqual) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Builds an IN clause. |
||
| 345 | * |
||
| 346 | * @param string $field |
||
| 347 | * @param array $values |
||
| 348 | * @param bool $isIn |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function buildIn($field, array $values, $isIn) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Implodes a list of WHERE clauses. |
||
| 361 | * |
||
| 362 | * @param array $clauses |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | protected function implodeClauses(array $clauses) |
||
| 388 | } |
||
| 389 |
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: