Complex classes like BaseModelController 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 BaseModelController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Mascame\Artificer\Http\Controllers; |
||
| 22 | class BaseModelController extends BaseController |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The Eloquent model instance |
||
| 27 | * @var \Eloquent |
||
| 28 | */ |
||
| 29 | protected $model; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | */ |
||
| 53 | protected function checkPermissions() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param $data |
||
| 69 | */ |
||
| 70 | protected function handleData($data) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $data |
||
| 79 | * @return null |
||
| 80 | */ |
||
| 81 | protected function getFields($data) |
||
| 110 | |||
| 111 | // Prepares fields for factory |
||
| 112 | // protected function prepareFields($data) { |
||
| 113 | // $fields = []; |
||
| 114 | // |
||
| 115 | // foreach ($data as $key => $item) { |
||
| 116 | // foreach ($this->modelObject->columns as $column) { |
||
| 117 | // $fields[$key][$column] = $item->$column; |
||
| 118 | // } |
||
| 119 | // } |
||
| 120 | // |
||
| 121 | // return $fields; |
||
| 122 | // } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function getSort() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function getRules() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $items |
||
| 166 | * @return null |
||
| 167 | */ |
||
| 168 | public static function getCurrentModelId($items) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return array|mixed |
||
| 175 | */ |
||
| 176 | protected function filterInputData() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $keys |
||
| 196 | * @param $values |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | protected function except($keys, $values) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $data |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function handleFiles($data) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param $type |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | protected function isFileInput($type) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * This is used for simple upload (no plugins) |
||
| 245 | * |
||
| 246 | * @param $fieldName |
||
| 247 | * @param null $path |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | protected function uploadFile($fieldName, $path = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param $modelName |
||
| 271 | * @param $id |
||
| 272 | * @param $field |
||
| 273 | * @return null |
||
| 274 | */ |
||
| 275 | protected function getRelatedFieldOutput($modelName, $id, $field) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $validator |
||
| 300 | * @param string $route |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | protected function redirect($validator, $route, $id = null) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param $data |
||
| 322 | * @return \Illuminate\Validation\Validator |
||
| 323 | */ |
||
| 324 | protected function validator($data) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $modelName |
||
| 331 | * @param null $data |
||
| 332 | * @param $sort |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | protected function all($modelName, $data = null, $sort) |
||
| 343 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.