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 |
||
| 7 | trait QueryBuilderTrait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var |
||
| 11 | */ |
||
| 12 | protected $options = []; |
||
| 13 | /** |
||
| 14 | * @var |
||
| 15 | */ |
||
| 16 | protected $where = []; |
||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | protected $properties = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Retrieve all Items. |
||
| 24 | * |
||
| 25 | * @param array $options |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | protected function all($options = []) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Retrieve single Item. |
||
| 36 | * |
||
| 37 | * @param int $id |
||
| 38 | * @param array $options |
||
| 39 | * |
||
| 40 | * @return object |
||
| 41 | */ |
||
| 42 | protected function find($id, $options = []) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create new Item. |
||
| 49 | * |
||
| 50 | * @param array $data |
||
| 51 | * |
||
| 52 | * @return object |
||
| 53 | */ |
||
| 54 | protected function create($data) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Update Existing Item. |
||
| 61 | * |
||
| 62 | * @param int $id |
||
| 63 | * @param array $data |
||
| 64 | * |
||
| 65 | * @return object |
||
| 66 | */ |
||
| 67 | protected function update($id, $data) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Destroy Item. |
||
| 74 | * |
||
| 75 | * @param int $id |
||
| 76 | * @param array $options |
||
| 77 | * |
||
| 78 | * @return object |
||
| 79 | */ |
||
| 80 | protected function delete($id, $options = []) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Batch Update. |
||
| 87 | * |
||
| 88 | * @param array $data |
||
| 89 | * |
||
| 90 | * @return object |
||
| 91 | */ |
||
| 92 | protected function batch($data) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Retrieve data. |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | protected function get() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Retrieve data. |
||
| 114 | * |
||
| 115 | * @return object |
||
| 116 | */ |
||
| 117 | protected function first() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set options for woocommerce request. |
||
| 124 | * |
||
| 125 | * @param array $parameters |
||
| 126 | * |
||
| 127 | * @return object $this |
||
| 128 | */ |
||
| 129 | protected function options($parameters) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Join options for woocommerce request. |
||
| 148 | * |
||
| 149 | * @param array $parameters |
||
| 150 | * |
||
| 151 | * @return object $this |
||
| 152 | */ |
||
| 153 | protected function where(...$parameters) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set order direction. |
||
| 216 | * |
||
| 217 | * @param string $name |
||
| 218 | * @param string $direction |
||
| 219 | * |
||
| 220 | * @return object $this |
||
| 221 | */ |
||
| 222 | protected function orderBy($name, $direction = 'desc') |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Paginate results. |
||
| 232 | * |
||
| 233 | * @param int $per_page |
||
| 234 | * @param int $current_page |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function paginate($per_page, $current_page = 1) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Count all results. |
||
| 274 | * |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | protected function count() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Store data. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function save() |
||
| 300 | } |
||
| 301 |
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: