Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
23 | class DefaultController extends Controller |
||
24 | { |
||
25 | /** |
||
26 | * @return array |
||
27 | */ |
||
28 | public function behaviors() |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | * @throws NotFoundHttpException |
||
44 | */ |
||
45 | public function actionIndex() |
||
46 | { |
||
47 | $model = $this->findModel(); |
||
48 | return $this->render('index', [ |
||
49 | 'model' => $model, |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Logs in a user. |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | public function actionLogin() |
||
59 | { |
||
60 | if (!Yii::$app->user->isGuest) { |
||
61 | return $this->processGoHome(); |
||
62 | } |
||
63 | |||
64 | $model = new LoginForm(); |
||
65 | if ($model->load(Yii::$app->request->post()) && $model->login()) { |
||
66 | return $this->goBack(); |
||
67 | } else { |
||
68 | return $this->render('login', [ |
||
69 | 'model' => $model, |
||
70 | ]); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Logs out the current user. |
||
76 | * |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function actionLogout() |
||
80 | { |
||
81 | Yii::$app->user->logout(); |
||
82 | return $this->processGoHome(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Signs user up. |
||
87 | * |
||
88 | * @return mixed |
||
89 | */ |
||
90 | public function actionSignup() |
||
91 | { |
||
92 | $model = new SignupForm(); |
||
93 | if ($model->load(Yii::$app->request->post())) { |
||
94 | if ($model->signup()) { |
||
95 | return $this->processGoHome(Module::t('module', 'It remains to activate the account.')); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $this->render('signup', [ |
||
100 | 'model' => $model, |
||
101 | ]); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $token |
||
106 | * @return \yii\web\Response |
||
107 | * @throws BadRequestHttpException |
||
108 | */ |
||
109 | public function actionEmailConfirm($token = '') |
||
122 | |||
123 | /** |
||
124 | * Requests password reset. |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function actionRequestPasswordReset() |
||
143 | |||
144 | /** |
||
145 | * Resets password. |
||
146 | * |
||
147 | * @param string $token |
||
148 | * @return mixed |
||
149 | * @throws BadRequestHttpException |
||
150 | */ |
||
151 | public function actionResetPassword($token = '') |
||
165 | |||
166 | /** |
||
167 | * Finds the User model based on its primary key value. |
||
168 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
169 | * @return User the loaded model |
||
170 | * @throws NotFoundHttpException if the model cannot be found |
||
171 | */ |
||
172 | View Code Duplication | protected function findModel() |
|
183 | |||
184 | /** |
||
185 | * @param string $message |
||
186 | * @param string $type |
||
187 | * @return \yii\web\Response |
||
188 | */ |
||
189 | public function processGoHome($message = '', $type = 'success') |
||
196 | } |
||
197 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.