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 |
||
19 | View Code Duplication | class Permission extends Rbac implements ModelInterface |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * Permission object. |
||
23 | * |
||
24 | * @var BasePermission |
||
25 | */ |
||
26 | public $permission; |
||
27 | |||
28 | /** |
||
29 | * Child permissions. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public $permissions = []; |
||
34 | |||
35 | /** |
||
36 | * Validate rules. |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function rules() |
||
41 | { |
||
42 | return ArrayHelper::merge( |
||
43 | parent::rules(), |
||
44 | [ |
||
45 | [ |
||
46 | [ |
||
47 | 'permissions', |
||
48 | ], |
||
49 | 'safe', |
||
50 | ], |
||
51 | ] |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | public function attributeLabels() |
||
66 | |||
67 | /** |
||
68 | * Get current child permissions assigned to current permission. |
||
69 | * |
||
70 | * @return \yii\rbac\Permission[] |
||
71 | */ |
||
72 | protected function getCurrentChildren(): array |
||
78 | |||
79 | /** |
||
80 | * Get new child items. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | protected function getNewChildren(): array |
||
88 | |||
89 | /** |
||
90 | * Create new Permission. |
||
91 | * |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return Item |
||
95 | */ |
||
96 | protected function createItem(string $name): Item |
||
100 | |||
101 | /** |
||
102 | * Create self child permission. |
||
103 | * |
||
104 | * @param string $name |
||
105 | * |
||
106 | * @return Item |
||
107 | */ |
||
108 | protected function createChild(string $name): Item |
||
112 | |||
113 | /** |
||
114 | * Returns old name in current object, which is set before new value. |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | protected function getOldName() |
||
122 | |||
123 | /** |
||
124 | * Returns Permission object. |
||
125 | * |
||
126 | * @param string $key |
||
127 | * |
||
128 | * @return Item|null |
||
129 | */ |
||
130 | protected function getItem(string $key) |
||
134 | |||
135 | /** |
||
136 | * Set permission field for child instance of Rbac - Permission. |
||
137 | * |
||
138 | * @param Rbac $instance |
||
139 | * @param Item $item |
||
140 | * |
||
141 | * @return Rbac |
||
142 | */ |
||
143 | protected function setItemForInstance(Rbac $instance, Item $item): Rbac |
||
148 | |||
149 | /** |
||
150 | * Set children permissions for instance of Role. |
||
151 | * |
||
152 | * @param Rbac $instance |
||
153 | * |
||
154 | * @return Rbac |
||
155 | */ |
||
156 | protected function setChildrenForInstance(Rbac $instance): Rbac |
||
161 | } |
||
162 |
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.