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 |
||
| 15 | class ContentType extends \yii\db\ActiveRecord |
||
| 16 | { |
||
| 17 | const BASE_CACHE_TIME = 3600; |
||
| 18 | const SUB_PATH = 'app\\models\\types\\'; |
||
| 19 | |||
| 20 | public $name; |
||
| 21 | public $description; |
||
| 22 | public $html; |
||
| 23 | public $css; |
||
| 24 | public $js; |
||
| 25 | public $appendParams; |
||
| 26 | public $selfUpdate; |
||
| 27 | public $input; |
||
| 28 | public $output; |
||
| 29 | public $usable; |
||
| 30 | public $preview; |
||
| 31 | public $canPreview; |
||
| 32 | |||
| 33 | const KINDS = [ |
||
| 34 | 'NONE' => 'none', |
||
| 35 | 'RAW' => 'raw', |
||
| 36 | 'URL' => 'url', |
||
| 37 | 'FILE' => 'file', |
||
| 38 | 'TEXT' => 'text', |
||
| 39 | 'POS' => 'latlong', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | public static function tableName() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function rules() |
|
| 60 | |||
| 61 | public function contentRules() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function attributeLabels() |
||
| 80 | |||
| 81 | public function contentLabels() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get class from content type ID. |
||
| 88 | * |
||
| 89 | * @param string $typeId content type id |
||
| 90 | * |
||
| 91 | * @return string class name |
||
| 92 | */ |
||
| 93 | public static function fromType($typeId) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Overload default instantiate to fill attributes from specific content type. |
||
| 105 | * |
||
| 106 | * @param array $row |
||
| 107 | * |
||
| 108 | * @return array transformed row |
||
| 109 | */ |
||
| 110 | public static function instantiate($row) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get all filtered content types. |
||
| 119 | * |
||
| 120 | * @param bool $selfUpdate does content type manages itself |
||
| 121 | * @param bool $usableOnly show only usable content types |
||
| 122 | * |
||
| 123 | * @return array content types |
||
| 124 | */ |
||
| 125 | public static function getAll($selfUpdate = null, $usableOnly = true) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get all filterd content types in array. |
||
| 136 | * |
||
| 137 | * @param bool $selfUpdate does content type manages itself |
||
| 138 | * @param bool $usableOnly show only usable content types |
||
| 139 | * |
||
| 140 | * @return array content types |
||
| 141 | */ |
||
| 142 | public static function getAllList($selfUpdate = null, $usableOnly = true) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get all file based content types. |
||
| 157 | * |
||
| 158 | * @return array content types |
||
| 159 | */ |
||
| 160 | public static function getAllFileTypeIds() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Downloads content from URL through proxy if necessary. |
||
| 171 | * |
||
| 172 | * @param string $url |
||
| 173 | * |
||
| 174 | * @return string content |
||
| 175 | */ |
||
| 176 | public static function downloadContent($url) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check cache existence. |
||
| 194 | * |
||
| 195 | * @param string $key cache key |
||
| 196 | * |
||
| 197 | * @return bool has cached data |
||
| 198 | */ |
||
| 199 | public function hasCache($key) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get from cache. |
||
| 206 | * |
||
| 207 | * @param string $key cache key |
||
| 208 | * |
||
| 209 | * @return string cached data |
||
| 210 | */ |
||
| 211 | public function fromCache($key) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Store to cache. |
||
| 222 | * |
||
| 223 | * @param string $key cache key |
||
| 224 | * @param string $content cache data |
||
| 225 | */ |
||
| 226 | public function toCache($key, $content) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return \yii\db\ActiveQuery |
||
| 235 | */ |
||
| 236 | public function getContents() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return \yii\db\ActiveQuery |
||
| 243 | */ |
||
| 244 | public function getFieldHasContentTypes() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return \yii\db\ActiveQuery |
||
| 251 | */ |
||
| 252 | public function getFields() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get translated content type name. |
||
| 259 | * |
||
| 260 | * @return string name |
||
| 261 | */ |
||
| 262 | public function getTName() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get translated content type description. |
||
| 269 | * |
||
| 270 | * @return string description |
||
| 271 | */ |
||
| 272 | public function getTDescription() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * File management methods. |
||
| 279 | */ |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Take a file instance and upload it to FS, also save in DB. |
||
| 283 | * |
||
| 284 | * @param \FileInstance $fileInstance |
||
| 285 | * |
||
| 286 | * @return bool success |
||
| 287 | */ |
||
| 288 | public function upload() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Take an url and download it, also save it in DB. |
||
| 295 | * |
||
| 296 | * @param string $url |
||
| 297 | * |
||
| 298 | * @return bool|string[] error or json success string |
||
| 299 | */ |
||
| 300 | public function sideload() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Custom error getter for upload/sideload temp file. |
||
| 307 | * |
||
| 308 | * @return string error |
||
| 309 | */ |
||
| 310 | public function getLoadError() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Before save event |
||
| 317 | * Handles file movement from tmp directory to proper media storage |
||
| 318 | * Makes sure there is no overwrite by appending to filename. |
||
| 319 | * |
||
| 320 | * @param bool $insert is inserted |
||
| 321 | * |
||
| 322 | * @return bool success |
||
| 323 | */ |
||
| 324 | public function beforeSaveContent() |
||
| 328 | } |
||
| 329 |
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.