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 = []) |
||
43 | { |
||
44 | $this->_item = $item; |
||
45 | if ($item !== null) { |
||
46 | $this->name = $item->name; |
||
47 | $this->type = $item->type; |
||
48 | $this->description = $item->description; |
||
49 | $this->ruleName = $item->ruleName; |
||
50 | $this->data = $item->data === null ? null : Json::encode($item->data); |
||
51 | } |
||
52 | parent::__construct($config); |
||
53 | } |
||
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() |
||
93 | { |
||
94 | $name = $this->ruleName; |
||
95 | if (!Yii::$app->getAuthManager()->getRule($name)) { |
||
96 | try { |
||
97 | $rule = Yii::createObject($name); |
||
98 | if ($rule instanceof \yii\rbac\Rule) { |
||
99 | $rule->name = $name; |
||
100 | Yii::$app->getAuthManager()->add($rule); |
||
101 | } else { |
||
102 | $this->addError('ruleName', Yii::t('rbac-admin', 'Invalid rule "{value}"', ['value' => $name])); |
||
103 | } |
||
104 | } catch (\Exception $exc) { |
||
105 | $this->addError('ruleName', Yii::t('rbac-admin', 'Rule "{value}" does not exists', ['value' => $name])); |
||
106 | } |
||
107 | } |
||
108 | } |
||
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() |
||
129 | { |
||
130 | return $this->_item === null; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Find role |
||
135 | * @param string $id |
||
136 | * @return null|\self |
||
137 | */ |
||
138 | public static function find($id) |
||
139 | { |
||
140 | $item = Yii::$app->authManager->getRole($id); |
||
141 | if ($item !== null) { |
||
142 | return new self($item); |
||
143 | } |
||
144 | |||
145 | return null; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Save role to [[\yii\rbac\authManager]] |
||
150 | * @return boolean |
||
151 | */ |
||
152 | public function save() |
||
153 | { |
||
154 | if ($this->validate()) { |
||
155 | $manager = Yii::$app->authManager; |
||
156 | if ($this->_item === null) { |
||
157 | if ($this->type == Item::TYPE_ROLE) { |
||
158 | $this->_item = $manager->createRole($this->name); |
||
159 | } else { |
||
160 | $this->_item = $manager->createPermission($this->name); |
||
161 | } |
||
162 | $isNew = true; |
||
163 | } else { |
||
164 | $isNew = false; |
||
165 | $oldName = $this->_item->name; |
||
166 | } |
||
167 | $this->_item->name = $this->name; |
||
168 | $this->_item->description = $this->description; |
||
169 | $this->_item->ruleName = $this->ruleName; |
||
170 | $this->_item->data = $this->data === null || $this->data === '' ? null : Json::decode($this->data); |
||
171 | if ($isNew) { |
||
172 | $manager->add($this->_item); |
||
173 | } else { |
||
174 | $manager->update($oldName, $this->_item); |
||
175 | } |
||
176 | Helper::invalidate(); |
||
177 | return true; |
||
178 | } else { |
||
179 | return false; |
||
180 | } |
||
181 | } |
||
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() |
||
282 | |||
283 | /** |
||
284 | * Get item |
||
285 | * @return Item |
||
286 | */ |
||
287 | public function getItem() |
||
291 | |||
292 | /** |
||
293 | * Get type name |
||
294 | * @param mixed $type |
||
295 | * @return string|array |
||
296 | */ |
||
297 | public static function getTypeName($type = null) |
||
309 | } |
||
310 |
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.