Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Query |
||
| 15 | { |
||
| 16 | const DEFAULT_LIMIT = 100; |
||
| 17 | const MAX_LIMIT = 1000; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $model; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $joins; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $relationships; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $where; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $limit; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $start; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $sort; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $model model class |
||
| 56 | */ |
||
| 57 | public function __construct($model = '') |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Gets the model class associated with this query. |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getModel() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the limit for this query. |
||
| 80 | * |
||
| 81 | * @param int $limit |
||
| 82 | * |
||
| 83 | * @return self |
||
| 84 | */ |
||
| 85 | public function limit($limit) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the limit for this query. |
||
| 94 | * |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | public function getLimit() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Sets the start offset. |
||
| 104 | * |
||
| 105 | * @param int $start |
||
| 106 | * |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | public function start($start) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Gets the start offset. |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | public function getStart() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Sets the sort pattern for the query. |
||
| 128 | * |
||
| 129 | * @param array|string $sort |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | public function sort($sort) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Gets the sort parameters. |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getSort() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Sets the where parameters. |
||
| 171 | * Accepts the following forms: |
||
| 172 | * i) where(['name' => 'Bob']) |
||
| 173 | * ii) where('name', 'Bob') |
||
| 174 | * iii) where('balance', 100, '>') |
||
| 175 | * iv) where('balance > 100'). |
||
| 176 | * |
||
| 177 | * @param array|string $where |
||
| 178 | * @param mixed $value optional value |
||
| 179 | * @param string|null $condition optional condition |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function where($where, $value = null, $condition = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Gets the where parameters. |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function getWhere() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Adds a join to the query. Matches a property on this model |
||
| 217 | * to the ID of the model we are joining. |
||
| 218 | * |
||
| 219 | * @param string $model model being joined |
||
| 220 | * @param string $column name of local property |
||
| 221 | * @param string $foreignKey |
||
| 222 | * |
||
| 223 | * @return self |
||
| 224 | */ |
||
| 225 | public function join($model, $column, $foreignKey) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Gets the joins. |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function getJoins() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Marks a relationship property on the model that should be eager loaded. |
||
| 244 | * |
||
| 245 | * @param string $k local property containing the relationship |
||
| 246 | * |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | public function with($k) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the relationship properties that are going to be eager-loaded. |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getWith() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Executes the query against the model's driver. |
||
| 268 | * |
||
| 269 | * @return array results |
||
| 270 | */ |
||
| 271 | public function execute() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Creates an iterator for a search. |
||
| 315 | * |
||
| 316 | * @return Iterator |
||
| 317 | */ |
||
| 318 | public function all() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Executes the query against the model's driver and returns the first result. |
||
| 325 | * |
||
| 326 | * @param int $limit |
||
| 327 | * |
||
| 328 | * @return array|Model|null when $limit = 1, returns a single model or null, otherwise returns an array |
||
| 329 | */ |
||
| 330 | public function first($limit = 1) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Gets the number of models matching the query. |
||
| 343 | * |
||
| 344 | * @return int |
||
| 345 | */ |
||
| 346 | public function count() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @deprecated |
||
| 356 | * |
||
| 357 | * Gets the total number of records matching an optional criteria |
||
| 358 | * |
||
| 359 | * @param array $where criteria |
||
| 360 | * |
||
| 361 | * @return int |
||
| 362 | */ |
||
| 363 | public function totalRecords(array $where = []) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Updates all of the models matched by this query. |
||
| 370 | * |
||
| 371 | * @param array $params key-value update parameters |
||
| 372 | * |
||
| 373 | * @return int # of models updated |
||
| 374 | */ |
||
| 375 | public function set(array $params) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Deletes all of the models matched by this query. |
||
| 388 | * |
||
| 389 | * @todo should be optimized to be done in a single call to the data layer |
||
| 390 | * |
||
| 391 | * @return int # of models deleted |
||
| 392 | */ |
||
| 393 | public function delete() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Hydrates the eager-loaded relationships for a given set of models. |
||
| 406 | * |
||
| 407 | * @param string $modelClass |
||
| 408 | * @param array $ids |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | private function fetchRelationships($modelClass, array $ids) |
||
| 430 | } |
||
| 431 |