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\Controllers; |
||
| 19 | class BaseModelController extends BaseController |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The Eloquent model instance |
||
| 24 | * @var \Eloquent |
||
| 25 | */ |
||
| 26 | protected $model; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | public function __construct() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | protected function checkPermissions() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param $data |
||
| 66 | */ |
||
| 67 | protected function handleData($data) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param $data |
||
| 78 | * @return null |
||
| 79 | */ |
||
| 80 | protected function getFields($data) |
||
| 106 | |||
| 107 | // Prepares fields for factory |
||
| 108 | // protected function prepareFields($data) { |
||
| 109 | // $fields = []; |
||
| 110 | // |
||
| 111 | // foreach ($data as $key => $item) { |
||
| 112 | // foreach ($this->modelObject->columns as $column) { |
||
| 113 | // $fields[$key][$column] = $item->$column; |
||
| 114 | // } |
||
| 115 | // } |
||
| 116 | // |
||
| 117 | // return $fields; |
||
| 118 | // } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | protected function getSort() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | protected function getRules() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $items |
||
| 149 | * @return null |
||
| 150 | */ |
||
| 151 | public static function getCurrentModelId($items) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * |
||
| 158 | * |
||
| 159 | * @return array|mixed |
||
| 160 | */ |
||
| 161 | protected function filterInputData() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $keys |
||
| 182 | * @param $values |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | protected function except($keys, $values) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $data |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function handleFiles($data) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $type |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | protected function isFileInput($type) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * This is used for simple upload (no plugins) |
||
| 231 | * |
||
| 232 | * @param $fieldName |
||
| 233 | * @param null $path |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | protected function uploadFile($fieldName, $path = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $modelName |
||
| 257 | * @param $id |
||
| 258 | * @param $field |
||
| 259 | * @return null |
||
| 260 | */ |
||
| 261 | protected function getRelatedFieldOutput($modelName, $id, $field) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param $validator |
||
| 286 | * @param $route |
||
| 287 | * @param null $id |
||
| 288 | * @return Redirect |
||
| 289 | */ |
||
| 290 | protected function redirect($validator, $route, $id = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param $data |
||
| 309 | * @return \Illuminate\Validation\Validator |
||
| 310 | */ |
||
| 311 | protected function validate($data) |
||
| 312 | { |
||
| 313 | return Validator::make($data, ['name' => 'required']); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param $modelName |
||
| 318 | * @param null $data |
||
| 319 | * @param $sort |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | protected function all($modelName, $data = null, $sort) |
||
| 330 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..