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 |
||
24 | class Content extends \yii\db\ActiveRecord |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public static function tableName() |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function rules() |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function attributeLabels() |
||
72 | |||
73 | /** |
||
74 | * @return \yii\db\ActiveQuery |
||
75 | */ |
||
76 | public function getFlow() |
||
80 | |||
81 | /** |
||
82 | * @return \yii\db\ActiveQuery |
||
83 | */ |
||
84 | public function getType() |
||
88 | |||
89 | /** |
||
90 | * Build a query for a specific user, allowing to see only authorized contents. |
||
91 | * |
||
92 | * @param \yii\web\User $user |
||
93 | * |
||
94 | * @return \yii\db\ActiveQuery |
||
95 | */ |
||
96 | public static function availableQuery($user) |
||
104 | |||
105 | /** |
||
106 | * Check if a specific user is allowed to see this content. |
||
107 | * |
||
108 | * @param \yii\web\User $user |
||
109 | * |
||
110 | * @return bool can see |
||
111 | */ |
||
112 | View Code Duplication | public function canView($user) |
|
123 | |||
124 | /** |
||
125 | * Get raw data and transform it to content type specific needs. |
||
126 | * |
||
127 | * @param string $data |
||
128 | * |
||
129 | * @return string transformed data |
||
130 | */ |
||
131 | public function processData($data) |
||
135 | |||
136 | /** |
||
137 | * Retrieve data for content |
||
138 | * Transforming it if necessary (mostly urls). |
||
139 | * |
||
140 | * @return string|null usable data |
||
141 | */ |
||
142 | public function getData() |
||
164 | |||
165 | public function beforeSave($insert) |
||
180 | |||
181 | /** |
||
182 | * After delete event |
||
183 | * Try to delete file if necessary. |
||
184 | */ |
||
185 | public function afterDelete() |
||
192 | |||
193 | /** |
||
194 | * Decide if content file should be deleted by checking usage in DB. |
||
195 | * |
||
196 | * @return bool deletable |
||
197 | */ |
||
198 | protected function shouldDeleteFile() |
||
210 | |||
211 | /** |
||
212 | * Get filepath from web root. |
||
213 | * |
||
214 | * @return string filepath |
||
215 | */ |
||
216 | public function getFilepath() |
||
222 | |||
223 | /** |
||
224 | * Get Yii aliased filepath. |
||
225 | * |
||
226 | * @return string filepath |
||
227 | */ |
||
228 | public function getWebFilepath() |
||
232 | |||
233 | /** |
||
234 | * Get filesystem filepath. |
||
235 | * |
||
236 | * @return string filepath |
||
237 | */ |
||
238 | public function getRealFilepath() |
||
242 | } |
||
243 |
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.