Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilderTrait 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 QueryBuilderTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait QueryBuilderTrait |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var |
||
| 12 | */ |
||
| 13 | protected $options = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var |
||
| 17 | */ |
||
| 18 | protected $where = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var |
||
| 22 | */ |
||
| 23 | protected $properties = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | protected $isLazyCollection = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var |
||
| 32 | */ |
||
| 33 | protected $isCollection = true; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | protected $isOriginal = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Retrieve all Items. |
||
| 42 | * |
||
| 43 | * @param array $options |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | protected function all($options = []) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Retrieve single Item. |
||
| 63 | * |
||
| 64 | * @param int $id |
||
| 65 | * @param array $options |
||
| 66 | * |
||
| 67 | * @return object |
||
| 68 | */ |
||
| 69 | protected function find($id, $options = []) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Create new Item. |
||
| 84 | * |
||
| 85 | * @param array $data |
||
| 86 | * |
||
| 87 | * @return object |
||
| 88 | */ |
||
| 89 | protected function create($data) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Update Existing Item. |
||
| 104 | * |
||
| 105 | * @param int $id |
||
| 106 | * @param array $data |
||
| 107 | * |
||
| 108 | * @return object |
||
| 109 | */ |
||
| 110 | View Code Duplication | protected function update($id, $data) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Destroy Item. |
||
| 125 | * |
||
| 126 | * @param int $id |
||
| 127 | * @param array $options |
||
| 128 | * |
||
| 129 | * @return object |
||
| 130 | */ |
||
| 131 | protected function delete($id, $options = []) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Batch Update. |
||
| 146 | * |
||
| 147 | * @param array $data |
||
| 148 | * |
||
| 149 | * @return object |
||
| 150 | */ |
||
| 151 | View Code Duplication | protected function batch($data) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Retrieve data. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | View Code Duplication | protected function get() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Retrieve data. |
||
| 184 | * |
||
| 185 | * @return object |
||
| 186 | */ |
||
| 187 | protected function first() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set original. |
||
| 203 | * |
||
| 204 | * @return object $this |
||
| 205 | */ |
||
| 206 | View Code Duplication | protected function withOriginal() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set collection. |
||
| 217 | * |
||
| 218 | * @return object $this |
||
| 219 | */ |
||
| 220 | View Code Duplication | protected function withCollection() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Set lazy collection. |
||
| 231 | * |
||
| 232 | * @return object $this |
||
| 233 | */ |
||
| 234 | View Code Duplication | protected function withLazyCollection() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Set options for woocommerce request. |
||
| 245 | * |
||
| 246 | * @param array $parameters |
||
| 247 | * |
||
| 248 | * @return object $this |
||
| 249 | */ |
||
| 250 | protected function options($parameters) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Join options for woocommerce request. |
||
| 269 | * |
||
| 270 | * @param array $parameters |
||
| 271 | * |
||
| 272 | * @return object $this |
||
| 273 | */ |
||
| 274 | protected function where(...$parameters) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Set order direction. |
||
| 296 | * |
||
| 297 | * @param string $name |
||
| 298 | * @param string $direction |
||
| 299 | * |
||
| 300 | * @return object $this |
||
| 301 | */ |
||
| 302 | protected function orderBy($name, $direction = 'desc') |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Paginate results. |
||
| 312 | * |
||
| 313 | * @param int $per_page |
||
| 314 | * @param int $current_page |
||
| 315 | * @param array $options |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | protected function paginate($per_page = 10, $current_page = 1, $options = []) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Count all results. |
||
| 368 | * |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | protected function count() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Store data. |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function save() |
|
| 402 | } |
||
| 403 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: