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) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritDoc} |
||
| 56 | */ |
||
| 57 | public function orWhereTpl(/*# string */ $template, $col) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritDoc} |
||
| 65 | */ |
||
| 66 | public function whereRaw(/*# string */ $rawString) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritDoc} |
||
| 74 | */ |
||
| 75 | public function orWhereRaw(/*# string */ $rawString) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritDoc} |
||
| 83 | */ |
||
| 84 | public function andWhere( |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | public function orWhere( |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | public function whereNot( |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritDoc} |
||
| 116 | */ |
||
| 117 | public function orWhereNot( |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritDoc} |
||
| 127 | */ |
||
| 128 | public function whereIn(/*# string */ $col, $value) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritDoc} |
||
| 135 | */ |
||
| 136 | public function orWhereIn(/*# string */ $col, $value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritDoc} |
||
| 143 | */ |
||
| 144 | public function whereNotIn(/*# string */ $col, $value) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritDoc} |
||
| 151 | */ |
||
| 152 | public function orWhereNotIn(/*# string */ $col, $value) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritDoc} |
||
| 159 | */ |
||
| 160 | public function whereBetween(/*# string */ $col, $value1, $value2) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritDoc} |
||
| 168 | */ |
||
| 169 | public function orWhereBetween(/*# string */ $col, $value1, $value2) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritDoc} |
||
| 177 | */ |
||
| 178 | public function whereNotBetween(/*# string */ $col, $value1, $value2) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritDoc} |
||
| 186 | */ |
||
| 187 | public function orWhereNotBetween(/*# string */ $col, $value1, $value2) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritDoc} |
||
| 195 | */ |
||
| 196 | public function whereNull(/*# string */ $col) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritDoc} |
||
| 203 | */ |
||
| 204 | public function orWhereNull(/*# string */ $col) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritDoc} |
||
| 211 | */ |
||
| 212 | public function whereNotNull(/*# string */ $col) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | */ |
||
| 220 | public function orWhereNotNull(/*# string */ $col) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * WHERE EXISTS |
||
| 227 | * |
||
| 228 | * ```php |
||
| 229 | * // WHERE EXISTS (SELECT `user_id` FROM `users`) |
||
| 230 | * ->whereExists($users->select('user_id')) |
||
| 231 | * ``` |
||
| 232 | * |
||
| 233 | * @param SelectStatementInterface $sel |
||
| 234 | * @return $this |
||
| 235 | * @see WhereInterface::where() |
||
| 236 | * @access public |
||
| 237 | * @api |
||
| 238 | */ |
||
| 239 | public function whereExists(SelectStatementInterface $sel) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritDoc} |
||
| 246 | */ |
||
| 247 | public function orWhereExists(SelectStatementInterface $sel) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritDoc} |
||
| 254 | */ |
||
| 255 | public function whereNotExists(SelectStatementInterface $sel) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritDoc} |
||
| 262 | */ |
||
| 263 | public function orWhereNotExists(SelectStatementInterface $sel) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Real where |
||
| 270 | * |
||
| 271 | * @param string|string[]|Template $col col or cols |
||
| 272 | * @param mixed $operator |
||
| 273 | * @param mixed $value |
||
| 274 | * @param string $logicAnd 'AND' |
||
| 275 | * @param string $whereNot 'WHERE NOT' |
||
| 276 | * @param bool $rawMode |
||
| 277 | * @param string $clause 'where' or 'having' |
||
| 278 | * @return $this |
||
| 279 | * @access protected |
||
| 280 | */ |
||
| 281 | protected function realWhere( |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Fix operator and value |
||
| 303 | * |
||
| 304 | * @param mixed $operator |
||
| 305 | * @param mixed $value |
||
| 306 | * @param bool $rawMode |
||
| 307 | * @access protected |
||
| 308 | */ |
||
| 309 | protected function fixOperatorValue(&$operator, &$value, &$rawMode) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param array $cols |
||
| 322 | * @param string $logicAnd |
||
| 323 | * @param string $whereNot |
||
| 324 | * @param bool $rawMode |
||
| 325 | * @access protected |
||
| 326 | */ |
||
| 327 | protected function multipleWhere( |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Build WHERE |
||
| 346 | * |
||
| 347 | * @param string $clause 'where|having' |
||
| 348 | * @return array |
||
| 349 | * @access protected |
||
| 350 | */ |
||
| 351 | 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 |