Complex classes like WhereTrait 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 WhereTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | trait WhereTrait |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * {@inheritDoc} |
||
| 36 | */ |
||
| 37 | public function where( |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | public function whereTpl(/*# string */ $template, $col) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritDoc} |
||
| 57 | */ |
||
| 58 | public function orWhereTpl(/*# string */ $template, $col) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritDoc} |
||
| 67 | */ |
||
| 68 | public function whereRaw(/*# string */ $rawString) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritDoc} |
||
| 76 | */ |
||
| 77 | public function orWhereRaw(/*# string */ $rawString) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | */ |
||
| 86 | public function andWhere( |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritDoc} |
||
| 96 | */ |
||
| 97 | public function orWhere( |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | public function whereNot( |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritDoc} |
||
| 118 | */ |
||
| 119 | public function orWhereNot( |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | public function whereIn(/*# string */ $col, $value) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | public function orWhereIn(/*# string */ $col, $value) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritDoc} |
||
| 145 | */ |
||
| 146 | public function whereNotIn(/*# string */ $col, $value) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | */ |
||
| 154 | public function orWhereNotIn(/*# string */ $col, $value) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritDoc} |
||
| 161 | */ |
||
| 162 | public function whereBetween(/*# string */ $col, $value1, $value2) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritDoc} |
||
| 170 | */ |
||
| 171 | public function orWhereBetween(/*# string */ $col, $value1, $value2) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritDoc} |
||
| 179 | */ |
||
| 180 | public function whereNotBetween(/*# string */ $col, $value1, $value2) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritDoc} |
||
| 188 | */ |
||
| 189 | public function orWhereNotBetween(/*# string */ $col, $value1, $value2) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritDoc} |
||
| 197 | */ |
||
| 198 | public function whereNull(/*# string */ $col) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritDoc} |
||
| 205 | */ |
||
| 206 | public function orWhereNull(/*# string */ $col) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritDoc} |
||
| 213 | */ |
||
| 214 | public function whereNotNull(/*# string */ $col) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritDoc} |
||
| 221 | */ |
||
| 222 | public function orWhereNotNull(/*# string */ $col) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * WHERE EXISTS |
||
| 229 | * |
||
| 230 | * ```php |
||
| 231 | * // WHERE EXISTS (SELECT `user_id` FROM `users`) |
||
| 232 | * ->whereExists($users->select('user_id')) |
||
| 233 | * ``` |
||
| 234 | * |
||
| 235 | * @param SelectStatementInterface $sel |
||
| 236 | * @return $this |
||
| 237 | * @see WhereInterface::where() |
||
| 238 | * @access public |
||
| 239 | * @api |
||
| 240 | */ |
||
| 241 | public function whereExists(SelectStatementInterface $sel) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritDoc} |
||
| 248 | */ |
||
| 249 | public function orWhereExists(SelectStatementInterface $sel) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritDoc} |
||
| 256 | */ |
||
| 257 | public function whereNotExists(SelectStatementInterface $sel) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritDoc} |
||
| 264 | */ |
||
| 265 | public function orWhereNotExists(SelectStatementInterface $sel) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Real where |
||
| 272 | * |
||
| 273 | * @param string|string[]|Template $col col or cols |
||
| 274 | * @param mixed $operator |
||
| 275 | * @param mixed $value |
||
| 276 | * @param bool $logicAnd 'AND' |
||
| 277 | * @param bool $whereNot 'WHERE NOT' |
||
| 278 | * @param bool $rawMode |
||
| 279 | * @param string $clause 'where' or 'having' |
||
| 280 | * @return $this |
||
| 281 | * @access protected |
||
| 282 | */ |
||
| 283 | protected function realWhere( |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Fix operator and value |
||
| 305 | * |
||
| 306 | * @param mixed $operator |
||
| 307 | * @param mixed $value |
||
| 308 | * @param bool $rawMode |
||
| 309 | * @access protected |
||
| 310 | */ |
||
| 311 | protected function fixOperatorValue(&$operator, &$value, &$rawMode) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $cols |
||
| 324 | * @param bool $logicAnd |
||
| 325 | * @param bool $whereNot |
||
| 326 | * @param bool $rawMode |
||
| 327 | * @access protected |
||
| 328 | */ |
||
| 329 | protected function multipleWhere( |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Build WHERE |
||
| 348 | * |
||
| 349 | * @param string $clause 'where|having' |
||
| 350 | * @return array |
||
| 351 | * @access protected |
||
| 352 | */ |
||
| 353 | protected function buildWhere( |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Build 'col = val' part |
||
| 374 | * |
||
| 375 | * @param array $cls |
||
| 376 | * @param array $where |
||
| 377 | * @param array $settings |
||
| 378 | * @return string |
||
| 379 | * @access protected |
||
| 380 | */ |
||
| 381 | protected function buildWhereClause(array $cls, array $where, array $settings) |
||
| 402 | |||
| 403 | abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
||
| 413 | } |
||
| 414 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.