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 |
||
18 | class ScreenTemplate extends \yii\db\ActiveRecord |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public static function tableName() |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | View Code Duplication | public function rules() |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function attributeLabels() |
||
56 | |||
57 | /** |
||
58 | * After save event |
||
59 | * Set screen last_modified field on changes to force screen reload. |
||
60 | * |
||
61 | * @param bool $insert is model inserted |
||
62 | * @param array $changedAttributes |
||
63 | */ |
||
64 | public function afterSave($insert, $changedAttributes) |
||
71 | |||
72 | /** |
||
73 | * @return \yii\db\ActiveQuery |
||
74 | */ |
||
75 | public function getFields() |
||
79 | |||
80 | /** |
||
81 | * @return \yii\db\ActiveQuery |
||
82 | */ |
||
83 | public function getScreens() |
||
87 | |||
88 | /** |
||
89 | * @return \yii\db\ActiveQuery |
||
90 | */ |
||
91 | public function getBackground() |
||
95 | |||
96 | public function setBackground($backgroundId) |
||
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.