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 ContentType extends \yii\db\ActiveRecord |
||
17 | { |
||
18 | const BASE_CACHE_TIME = 3600; |
||
19 | const SUB_PATH = 'app\\models\\types\\'; |
||
20 | |||
21 | public $name; |
||
22 | public $description; |
||
23 | public $html; |
||
24 | public $css; |
||
25 | public $js; |
||
26 | public $appendParams; |
||
27 | public $selfUpdate; |
||
28 | public $input; |
||
29 | public $output; |
||
30 | public $usable; |
||
31 | public $exemple; |
||
32 | public $canPreview; |
||
33 | |||
34 | const KINDS = [ |
||
35 | 'NONE' => 'none', |
||
36 | 'RAW' => 'raw', |
||
37 | 'URL' => 'url', |
||
38 | 'FILE' => 'file', |
||
39 | 'TEXT' => 'text', |
||
40 | 'POS' => 'latlong', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public static function tableName() |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | View Code Duplication | public function rules() |
|
62 | |||
63 | public function contentRules() |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function attributeLabels() |
||
84 | |||
85 | public function contentLabels() |
||
89 | |||
90 | /** |
||
91 | * Get class from content type ID. |
||
92 | * |
||
93 | * @param string $typeId content type id |
||
94 | * |
||
95 | * @return string class name |
||
96 | */ |
||
97 | public static function fromType($typeId) |
||
106 | |||
107 | /** |
||
108 | * Overload default instantiate to fill attributes from specific content type. |
||
109 | * |
||
110 | * @param array $row |
||
111 | * |
||
112 | * @return array transformed row |
||
113 | */ |
||
114 | public static function instantiate($row) |
||
120 | |||
121 | /** |
||
122 | * Get raw data and transform it to content type specific needs. |
||
123 | * |
||
124 | * @param string $data |
||
125 | * |
||
126 | * @return string transformed data |
||
127 | */ |
||
128 | public function processData($data) |
||
132 | |||
133 | /** |
||
134 | * Get all filtered content types. |
||
135 | * |
||
136 | * @param bool $selfUpdate does content type manages itself |
||
137 | * @param bool $usableOnly show only usable content types |
||
138 | * |
||
139 | * @return array content types |
||
140 | */ |
||
141 | public static function getAll($selfUpdate = null, $usableOnly = true) |
||
149 | |||
150 | /** |
||
151 | * Get all filterd content types in array. |
||
152 | * |
||
153 | * @param bool $selfUpdate does content type manages itself |
||
154 | * @param bool $usableOnly show only usable content types |
||
155 | * |
||
156 | * @return array content types |
||
157 | */ |
||
158 | public static function getAllList($selfUpdate = null, $usableOnly = true) |
||
170 | |||
171 | /** |
||
172 | * Get all file based content types. |
||
173 | * |
||
174 | * @return array content types |
||
175 | */ |
||
176 | public static function getAllFileTypeIds() |
||
184 | |||
185 | /** |
||
186 | * Downloads content from URL through proxy if necessary. |
||
187 | * |
||
188 | * @param string $url |
||
189 | * |
||
190 | * @return string content |
||
191 | */ |
||
192 | public static function downloadContent($url) |
||
211 | |||
212 | /** |
||
213 | * Check cache existence. |
||
214 | * |
||
215 | * @param string $key cache key |
||
216 | * |
||
217 | * @return bool has cached data |
||
218 | */ |
||
219 | public function hasCache($key) |
||
223 | |||
224 | /** |
||
225 | * Get from cache. |
||
226 | * |
||
227 | * @param string $key cache key |
||
228 | * |
||
229 | * @return string cached data |
||
230 | */ |
||
231 | public function fromCache($key) |
||
238 | |||
239 | /** |
||
240 | * Store to cache. |
||
241 | * |
||
242 | * @param string $key cache key |
||
243 | * @param string $content cache data |
||
244 | */ |
||
245 | public function toCache($key, $content) |
||
251 | |||
252 | /** |
||
253 | * @return \yii\db\ActiveQuery |
||
254 | */ |
||
255 | public function getContents() |
||
259 | |||
260 | /** |
||
261 | * @return \yii\db\ActiveQuery |
||
262 | */ |
||
263 | public function getFieldHasContentTypes() |
||
267 | |||
268 | /** |
||
269 | * @return \yii\db\ActiveQuery |
||
270 | */ |
||
271 | public function getFields() |
||
275 | |||
276 | /** |
||
277 | * File management methods. |
||
278 | */ |
||
279 | |||
280 | /** |
||
281 | * Take a file instance and upload it to FS, also save in DB. |
||
282 | * |
||
283 | * @param \yii\web\UploadedFile|null $fileInstance |
||
284 | * |
||
285 | * @return bool|array error or json success string |
||
286 | */ |
||
287 | public function upload($fileInstance) |
||
291 | |||
292 | /** |
||
293 | * Take an url and download it, also save it in DB. |
||
294 | * |
||
295 | * @param string $url |
||
296 | * |
||
297 | * @return bool|array error or json success string |
||
298 | */ |
||
299 | public function sideload($url) |
||
303 | |||
304 | /** |
||
305 | * Custom error getter for upload/sideload temp file. |
||
306 | * |
||
307 | * @return string error |
||
308 | */ |
||
309 | public function getLoadError() |
||
313 | |||
314 | /** |
||
315 | * Transform data on beforeSave event. |
||
316 | * |
||
317 | * @param bool $insert is inserted |
||
318 | * @param string $data content data |
||
319 | * |
||
320 | * @return string transformed data |
||
321 | */ |
||
322 | public function transformDataBeforeSave($insert, $data) |
||
326 | } |
||
327 |
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.