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 AuthItem 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 AuthItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class AuthItem extends Model |
||
26 | { |
||
27 | public $name; |
||
28 | public $type; |
||
29 | public $description; |
||
30 | public $ruleName; |
||
31 | public $data; |
||
32 | /** |
||
33 | * @var Item |
||
34 | */ |
||
35 | private $_item; |
||
36 | |||
37 | /** |
||
38 | * Initialize object |
||
39 | * @param Item $item |
||
40 | * @param array $config |
||
41 | */ |
||
42 | public function __construct($item = null, $config = []) |
||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | public function rules() |
||
71 | |||
72 | /** |
||
73 | * Check role is unique |
||
74 | */ |
||
75 | public function unique() |
||
88 | |||
89 | /** |
||
90 | * Check for rule |
||
91 | */ |
||
92 | public function checkRule() |
||
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | public function attributeLabels() |
||
123 | |||
124 | /** |
||
125 | * Check if is new record. |
||
126 | * @return boolean |
||
127 | */ |
||
128 | public function getIsNewRecord() |
||
132 | |||
133 | /** |
||
134 | * Find role |
||
135 | * @param string $id |
||
136 | * @return null|\self |
||
137 | */ |
||
138 | public static function find($id) |
||
147 | |||
148 | /** |
||
149 | * Save role to [[\yii\rbac\authManager]] |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public function save() |
||
182 | |||
183 | /** |
||
184 | * Adds an item as a child of another item. |
||
185 | * @param array $items |
||
186 | * @return int |
||
187 | */ |
||
188 | View Code Duplication | public function addChildren($items) |
|
211 | |||
212 | /** |
||
213 | * Remove an item as a child of another item. |
||
214 | * @param array $items |
||
215 | * @return int |
||
216 | */ |
||
217 | View Code Duplication | public function removeChildren($items) |
|
240 | |||
241 | /** |
||
242 | * Get items |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getItems() |
||
269 | |||
270 | /** |
||
271 | * Get item |
||
272 | * @return Item |
||
273 | */ |
||
274 | public function getItem() |
||
278 | |||
279 | /** |
||
280 | * Get type name |
||
281 | * @param mixed $type |
||
282 | * @return string|array |
||
283 | */ |
||
284 | public static function getTypeName($type = null) |
||
296 | } |
||
297 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.