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:
Complex classes like ThemeController 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 ThemeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ThemeController extends Controller |
||
28 | { |
||
29 | /** |
||
30 | * @var string Path to theme directory. |
||
31 | */ |
||
32 | private $_dir; |
||
33 | |||
34 | /** |
||
35 | * @var string Path to temporary directory of theme. |
||
36 | */ |
||
37 | private $_tmp; |
||
38 | |||
39 | /** |
||
40 | * @var string Path to thumbnail directory of theme. |
||
41 | */ |
||
42 | private $_thumbDir; |
||
43 | |||
44 | /** |
||
45 | * @var string Base url of theme thumbnail. |
||
46 | */ |
||
47 | private $_thumbBaseUrl; |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | View Code Duplication | public function behaviors() |
|
74 | |||
75 | /** |
||
76 | * Scan theme directory to get list of all available themes. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function actionIndex() |
||
106 | |||
107 | /** |
||
108 | * Register new theme. |
||
109 | * Theme zip uploaded to temporary directory and extracted there. |
||
110 | * Check the theme directory and move the first directory of the extracted theme. |
||
111 | * If registration is successful, the browser will be redirected to the 'index' page. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function actionUpload() |
||
189 | |||
190 | /** |
||
191 | * Show theme detail based on theme dir then render theme detail view. |
||
192 | * |
||
193 | * @param string $theme |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function actionDetail($theme) |
||
204 | |||
205 | /** |
||
206 | * Install selected theme and run install and uninstall action based on config. |
||
207 | * |
||
208 | * @param string $theme |
||
209 | * |
||
210 | * @return \yii\web\Response |
||
211 | */ |
||
212 | public function actionInstall($theme) |
||
234 | |||
235 | /** |
||
236 | * Delete and existing theme and run action based on config. |
||
237 | * |
||
238 | * @param string $theme |
||
239 | * |
||
240 | * @return \yii\web\Response |
||
241 | */ |
||
242 | public function actionDelete($theme) |
||
262 | |||
263 | /** |
||
264 | * Detail selected theme via AJAX then show it on modal. |
||
265 | * |
||
266 | * @param string $theme |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | public function actionAjaxDetail($theme) |
||
277 | |||
278 | /** |
||
279 | * @inheritdoc |
||
280 | */ |
||
281 | public function beforeAction($action) |
||
294 | |||
295 | /** |
||
296 | * Get theme config based on theme directory; |
||
297 | * |
||
298 | * @param $theme |
||
299 | * @return array|mixed |
||
300 | */ |
||
301 | protected function getConfig($theme) |
||
321 | } |
||
322 |
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.