Complex classes like MultipleBlameableTrait 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 MultipleBlameableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | trait MultipleBlameableTrait |
||
61 | { |
||
62 | |||
63 | /** |
||
64 | * @var string class name of multiple blameable class. |
||
65 | */ |
||
66 | public $multiBlamesClass = ''; |
||
67 | |||
68 | /** |
||
69 | * @var string name of multiple blameable attribute. |
||
70 | */ |
||
71 | public $multiBlamesAttribute = 'blames'; |
||
72 | |||
73 | /** |
||
74 | * @var integer the limit of blames. it should be greater than or equal 1, and |
||
75 | * less than or equal 10. |
||
76 | */ |
||
77 | public $blamesLimit = 10; |
||
78 | |||
79 | /** |
||
80 | * @var boolean determines whether blames list has been changed. |
||
81 | */ |
||
82 | public $blamesChanged = false; |
||
83 | |||
84 | /** |
||
85 | * @var string event name. |
||
86 | */ |
||
87 | public static $eventMultipleBlamesChanged = 'multipleBlamesChanged'; |
||
88 | |||
89 | /** |
||
90 | * Get the rules associated with multiple blameable attribute. |
||
91 | * @return array rules. |
||
92 | */ |
||
93 | 44 | public function getMultipleBlameableAttributeRules() |
|
100 | |||
101 | /** |
||
102 | * Add specified blame. |
||
103 | * @param {$this->multiBlamesClass}|string $blame |
||
|
|||
104 | * @return false|array |
||
105 | * @throws InvalidParamException if blame existed. |
||
106 | * @throws InvalidCallException if blame limit reached. |
||
107 | */ |
||
108 | 7 | public function addBlame($blame) |
|
109 | { |
||
110 | 7 | if (!is_string($this->multiBlamesAttribute)) { |
|
111 | return false; |
||
112 | } |
||
113 | 7 | $blameGuid = ''; |
|
114 | 7 | if (is_string($blame)) { |
|
115 | 2 | $blameGuid = $blame; |
|
116 | } |
||
117 | 7 | if ($blame instanceof $this->multiBlamesClass) { |
|
118 | 5 | $blameGuid = $blame->getGUID(); |
|
119 | } |
||
120 | 7 | $blameGuids = $this->getBlameGuids(true); |
|
121 | 7 | if (array_search($blameGuid, $blameGuids)) { |
|
122 | throw new InvalidParamException('the blame has existed.'); |
||
123 | } |
||
124 | 7 | if ($this->getBlamesCount() >= $this->blamesLimit) { |
|
125 | throw new InvalidCallException("the limit($this->blamesLimit) of blames has been reached."); |
||
126 | } |
||
127 | 7 | $blameGuids[] = $blameGuid; |
|
128 | 7 | $this->setBlameGuids($blameGuids); |
|
129 | 7 | return $this->getBlameGuids(); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Create blame. |
||
134 | * @param BaseUserModel $user who will own this blame. |
||
135 | * @param array $config blame class configuration array. |
||
136 | * @return {$this->multiBlamesClass} |
||
137 | */ |
||
138 | 9 | public static function createBlame($user, $config = []) |
|
139 | { |
||
140 | 9 | if (!($user instanceof BaseUserModel)) { |
|
141 | 2 | $message = 'the type of user instance must be the extended class of BaseUserModel.'; |
|
142 | 2 | throw new InvalidParamException($message); |
|
143 | } |
||
144 | 8 | $mbClass = static::buildNoInitModel(); |
|
145 | 8 | $mbi = $mbClass->multiBlamesClass; |
|
146 | 8 | return $user->create($mbi::className(), $config); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Add specified blame, or create it before adding if doesn't exist. |
||
151 | * But you should save the blame instance before adding it, or the operation |
||
152 | * will fail. |
||
153 | * @param {$this->multiBlamesClass}|string|array $blame |
||
154 | * It will be regarded as blame's guid if it is a string. And assign the |
||
155 | * reference parameter $blame the instance if it existed, or create one if not |
||
156 | * found. |
||
157 | * If it is {$this->multiBlamesClass} instance and existed, then will add it, or |
||
158 | * false will be given if it is not found in database. So if you want to add |
||
159 | * blame instance, you should save it before adding. |
||
160 | * If it is a array, it will be regarded as configuration array of blame. |
||
161 | * Notice! This parameter passed by reference, so it must be a variable! |
||
162 | * @param BaseUserModel $user whose blame. |
||
163 | * If null, it will take this blameable model's user. |
||
164 | * @return false|array false if blame created failed or not enable this feature. |
||
165 | * blames guid array if created and added successfully. |
||
166 | * @throws InvalidConfigException |
||
167 | * @throws InvalidParamException |
||
168 | * @see addBlame() |
||
169 | */ |
||
170 | 3 | public function addOrCreateBlame(&$blame = null, $user = null) |
|
171 | { |
||
172 | 3 | if (!is_string($this->multiBlamesClass)) { |
|
173 | throw new InvalidConfigException('$multiBlamesClass must be specified if you want to use multiple blameable features.'); |
||
174 | } |
||
175 | 3 | if (is_array($blame) || $blame === null) { |
|
176 | 2 | if ($user == null) { |
|
177 | 2 | $user = $this->host; |
|
178 | } |
||
179 | 2 | $blame = static::getOrCreateBlame($blame, $user); |
|
180 | 2 | if (!$blame->save()) { |
|
181 | return false; |
||
182 | } |
||
183 | 2 | return $this->addBlame($blame->getGUID()); |
|
184 | } |
||
185 | 1 | $blameGuid = ''; |
|
186 | 1 | if (is_string($blame)) { |
|
187 | $blameGuid = $blame; |
||
188 | } |
||
189 | 1 | if ($blame instanceof $this->multiBlamesClass) { |
|
190 | 1 | $blameGuid = $blame->getGUID(); |
|
191 | } |
||
192 | 1 | if (($mbi = static::getBlame($blameGuid)) !== null) { |
|
193 | 1 | return $this->addBlame($mbi); |
|
194 | } |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Remove specified blame. |
||
200 | * @param {$this->multiBlamesClass}|string $blame |
||
201 | * @return false|array all guids in json format. |
||
202 | */ |
||
203 | 1 | public function removeBlame($blame) |
|
204 | { |
||
205 | 1 | if (!is_string($this->multiBlamesAttribute)) { |
|
206 | return false; |
||
207 | } |
||
208 | 1 | $blameGuid = ''; |
|
209 | 1 | if (is_string($blame)) { |
|
210 | 1 | $blameGuid = $blame; |
|
211 | } |
||
212 | 1 | if ($blame instanceof $this->multiBlamesClass) { |
|
213 | 1 | $blameGuid = $blame->getGUID(); |
|
214 | } |
||
215 | 1 | $blameGuids = $this->getBlameGuids(true); |
|
216 | 1 | if (($key = array_search($blameGuid, $blameGuids)) !== false) { |
|
217 | 1 | unset($blameGuids[$key]); |
|
218 | 1 | $this->setBlameGuids($blameGuids); |
|
219 | } |
||
220 | 1 | return $this->getBlameGuids(); |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Remove all blames. |
||
225 | */ |
||
226 | 47 | public function removeAllBlames() |
|
230 | |||
231 | /** |
||
232 | * Count the blames. |
||
233 | * @return integer |
||
234 | */ |
||
235 | 7 | public function getBlamesCount() |
|
236 | { |
||
237 | 7 | return count($this->getBlameGuids(true)); |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Get the guid array of blames. it may check all guids if valid before return. |
||
242 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
243 | * @return array all guids in json format. |
||
244 | */ |
||
245 | 7 | public function getBlameGuids($checkValid = false) |
|
246 | { |
||
247 | 7 | $multiBlamesAttribute = $this->multiBlamesAttribute; |
|
248 | 7 | if ($multiBlamesAttribute === false) { |
|
249 | return []; |
||
250 | } |
||
251 | 7 | $guids = Number::divide_guid_bin($this->$multiBlamesAttribute); |
|
252 | 7 | if ($checkValid) { |
|
253 | 7 | $guids = $this->unsetInvalidBlames($guids); |
|
254 | } |
||
255 | 7 | return $guids; |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * Event triggered when blames list changed. |
||
260 | * @param MultipleBlameableEvent $event |
||
261 | */ |
||
262 | 47 | public function onBlamesChanged($event) |
|
267 | |||
268 | /** |
||
269 | * Remove invalid blame guid from provided guid array. |
||
270 | * @param array $guids guid array of blames. |
||
271 | * @return array guid array of blames unset invalid. |
||
272 | */ |
||
273 | 47 | protected function unsetInvalidBlames($guids) |
|
274 | { |
||
275 | 47 | $unchecked = $guids; |
|
276 | 47 | $multiBlamesClass = $this->multiBlamesClass; |
|
277 | 47 | $mbi = $multiBlamesClass::buildNoInitModel(); |
|
278 | 47 | foreach ($guids as $key => $guid) { |
|
279 | 7 | $blame = $multiBlamesClass::find()->where([$mbi->guidAttribute => $guid])->exists(); |
|
280 | 7 | if (!$blame) { |
|
281 | 7 | unset($guids[$key]); |
|
282 | } |
||
283 | } |
||
284 | 47 | $diff = array_diff($unchecked, $guids); |
|
285 | 47 | $eventName = static::$eventMultipleBlamesChanged; |
|
286 | 47 | $this->trigger($eventName, new MultipleBlameableEvent(['blamesChanged' => !empty($diff)])); |
|
287 | 47 | return $guids; |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * Set the guid array of blames, it may check all guids if valid. |
||
292 | * @param array $guids guid array of blames. |
||
293 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
294 | * @return false|array all guids. |
||
295 | */ |
||
296 | 47 | public function setBlameGuids($guids = [], $checkValid = true) |
|
308 | |||
309 | /** |
||
310 | * Get blame. |
||
311 | * @param string $blameGuid |
||
312 | * @return {$this->multiBlamesClass} |
||
313 | */ |
||
314 | 6 | public static function getBlame($blameGuid) |
|
315 | { |
||
316 | 6 | $self = static::buildNoInitModel(); |
|
317 | 6 | if (empty($blameGuid) || empty($self->multiBlamesClass) || !is_string($self->multiBlamesClass) || $self->multiBlamesAttribute === false) { |
|
318 | 1 | return null; |
|
319 | } |
||
320 | 5 | $mbClass = $self->multiBlamesClass; |
|
321 | 5 | return $mbClass::findOne($blameGuid); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Get all blames that owned this relation. |
||
326 | * @return array |
||
327 | */ |
||
328 | 2 | public function getOwnBlames() |
|
334 | |||
335 | /** |
||
336 | * Set blames which would own this relation. |
||
337 | * @param array $blames |
||
338 | */ |
||
339 | public function setOwnBlames($blames) |
||
344 | |||
345 | /** |
||
346 | * Check blame owned this. |
||
347 | * @param {$this->multiBlamesClass} $blame |
||
348 | * @return boolean |
||
349 | */ |
||
350 | 2 | public function IsBlameOwned($blame) |
|
351 | { |
||
352 | 2 | if (!($blame instanceof $this->multiBlamesClass) || $blame->getIsNewRecord() || !static::getBlame($blame->getGUID())) { |
|
353 | 1 | return false; |
|
354 | } |
||
355 | 1 | return array_search($blame->getGUID(), $this->getBlameGuids(true)); |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * Get or create blame. |
||
360 | * @param string|array|null $blameGuid |
||
361 | * @param BaseUserModel $user |
||
362 | * @return {$this->multiBlamesClass}|null |
||
363 | */ |
||
364 | 3 | public static function getOrCreateBlame($blameGuid, $user = null) |
|
365 | { |
||
366 | 3 | if (is_string($blameGuid)) { |
|
367 | 1 | return static::getBlame($blameGuid); |
|
368 | } |
||
369 | 3 | if (is_array($blameGuid) || $blameGuid === null) { |
|
370 | 3 | return static::createBlame($user, $blameGuid); |
|
371 | } |
||
372 | 1 | return null; |
|
373 | } |
||
374 | |||
375 | /** |
||
376 | * Get all ones to be blamed by `$blame`. |
||
377 | * @param {$this->multiBlamesClass} $blame |
||
378 | * @return array |
||
379 | */ |
||
380 | 2 | public static function getBlameds($blame) |
|
381 | { |
||
382 | 2 | if (is_string($blame)) { |
|
383 | 1 | $blame = static::getBlame($blame); |
|
384 | } |
||
385 | 2 | if (empty($blame)) { |
|
386 | 1 | return []; |
|
387 | } |
||
388 | 1 | $noInit = static::buildNoInitModel(); |
|
389 | /* @var $noInit static */ |
||
390 | 1 | return static::find()->createdBy($blame->host)->andWhere(['like', $noInit->multiBlamesAttribute, $blame->getGUID()])->all(); |
|
391 | } |
||
392 | |||
393 | /** |
||
394 | * Get all the blames of record. |
||
395 | * @param BaseUserModel $user |
||
396 | * @return array all blames. |
||
397 | */ |
||
398 | 2 | public static function getAllBlames($user) |
|
410 | |||
411 | /** |
||
412 | * Get all records which without any blames. |
||
413 | * @param BaseUserModel $user |
||
414 | * @return array all non-blameds. |
||
415 | */ |
||
416 | 1 | public static function getNonBlameds($user = null) |
|
430 | |||
431 | /** |
||
432 | * Initialize blames limit. |
||
433 | * @param ModelEvent $event |
||
434 | */ |
||
435 | 47 | public function onInitBlamesLimit($event) |
|
442 | |||
443 | /** |
||
444 | * Get blames which do not own any model. |
||
445 | * @param BaseUserModel $user |
||
446 | */ |
||
447 | 1 | public static function getEmptyBlames($user) |
|
461 | } |
||
462 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.