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; |
||
23 | class BaseModelController extends BaseController |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * The Eloquent model instance |
||
28 | * @var \Eloquent |
||
29 | */ |
||
30 | protected $model; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | */ |
||
35 | public function __construct() |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | */ |
||
54 | protected function checkPermissions() |
||
67 | |||
68 | /** |
||
69 | * @param $data |
||
70 | */ |
||
71 | protected function handleData($data) |
||
77 | |||
78 | /** |
||
79 | * @param $data |
||
80 | * @return null |
||
81 | */ |
||
82 | protected function getFields($data) |
||
98 | |||
99 | // Prepares fields for factory |
||
100 | // protected function prepareFields($data) { |
||
101 | // $fields = []; |
||
102 | // |
||
103 | // foreach ($data as $key => $item) { |
||
104 | // foreach ($this->modelObject->columns as $column) { |
||
105 | // $fields[$key][$column] = $item->$column; |
||
106 | // } |
||
107 | // } |
||
108 | // |
||
109 | // return $fields; |
||
110 | // } |
||
111 | |||
112 | /** |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function getSort() |
||
135 | |||
136 | /** |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function getRules() |
||
151 | |||
152 | /** |
||
153 | * @param $items |
||
154 | * @return null |
||
155 | */ |
||
156 | public static function getCurrentModelId($items) |
||
160 | |||
161 | /** |
||
162 | * @return array|mixed |
||
163 | */ |
||
164 | protected function filterInputData() |
||
181 | |||
182 | /** |
||
183 | * @param $keys |
||
184 | * @param $values |
||
185 | * @return mixed |
||
186 | */ |
||
187 | protected function except($keys, $values) |
||
197 | |||
198 | /** |
||
199 | * @param $data |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function handleFiles($data) |
||
221 | |||
222 | /** |
||
223 | * @param $type |
||
224 | * @return bool |
||
225 | */ |
||
226 | protected function isFileInput($type) |
||
230 | |||
231 | /** |
||
232 | * This is used for simple upload (no plugins) |
||
233 | * |
||
234 | * @param $fieldName |
||
235 | * @param null $path |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function uploadFile($fieldName, $path = null) |
||
256 | |||
257 | /** |
||
258 | * @param $modelName |
||
259 | * @param $id |
||
260 | * @param $field |
||
261 | * @return null |
||
262 | */ |
||
263 | protected function getRelatedFieldOutput($modelName, $id, $field) |
||
285 | |||
286 | /** |
||
287 | * @param $validator |
||
288 | * @param string $route |
||
289 | * @return $this |
||
290 | */ |
||
291 | protected function redirect($validator, $route, $id = null) |
||
307 | |||
308 | /** |
||
309 | * @param $data |
||
310 | * @return \Illuminate\Validation\Validator |
||
311 | */ |
||
312 | protected function validator($data) |
||
316 | |||
317 | /** |
||
318 | * @param $modelName |
||
319 | * @param null $data |
||
320 | * @param $sort |
||
321 | * @return $this |
||
322 | */ |
||
323 | protected function all($modelName, $data = null, $sort) |
||
331 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.