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; |
||
20 | class BaseModelController extends BaseController |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * The Eloquent model instance |
||
25 | * @var \Eloquent |
||
26 | */ |
||
27 | protected $model; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | */ |
||
32 | public function __construct() |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | */ |
||
51 | protected function checkPermissions() |
||
64 | |||
65 | /** |
||
66 | * @param $data |
||
67 | */ |
||
68 | protected function handleData($data) |
||
76 | |||
77 | /** |
||
78 | * @param $data |
||
79 | * @return null |
||
80 | */ |
||
81 | 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() |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | protected function getRules() |
||
159 | |||
160 | /** |
||
161 | * @param $items |
||
162 | * @return null |
||
163 | */ |
||
164 | public static function getCurrentModelId($items) |
||
168 | |||
169 | /** |
||
170 | * @return array|mixed |
||
171 | */ |
||
172 | protected function filterInputData() |
||
189 | |||
190 | /** |
||
191 | * @param $keys |
||
192 | * @param $values |
||
193 | * @return mixed |
||
194 | */ |
||
195 | protected function except($keys, $values) |
||
205 | |||
206 | /** |
||
207 | * @param $data |
||
208 | * @return array |
||
209 | */ |
||
210 | protected function handleFiles($data) |
||
229 | |||
230 | /** |
||
231 | * @param $type |
||
232 | * @return bool |
||
233 | */ |
||
234 | protected function isFileInput($type) |
||
238 | |||
239 | /** |
||
240 | * This is used for simple upload (no plugins) |
||
241 | * |
||
242 | * @param $fieldName |
||
243 | * @param null $path |
||
244 | * @return string |
||
245 | */ |
||
246 | protected function uploadFile($fieldName, $path = null) |
||
264 | |||
265 | /** |
||
266 | * @param $modelName |
||
267 | * @param $id |
||
268 | * @param $field |
||
269 | * @return null |
||
270 | */ |
||
271 | protected function getRelatedFieldOutput($modelName, $id, $field) |
||
293 | |||
294 | /** |
||
295 | * @param $validator |
||
296 | * @param string $route |
||
297 | * @return $this |
||
298 | */ |
||
299 | protected function redirect($validator, $route, $id = null) |
||
315 | |||
316 | /** |
||
317 | * @param $data |
||
318 | * @return \Illuminate\Validation\Validator |
||
319 | */ |
||
320 | protected function validator($data) |
||
324 | |||
325 | /** |
||
326 | * @param $modelName |
||
327 | * @param null $data |
||
328 | * @param $sort |
||
329 | * @return $this |
||
330 | */ |
||
331 | protected function all($modelName, $data = null, $sort) |
||
339 | } |
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..