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 |
||
20 | class DomainResource extends Resource |
||
21 | { |
||
22 | use ModelTrait; |
||
23 | |||
24 | public static function tableName() |
||
28 | |||
29 | const TYPE_DOMAIN_REGISTRATION = 'dregistration'; |
||
30 | const TYPE_DOMAIN_TRANSFER = 'dtransfer'; |
||
31 | const TYPE_DOMAIN_RENEWAL = 'drenewal'; |
||
32 | const TYPE_DOMAIN_DELETE_AGP = 'ddelete_agp'; |
||
33 | const TYPE_DOMAIN_RESTORE_EXPIRED = 'drestore_expired'; |
||
34 | const TYPE_DOMAIN_RESTORE_DELETED = 'drestore_deleted'; |
||
35 | |||
36 | const TYPE_PREMIUM_DNS = 'premium_dns'; |
||
37 | |||
38 | View Code Duplication | public function rules() |
|
54 | |||
55 | /** |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getTypes() |
||
59 | { |
||
60 | return [ |
||
61 | static::TYPE_DOMAIN_REGISTRATION => Yii::t('hipanel:finance:tariff', 'Registration'), |
||
62 | static::TYPE_DOMAIN_TRANSFER => Yii::t('hipanel:finance:tariff', 'Transfer'), |
||
63 | static::TYPE_DOMAIN_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'), |
||
64 | static::TYPE_DOMAIN_DELETE_AGP => Yii::t('hipanel:finance:tariff', 'Delete in AGP'), |
||
65 | static::TYPE_DOMAIN_RESTORE_EXPIRED => Yii::t('hipanel:finance:tariff', 'Restoring expired'), |
||
66 | static::TYPE_DOMAIN_RESTORE_DELETED => Yii::t('hipanel:finance:tariff', 'Restoring deleted'), |
||
67 | ]; |
||
68 | } |
||
69 | |||
70 | public function getServiceTypes() |
||
76 | |||
77 | public function isTypeCorrect() |
||
81 | } |
||
82 |
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.