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 |
||
22 | class Flow extends \yii\db\ActiveRecord |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | public static function tableName() |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | View Code Duplication | public function rules() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function attributeLabels() |
||
60 | |||
61 | /** |
||
62 | * @return \yii\db\ActiveQuery |
||
63 | */ |
||
64 | public function getContents() |
||
68 | |||
69 | /** |
||
70 | * @return \yii\db\ActiveQuery |
||
71 | */ |
||
72 | public function getParent() |
||
76 | |||
77 | /** |
||
78 | * Set parent_id with flow. |
||
79 | * |
||
80 | * @param \app\models\Flow $flow parent flow |
||
81 | */ |
||
82 | public function setParent($flow) |
||
86 | |||
87 | /** |
||
88 | * @return \yii\db\ActiveQuery |
||
89 | */ |
||
90 | public function getChildren() |
||
94 | |||
95 | /** |
||
96 | * @return \yii\db\ActiveQuery |
||
97 | */ |
||
98 | public function getScreenHasFlows() |
||
102 | |||
103 | /** |
||
104 | * @return \yii\db\ActiveQuery |
||
105 | */ |
||
106 | public function getScreens() |
||
110 | |||
111 | /** |
||
112 | * @return \yii\db\ActiveQuery |
||
113 | */ |
||
114 | public function getUserHasFlows() |
||
118 | |||
119 | /** |
||
120 | * @return \yii\db\ActiveQuery |
||
121 | */ |
||
122 | public function getUsers() |
||
126 | |||
127 | /** |
||
128 | * Build a query for a specific user, allowing to see only authorized flows. |
||
129 | * |
||
130 | * @param \yii\web\User $user |
||
131 | * |
||
132 | * @return \yii\db\ActiveQuery |
||
133 | */ |
||
134 | public static function availableQuery($user) |
||
142 | |||
143 | /** |
||
144 | * Check if a specific user is allowed to see this flow. |
||
145 | * |
||
146 | * @param \yii\web\User $user |
||
147 | * |
||
148 | * @return bool can see |
||
149 | */ |
||
150 | View Code Duplication | public function canView($user) |
|
161 | } |
||
162 |
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.