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 |
||
18 | class ActiveRecord extends \yii\db\ActiveRecord |
||
19 | { |
||
20 | use BeforeQueryTrait; |
||
21 | |||
22 | //Status state |
||
23 | const STATUS_UNLOCK = 0; |
||
24 | const STATUS_LOCK = 1; //Blocking records |
||
25 | |||
26 | public static $BEFORE_QUERY = ['locked' => self::STATUS_UNLOCK]; |
||
27 | |||
28 | |||
29 | // Dynamical fields for behaviors |
||
30 | /** |
||
31 | * @var string the attribute that will receive timestamp value |
||
32 | * Set this property to false if you do not want to record the creation time. |
||
33 | */ |
||
34 | public $createdAtAttribute = 'create_at'; |
||
35 | /** |
||
36 | * @var string the attribute that will receive timestamp value. |
||
37 | * Set this property to false if you do not want to record the update time. |
||
38 | */ |
||
39 | public $updatedAtAttribute = 'update_at'; |
||
40 | |||
41 | /** |
||
42 | * @var string the attribute that will receive current user ID value |
||
43 | * Set this property to false if you do not want to record the creator ID. |
||
44 | */ |
||
45 | public $createdByAttribute = 'create_by'; |
||
46 | /** |
||
47 | * @var string the attribute that will receive current user ID value |
||
48 | * Set this property to false if you do not want to record the updater ID. |
||
49 | */ |
||
50 | public $updatedByAttribute = 'update_by'; |
||
51 | |||
52 | public $lockedAttribute = 'locked'; |
||
53 | |||
54 | public $removedAttribute = 'removed'; |
||
55 | |||
56 | |||
57 | public function behaviors() |
||
115 | |||
116 | public function isNestedSet(){ |
||
119 | |||
120 | /** |
||
121 | * Duplicate entries in the table. |
||
122 | * @return $this|null |
||
123 | */ |
||
124 | public function duplicate() { |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @author Vitaly Voskobovich <[email protected]> |
||
140 | */ |
||
141 | public static function listAll($keyField = 'id', $valueField = 'name', $asArray = true) |
||
150 | } |
||
151 |
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.