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 |
||
19 | class SiteController extends Controller |
||
20 | { |
||
21 | /** |
||
22 | * @inheritdoc |
||
23 | */ |
||
24 | public function behaviors() |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function actions() |
||
67 | |||
68 | /** |
||
69 | * Displays homepage. |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function actionIndex() |
||
77 | |||
78 | /** |
||
79 | * Logs in a user. |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | View Code Duplication | public function actionLogin() |
|
98 | |||
99 | /** |
||
100 | * Logs out the current user. |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function actionLogout() |
||
110 | |||
111 | /** |
||
112 | * Displays contact page. |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | View Code Duplication | public function actionContact() |
|
133 | |||
134 | /** |
||
135 | * Displays about page. |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public function actionAbout() |
||
143 | |||
144 | /** |
||
145 | * Signs user up. |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public function actionSignup() |
||
164 | |||
165 | /** |
||
166 | * Requests password reset. |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | View Code Duplication | public function actionRequestPasswordReset() |
|
187 | |||
188 | /** |
||
189 | * Resets password. |
||
190 | * |
||
191 | * @param string $token |
||
192 | * @return mixed |
||
193 | * @throws BadRequestHttpException |
||
194 | */ |
||
195 | public function actionResetPassword($token) |
||
213 | } |
||
214 |
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.