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 |
||
21 | class Feedback extends ActiveRecord implements ModelInterface |
||
22 | { |
||
23 | const SCENARIO_CONTACT = 'contact'; |
||
24 | const SCENARIO_FEEDBACK = 'feedback'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | public $verifyCode; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public static function tableName() |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function rules() |
||
110 | |||
111 | /** |
||
112 | * Scenarios. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function scenarios() |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function attributeLabels() |
||
140 | |||
141 | /** |
||
142 | * Returns id of the model. |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | public function getId() |
||
150 | |||
151 | /** |
||
152 | * Set read status to "1" after view record. |
||
153 | * |
||
154 | * @param int $id |
||
155 | */ |
||
156 | public static function fixReadStatus(int $id): void |
||
164 | |||
165 | /** |
||
166 | * Sends an email to the specified email address using the information collected by this model. |
||
167 | * |
||
168 | * @param string $email the target email address |
||
169 | * |
||
170 | * @return bool whether the model passes validation |
||
171 | */ |
||
172 | View Code Duplication | public function contact($email) |
|
187 | } |
||
188 |
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.