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 |
||
26 | class AuthItem extends Model |
||
27 | { |
||
28 | public $name; |
||
29 | public $type; |
||
30 | public $description; |
||
31 | public $ruleName; |
||
32 | public $data; |
||
33 | /** |
||
34 | * @var Item |
||
35 | */ |
||
36 | private $_item; |
||
37 | |||
38 | /** |
||
39 | * Initialize object |
||
40 | * @param Item $item |
||
41 | * @param array $config |
||
42 | */ |
||
43 | public function __construct($item = null, $config = []) |
||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | public function rules() |
||
72 | |||
73 | /** |
||
74 | * Check role is unique |
||
75 | */ |
||
76 | public function checkUnique() |
||
89 | |||
90 | /** |
||
91 | * Check for rule |
||
92 | */ |
||
93 | public function checkRule() |
||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | public function attributeLabels() |
||
124 | |||
125 | /** |
||
126 | * Check if is new record. |
||
127 | * @return boolean |
||
128 | */ |
||
129 | public function getIsNewRecord() |
||
133 | |||
134 | /** |
||
135 | * Find role |
||
136 | * @param string $id |
||
137 | * @return null|\self |
||
138 | */ |
||
139 | public static function find($id) |
||
148 | |||
149 | /** |
||
150 | * Save role to [[\yii\rbac\authManager]] |
||
151 | * @return boolean |
||
152 | */ |
||
153 | public function save() |
||
183 | |||
184 | /** |
||
185 | * Adds an item as a child of another item. |
||
186 | * @param array $items |
||
187 | * @return int |
||
188 | */ |
||
189 | View Code Duplication | public function addChildren($items) |
|
212 | |||
213 | /** |
||
214 | * Remove an item as a child of another item. |
||
215 | * @param array $items |
||
216 | * @return int |
||
217 | */ |
||
218 | View Code Duplication | public function removeChildren($items) |
|
241 | |||
242 | /** |
||
243 | * Get items |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getItems() |
||
272 | |||
273 | /** |
||
274 | * Get item |
||
275 | * @return Item |
||
276 | */ |
||
277 | public function getItem() |
||
281 | |||
282 | /** |
||
283 | * Get type name |
||
284 | * @param mixed $type |
||
285 | * @return string|array |
||
286 | */ |
||
287 | public static function getTypeName($type = null) |
||
299 | } |
||
300 |
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.