Complex classes like DefaultController 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 DefaultController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class DefaultController extends Controller |
||
24 | { |
||
25 | protected $jsFile; |
||
26 | |||
27 | /** |
||
28 | * @inheritdoc |
||
29 | */ |
||
30 | public function behaviors() |
||
61 | |||
62 | public function init() |
||
75 | |||
76 | /** |
||
77 | * Lists all User models. |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function actionIndex() |
||
90 | |||
91 | /** |
||
92 | * Displays a single User model. |
||
93 | * @param integer $id |
||
94 | * @return mixed |
||
95 | */ |
||
96 | public function actionView($id) |
||
105 | |||
106 | /** |
||
107 | * Creates a new User model. |
||
108 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function actionCreate() |
||
137 | |||
138 | /** |
||
139 | * Updates an existing User model. |
||
140 | * If update is successful, the browser will be redirected to the 'view' page. |
||
141 | * @param integer $id |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function actionUpdate($id) |
||
156 | |||
157 | /** |
||
158 | * @param $id |
||
159 | * @return array|\yii\web\Response |
||
160 | * @throws NotFoundHttpException |
||
161 | */ |
||
162 | public function actionStatus($id) |
||
189 | |||
190 | /** |
||
191 | * @param $id |
||
192 | * @return \yii\web\Response |
||
193 | * @throws NotFoundHttpException |
||
194 | */ |
||
195 | public function actionUpdateProfile($id) |
||
221 | |||
222 | /** |
||
223 | * @param $id |
||
224 | * @return \yii\web\Response |
||
225 | * @throws NotFoundHttpException |
||
226 | */ |
||
227 | public function actionUpdatePassword($id) |
||
237 | |||
238 | /** |
||
239 | * @param $id |
||
240 | * @return \yii\web\Response |
||
241 | * @throws NotFoundHttpException |
||
242 | */ |
||
243 | public function actionUpdateAvatar($id) |
||
244 | { |
||
245 | if ($model = $this->findModel($id)) { |
||
246 | $model->scenario = $model::SCENARIO_AVATAR_UPDATE; |
||
247 | $oldAvatar = $model->avatar; |
||
248 | if ($model->load(Yii::$app->request->post()) && ($model->scenario === $model::SCENARIO_AVATAR_UPDATE)) { |
||
249 | if ($model->isDel) { |
||
250 | $this->processRemoveAvatar($model, $oldAvatar); |
||
251 | } |
||
252 | $uploadModel = new UploadForm(); |
||
253 | if ($uploadModel->imageFile = UploadedFile::getInstance($model, 'imageFile')) |
||
254 | $uploadModel->upload($model->id); |
||
255 | } |
||
256 | } |
||
257 | return $this->redirect(['update', 'id' => $model->id, 'tab' => 'avatar']); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Remove Avatar |
||
262 | * @param \modules\users\models\backend\User $model |
||
263 | * @param string $oldAvatar |
||
264 | */ |
||
265 | public function processRemoveAvatar($model = null, $oldAvatar = '') |
||
266 | { |
||
267 | if ($model && $oldAvatar) { |
||
268 | $upload = Yii::$app->getModule('users')->uploads; |
||
269 | $path = str_replace('\\', '/', Url::to('@upload') . DIRECTORY_SEPARATOR . $upload . DIRECTORY_SEPARATOR . $model->id); |
||
270 | $avatar = $path . '/' . $oldAvatar; |
||
271 | if (file_exists($avatar)) { |
||
272 | unlink($avatar); |
||
273 | } |
||
274 | $model->avatar = null; |
||
275 | $model->save(); |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Deletes an existing User model. |
||
281 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
282 | * @param integer $id |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function actionDelete($id) |
||
306 | |||
307 | /** |
||
308 | * Finds the User model based on its primary key value. |
||
309 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
310 | * @param integer $id |
||
311 | * @return User the loaded model |
||
312 | * @throws NotFoundHttpException if the model cannot be found |
||
313 | */ |
||
314 | protected function findModel($id) |
||
322 | |||
323 | /** |
||
324 | * Login action. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function actionLogin() |
||
350 | |||
351 | /** |
||
352 | * Logout action. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function actionLogout() |
||
361 | } |
||
362 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.