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:
Complex classes like AuthItemModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthItemModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class AuthItemModel extends Model |
||
22 | { |
||
23 | /** |
||
24 | * @var string auth item name |
||
25 | */ |
||
26 | public $name; |
||
27 | |||
28 | /** |
||
29 | * @var int auth item type |
||
30 | */ |
||
31 | public $type; |
||
32 | |||
33 | /** |
||
34 | * @var string auth item description |
||
35 | */ |
||
36 | public $description; |
||
37 | |||
38 | /** |
||
39 | * @var string biz rule name |
||
40 | */ |
||
41 | public $ruleName; |
||
42 | |||
43 | /** |
||
44 | * @var null|string additional data |
||
45 | */ |
||
46 | public $data; |
||
47 | |||
48 | /** |
||
49 | * @var \yii\rbac\ManagerInterface |
||
50 | */ |
||
51 | protected $manager; |
||
52 | |||
53 | /** |
||
54 | * @var Item |
||
55 | */ |
||
56 | private $_item; |
||
57 | |||
58 | /** |
||
59 | * AuthItemModel constructor. |
||
60 | * |
||
61 | * @param Item|null $item |
||
62 | * @param array $config |
||
63 | */ |
||
64 | public function __construct($item = null, $config = []) |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | public function rules(): array |
||
97 | |||
98 | /** |
||
99 | * Validate item name |
||
100 | */ |
||
101 | public function validateName() |
||
113 | |||
114 | /** |
||
115 | * Check for rule |
||
116 | */ |
||
117 | public function checkRule() |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | View Code Duplication | public function attributeLabels(): array |
|
149 | |||
150 | /** |
||
151 | * Check if is new record. |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function getIsNewRecord(): bool |
||
159 | |||
160 | /** |
||
161 | * Find role |
||
162 | * |
||
163 | * @param string $id |
||
164 | * |
||
165 | * @return null|\self |
||
166 | */ |
||
167 | public static function find(string $id) |
||
177 | |||
178 | /** |
||
179 | * Save role to [[\yii\rbac\authManager]] |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function save(): bool |
||
215 | |||
216 | /** |
||
217 | * Add child to Item |
||
218 | * |
||
219 | * @param array $items |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | View Code Duplication | public function addChildren(array $items): bool |
|
237 | |||
238 | /** |
||
239 | * Remove child from an item |
||
240 | * |
||
241 | * @param array $items |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | View Code Duplication | public function removeChildren(array $items): bool |
|
259 | |||
260 | /** |
||
261 | * Get all available and assigned roles, permission and routes |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function getItems(): array |
||
291 | |||
292 | /** |
||
293 | * @return null|Item |
||
294 | */ |
||
295 | public function getItem() |
||
299 | |||
300 | /** |
||
301 | * Get type name |
||
302 | * |
||
303 | * @param mixed $type |
||
304 | * |
||
305 | * @return string|array |
||
306 | */ |
||
307 | public static function getTypeName($type = null) |
||
320 | } |
||
321 |
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.