Complex classes like Box 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 Box, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Box extends Controller |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Box model class instance |
||
| 27 | * @var \gplcart\modules\packer\models\Box $box |
||
| 28 | */ |
||
| 29 | protected $box; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Packer model class instance |
||
| 33 | * @var \gplcart\modules\packer\models\Packer $packer |
||
| 34 | */ |
||
| 35 | protected $packer; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Product model class instance |
||
| 39 | * @var \gplcart\core\models\Product $product |
||
| 40 | */ |
||
| 41 | protected $product; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Convertor model class instance |
||
| 45 | * @var \gplcart\core\models\Convertor $convertor |
||
| 46 | */ |
||
| 47 | protected $convertor; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Pager limit |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $data_limit; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The current updating box |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $data_box = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Box constructor. |
||
| 63 | * @param BoxModel $box |
||
| 64 | * @param PackerModel $packer |
||
| 65 | * @param ProductModel $product |
||
| 66 | * @param ConvertorModel $convertor |
||
| 67 | */ |
||
| 68 | public function __construct(BoxModel $box, PackerModel $packer, ProductModel $product, ConvertorModel $convertor) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Route callback |
||
| 80 | * Displays the box overview page |
||
| 81 | */ |
||
| 82 | public function listBox() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Applies an action to the selected boxes |
||
| 96 | */ |
||
| 97 | protected function actionListBox() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets filter parameters on the box overview page |
||
| 117 | */ |
||
| 118 | protected function setFilterListBox() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sets pager on the box overview page |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | protected function setPagerListBox() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns an array of boxes |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function getListBox() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Sets title on the box overview page |
||
| 161 | */ |
||
| 162 | protected function setTitleListBox() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Sets breadcrumbs on the box overview page |
||
| 169 | */ |
||
| 170 | protected function setBreadcrumbListBox() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Render and output the box overview page |
||
| 182 | */ |
||
| 183 | protected function outputListBox() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Page callback |
||
| 190 | * Displays the fit items page |
||
| 191 | * @param $box_id |
||
| 192 | */ |
||
| 193 | public function fitBox($box_id) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Prepare and set variables for template |
||
| 208 | */ |
||
| 209 | protected function setDataFitBox() |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Sets titles on the fit box page |
||
| 221 | */ |
||
| 222 | protected function setTitleFitBox() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Handles submitted data while trying to fit products into a box |
||
| 230 | */ |
||
| 231 | protected function submitFitBox() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Validates submitted data while trying to fit products into a box |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | protected function validateFitBox() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Validates an array of submitted product IDs |
||
| 254 | */ |
||
| 255 | protected function validateProductsFitBox() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Tries to fit the submitted products into a box |
||
| 305 | */ |
||
| 306 | protected function doFitBox() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Render and output the box fit items page |
||
| 327 | */ |
||
| 328 | protected function outputFitBox() |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Page callback |
||
| 336 | * Displays the edit box page |
||
| 337 | * @param null|int $box_id |
||
| 338 | */ |
||
| 339 | public function editBox($box_id = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Sets the box data |
||
| 356 | * @param $box_id |
||
| 357 | */ |
||
| 358 | protected function setBox($box_id) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Sets titles on the edit box page |
||
| 370 | */ |
||
| 371 | protected function setTitleEditBox() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Sets breadcrumbs on the box edit page |
||
| 384 | */ |
||
| 385 | protected function setBreadcrumbEditBox() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Handles a submitted box |
||
| 404 | */ |
||
| 405 | protected function submitEditBox() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Validates a submitted box data |
||
| 420 | */ |
||
| 421 | protected function validateEditBox() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns an array of box dimension field names keyed by DB fields |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | protected function getDimensionFieldsBox() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Updates a submitted box |
||
| 472 | */ |
||
| 473 | protected function updateBox() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Adds a new box |
||
| 486 | */ |
||
| 487 | protected function addBox() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Delete a submitted box |
||
| 500 | */ |
||
| 501 | protected function deleteBox() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Render and output the box edit page |
||
| 514 | */ |
||
| 515 | protected function outputEditBox() |
||
| 519 | |||
| 520 | } |
||
| 521 |