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 |
||
16 | class Move extends BaseAction |
||
17 | { |
||
18 | const UP = 1; |
||
19 | const DOWN = 2; |
||
20 | |||
21 | /** |
||
22 | * Attribute in model |
||
23 | * @var string |
||
24 | */ |
||
25 | public $positionAttribute = 'position'; |
||
26 | |||
27 | /** |
||
28 | * Direction to move |
||
29 | * @var integer |
||
30 | */ |
||
31 | public $direction; |
||
32 | |||
33 | /** |
||
34 | * Move model |
||
35 | * @param mixed $id ID of the model to be moved |
||
36 | * @return mixed |
||
37 | */ |
||
38 | 6 | public function run($id) |
|
46 | |||
47 | /** |
||
48 | * Move model to down |
||
49 | * @param mixed $id ID of the model to be moved down |
||
50 | * @return mixed |
||
51 | * @throws \yii\web\NotFoundHttpException |
||
52 | */ |
||
53 | 3 | View Code Duplication | protected function moveDown($id) |
73 | |||
74 | /** |
||
75 | * Move model to up |
||
76 | * @param mixed $id ID of the model to be moved up |
||
77 | * @return mixed |
||
78 | * @throws \yii\web\NotFoundHttpException |
||
79 | */ |
||
80 | 3 | View Code Duplication | protected function moveUp($id) |
100 | } |
||
101 |
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.