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; |
||
21 | class BaseModelController extends BaseController |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * The Eloquent model instance |
||
26 | * @var \Eloquent |
||
27 | */ |
||
28 | protected $model; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | */ |
||
33 | public function __construct() |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | */ |
||
52 | protected function checkPermissions() |
||
65 | |||
66 | /** |
||
67 | * @param $data |
||
68 | */ |
||
69 | protected function handleData($data) |
||
77 | |||
78 | /** |
||
79 | * @param $data |
||
80 | * @return null |
||
81 | */ |
||
82 | protected function getFields($data) |
||
108 | |||
109 | // Prepares fields for factory |
||
110 | // protected function prepareFields($data) { |
||
111 | // $fields = []; |
||
112 | // |
||
113 | // foreach ($data as $key => $item) { |
||
114 | // foreach ($this->modelObject->columns as $column) { |
||
115 | // $fields[$key][$column] = $item->$column; |
||
116 | // } |
||
117 | // } |
||
118 | // |
||
119 | // return $fields; |
||
120 | // } |
||
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | protected function getSort() |
||
126 | { |
||
127 | $sort = array(); |
||
128 | |||
129 | if (Input::has('sort_by')) { |
||
130 | $sort['column'] = Input::get('sort_by'); |
||
131 | $sort['direction'] = Input::get('direction'); |
||
132 | } else { |
||
133 | |||
134 | if ($this->modelObject->schema->hasColumn('sort_id')) { |
||
135 | $sort['column'] = 'sort_id'; |
||
136 | } else { |
||
137 | $sort['column'] = 'id'; |
||
138 | } |
||
139 | |||
140 | $sort['direction'] = 'asc'; |
||
141 | } |
||
142 | |||
143 | return $sort; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function getRules() |
||
150 | { |
||
151 | if (isset($this->options['rules'])) { |
||
152 | return $this->options['rules']; |
||
153 | } else { |
||
154 | if (isset($this->model->rules)) { |
||
155 | return $this->model->rules; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return array(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param $items |
||
164 | * @return null |
||
165 | */ |
||
166 | public static function getCurrentModelId($items) |
||
167 | { |
||
168 | return (isset($items->id)) ? $items->id : null; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return array|mixed |
||
173 | */ |
||
174 | protected function filterInputData() |
||
175 | { |
||
176 | if ($this->modelObject->hasGuarded()) { |
||
177 | $input = Input::all(); |
||
178 | $filtered_input = array(); |
||
179 | |||
180 | foreach ($input as $key => $value) { |
||
181 | if (in_array($key, $this->modelObject->columns)) { |
||
182 | $filtered_input[$key] = $value; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | return $this->except($this->modelObject->getOption('guarded'), $filtered_input); |
||
187 | } |
||
188 | |||
189 | return Input::except('id'); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param $keys |
||
194 | * @param $values |
||
195 | * @return mixed |
||
196 | */ |
||
197 | protected function except($keys, $values) |
||
198 | { |
||
199 | $keys = is_array($keys) ? $keys : func_get_args(); |
||
200 | |||
201 | $results = $values; |
||
202 | |||
203 | array_forget($results, $keys); |
||
204 | |||
205 | return $results; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param $data |
||
210 | * @return array |
||
211 | */ |
||
212 | protected function handleFiles($data) |
||
213 | { |
||
214 | $new_data = array(); |
||
215 | $fields = $this->getFields($data); |
||
216 | |||
217 | if (!is_null($fields)) { |
||
218 | foreach ($fields as $field) { |
||
219 | if ($this->isFileInput($field->type)) { |
||
220 | if (Input::hasFile($field->name)) { |
||
221 | $new_data[$field->name] = $this->uploadFile($field->name); |
||
222 | } else { |
||
223 | unset($data[$field->name]); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return array_merge($data, $new_data); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param $type |
||
234 | * @return bool |
||
235 | */ |
||
236 | protected function isFileInput($type) |
||
237 | { |
||
238 | return ($type == 'file' || $type == 'image'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * This is used for simple upload (no plugins) |
||
243 | * |
||
244 | * @param $fieldName |
||
245 | * @param null $path |
||
246 | * @return string |
||
247 | */ |
||
248 | protected function uploadFile($fieldName, $path = null) |
||
249 | { |
||
250 | if (!$path) { |
||
251 | $path = public_path() . '/uploads/'; |
||
252 | } |
||
253 | |||
254 | $file = Input::file($fieldName); |
||
255 | |||
256 | if (!file_exists($path)) { |
||
257 | File::makeDirectory($path); |
||
258 | } |
||
259 | |||
260 | $name = uniqid() . '-' . Str::slug($file->getFilename()) . '.' . $file->guessExtension(); |
||
261 | |||
262 | $file->move($path, $name); |
||
263 | |||
264 | return $name; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param $modelName |
||
269 | * @param $id |
||
270 | * @param $field |
||
271 | * @return null |
||
272 | */ |
||
273 | protected function getRelatedFieldOutput($modelName, $id, $field) |
||
295 | |||
296 | /** |
||
297 | * @param $validator |
||
298 | * @param string $route |
||
299 | * @return $this |
||
300 | */ |
||
301 | protected function redirect($validator, $route, $id = null) |
||
317 | |||
318 | /** |
||
319 | * @param $data |
||
320 | * @return \Illuminate\Validation\Validator |
||
321 | */ |
||
322 | protected function validator($data) |
||
323 | { |
||
324 | return Validator::make($data, $this->getRules()); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param $modelName |
||
329 | * @param null $data |
||
330 | * @param $sort |
||
331 | * @return $this |
||
332 | */ |
||
333 | protected function all($modelName, $data = null, $sort) |
||
341 | } |
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..