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 |
||
61 | trait MultipleBlameableTrait |
||
62 | { |
||
63 | |||
64 | /** |
||
65 | * @var string class name of multiple blameable class. |
||
66 | */ |
||
67 | public $multiBlamesClass = ''; |
||
68 | |||
69 | /** |
||
70 | * @var string name of multiple blameable attribute. |
||
71 | */ |
||
72 | public $multiBlamesAttribute = 'blames'; |
||
73 | |||
74 | /** |
||
75 | * @var integer the limit of blames. it should be greater than or equal 1, and |
||
76 | * less than or equal 10. |
||
77 | */ |
||
78 | public $blamesLimit = 10; |
||
79 | |||
80 | /** |
||
81 | * @var boolean determines whether blames list has been changed. |
||
82 | */ |
||
83 | public $blamesChanged = false; |
||
84 | |||
85 | /** |
||
86 | * @var string event name. |
||
87 | */ |
||
88 | public static $eventMultipleBlamesChanged = 'multipleBlamesChanged'; |
||
89 | |||
90 | /** |
||
91 | * Get the rules associated with multiple blameable attribute. |
||
92 | * @return array rules. |
||
93 | */ |
||
94 | 10 | public function getMultipleBlameableAttributeRules() |
|
95 | { |
||
96 | 10 | return is_string($this->multiBlamesAttribute) ? [ |
|
97 | 10 | [[$this->multiBlamesAttribute], 'required'], |
|
98 | 10 | [[$this->multiBlamesAttribute], 'string', 'max' => $this->blamesLimit * 39 + 1], |
|
99 | 10 | [[$this->multiBlamesAttribute], 'default', 'value' => '[]'], |
|
100 | 10 | ] : []; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Add specified blame. |
||
105 | * @param {$this->multiBlamesClass}|string $blame |
||
|
|||
106 | * @return false|array |
||
107 | * @throws InvalidParamException |
||
108 | * @throws InvalidCallException |
||
109 | */ |
||
110 | 2 | public function addBlame($blame) |
|
111 | { |
||
112 | 2 | if (!is_string($this->multiBlamesAttribute)) { |
|
113 | return false; |
||
114 | } |
||
115 | 2 | $blameGuid = ''; |
|
116 | 2 | if (is_string($blame)) { |
|
117 | 1 | $blameGuid = $blame; |
|
118 | 1 | } |
|
119 | 2 | if ($blame instanceof $this->multiBlamesClass) { |
|
120 | 1 | $blameGuid = $blame->guid; |
|
121 | 1 | } |
|
122 | 2 | $blameGuids = $this->getBlameGuids(true); |
|
123 | 2 | if (array_search($blameGuid, $blameGuids)) { |
|
124 | throw new InvalidParamException('the blame has existed.'); |
||
125 | } |
||
126 | 2 | if ($this->getBlamesCount() >= $this->blamesLimit) { |
|
127 | throw new InvalidCallException("the limit($this->blamesLimit) of blames has been reached."); |
||
128 | } |
||
129 | 2 | $blameGuids[] = $blameGuid; |
|
130 | 2 | $this->setBlameGuids($blameGuids); |
|
131 | 2 | return $this->getBlameGuids(); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Create blame. |
||
136 | * @param BaseUserModel $user who will own this blame. |
||
137 | * @param array $config blame class configuration array. |
||
138 | * @return {$this->multiBlamesClass} |
||
139 | */ |
||
140 | 1 | public static function createBlame($user, $config = []) |
|
150 | |||
151 | /** |
||
152 | * Add specified blame, or create it before adding if doesn't exist. |
||
153 | * But you should save the blame instance before adding, or the operation |
||
154 | * will fail. |
||
155 | * @param {$this->multiBlamesClass}|string|array $blame |
||
156 | * It will be regarded as blame's guid if it is a string. And assign the |
||
157 | * reference parameter $blame the instance if it existed, or create one if not |
||
158 | * found. |
||
159 | * If it is {$this->multiBlamesClass} instance and existed, then will add it, or |
||
160 | * false will be given if it is not found in database. So if you want to add |
||
161 | * blame instance, you should save it before adding. |
||
162 | * If it is a array, it will be regarded as configuration array of blame. |
||
163 | * Notice! This parameter passed by reference, so it must be a variable! |
||
164 | * @param BaseUserModel $user whose blame. |
||
165 | * If null, it will take this blameable model's user. |
||
166 | * @return false|array false if blame created failed or not enable this feature. |
||
167 | * blames guid array if created and added successfully. |
||
168 | * @throws InvalidConfigException |
||
169 | * @throws InvalidParamException |
||
170 | * @see addBlame() |
||
171 | */ |
||
172 | 1 | public function addOrCreateBlame(&$blame = null, $user = null) |
|
199 | |||
200 | /** |
||
201 | * Remove specified blame. |
||
202 | * @param {$this->multiBlamesClass} $blame |
||
203 | * @return false|array all guids in json format. |
||
204 | */ |
||
205 | 1 | public function removeBlame($blame) |
|
206 | { |
||
207 | 1 | if (!is_string($this->multiBlamesAttribute)) { |
|
208 | return false; |
||
209 | } |
||
210 | 1 | $blameGuid = ''; |
|
211 | 1 | if (is_string($blame)) { |
|
212 | 1 | $blameGuid = $blame; |
|
213 | 1 | } |
|
214 | 1 | if ($blame instanceof $this->multiBlamesClass) { |
|
215 | 1 | $blameGuid = $blame->guid; |
|
216 | 1 | } |
|
217 | 1 | $blameGuids = $this->getBlameGuids(true); |
|
218 | 1 | if (($key = array_search($blameGuid, $blameGuids)) !== false) { |
|
219 | 1 | unset($blameGuids[$key]); |
|
220 | 1 | $this->setBlameGuids($blameGuids); |
|
221 | 1 | } |
|
222 | 1 | return $this->getBlameGuids(); |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Remove all blames. |
||
227 | */ |
||
228 | 10 | public function removeAllBlames() |
|
229 | { |
||
230 | 10 | $this->setBlameGuids(); |
|
231 | 10 | } |
|
232 | |||
233 | /** |
||
234 | * Count the blames. |
||
235 | * @return integer |
||
236 | */ |
||
237 | 2 | public function getBlamesCount() |
|
238 | { |
||
239 | 2 | return count($this->getBlameGuids(true)); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get the guid array of blames. it may check all guids if valid before return. |
||
244 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
245 | * @return array all guids in json format. |
||
246 | */ |
||
247 | 2 | public function getBlameGuids($checkValid = false) |
|
248 | { |
||
249 | 2 | $multiBlamesAttribute = $this->multiBlamesAttribute; |
|
250 | 2 | if ($multiBlamesAttribute === false) { |
|
251 | return []; |
||
252 | } |
||
253 | 2 | $jsonParser = new JsonParser(); |
|
254 | 2 | $guids = $jsonParser->parse($this->$multiBlamesAttribute, true); |
|
255 | 2 | if ($checkValid) { |
|
256 | 2 | $guids = $this->unsetInvalidBlames($guids); |
|
257 | 2 | } |
|
258 | 2 | return $guids; |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * Event triggered when blames list changed. |
||
263 | * @param MultipleBlameableEvent $event |
||
264 | */ |
||
265 | 10 | public function onBlamesChanged($event) |
|
266 | { |
||
267 | 10 | $sender = $event->sender; |
|
268 | 10 | $sender->blamesChanged = $event->blamesChanged; |
|
269 | 10 | } |
|
270 | |||
271 | /** |
||
272 | * Remove invalid blame guid from provided guid array. |
||
273 | * @param array $guids guid array of blames. |
||
274 | * @return array guid array of blames unset invalid. |
||
275 | */ |
||
276 | 10 | protected function unsetInvalidBlames($guids) |
|
277 | { |
||
278 | 10 | $checkedGuids = Number::unsetInvalidUuids($guids); |
|
279 | 10 | $multiBlamesClass = $this->multiBlamesClass; |
|
280 | 10 | foreach ($checkedGuids as $key => $guid) { |
|
281 | 2 | $blame = $multiBlamesClass::findOne($guid); |
|
282 | 2 | if (!$blame) { |
|
283 | 1 | unset($checkedGuids[$key]); |
|
284 | 1 | } |
|
285 | 10 | } |
|
286 | 10 | $diff = array_diff($guids, $checkedGuids); |
|
287 | 10 | $eventName = static::$eventMultipleBlamesChanged; |
|
288 | 10 | $this->trigger($eventName, new MultipleBlameableEvent(['blamesChanged' => !empty($diff)])); |
|
289 | 10 | return $checkedGuids; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Set the guid array of blames, it may check all guids if valid. |
||
294 | * @param array $guids guid array of blames. |
||
295 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
296 | * @return false|array all guids. |
||
297 | */ |
||
298 | 10 | public function setBlameGuids($guids = [], $checkValid = true) |
|
299 | { |
||
300 | 10 | if (!is_array($guids) || $this->multiBlamesAttribute === false) { |
|
301 | return null; |
||
302 | } |
||
303 | 10 | if ($checkValid) { |
|
304 | 10 | $guids = $this->unsetInvalidBlames($guids); |
|
305 | 10 | } |
|
306 | 10 | $multiBlamesAttribute = $this->multiBlamesAttribute; |
|
307 | 10 | $this->$multiBlamesAttribute = json_encode(array_values($guids)); |
|
308 | 10 | return $guids; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get blame. |
||
313 | * @param string $blameGuid |
||
314 | * @return {$this->multiBlamesClass} |
||
315 | */ |
||
316 | 2 | public static function getBlame($blameGuid) |
|
317 | { |
||
318 | 2 | $self = static::buildNoInitModel(); |
|
319 | 2 | if (empty($self->multiBlamesClass) || !is_string($self->multiBlamesClass) || $self->multiBlamesAttribute === false) { |
|
320 | return null; |
||
321 | } |
||
322 | 2 | $mbClass = $self->multiBlamesClass; |
|
323 | 2 | return $mbClass::findOne($blameGuid); |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Get or create blame. |
||
328 | * @param string|array $blameGuid |
||
329 | * @param BaseUserModel $user |
||
330 | * @return {$this->multiBlamesClass}|null |
||
331 | */ |
||
332 | 1 | public static function getOrCreateBlame($blameGuid, $user = null) |
|
345 | |||
346 | /** |
||
347 | * Get all ones to be blamed by `$blame`. |
||
348 | * @param {$this->multiBlamesClass} $blame |
||
349 | * @return array |
||
350 | */ |
||
351 | 1 | public function getBlameds($blame) |
|
352 | { |
||
353 | 1 | $blameds = static::getBlame($blame->guid); |
|
354 | 1 | if (empty($blameds)) { |
|
355 | 1 | return null; |
|
356 | } |
||
357 | 1 | $createdByAttribute = $this->createdByAttribute; |
|
358 | 1 | return static::find()->where([$createdByAttribute => $this->$createdByAttribute]) |
|
359 | 1 | ->andWhere(['like', $this->multiBlamesAttribute, $blame->guid])->all(); |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * Get all the blames of record. |
||
364 | * @return array all blames. |
||
365 | */ |
||
366 | 1 | public function getAllBlames() |
|
367 | { |
||
368 | 1 | if (empty($this->multiBlamesClass) || |
|
369 | 1 | !is_string($this->multiBlamesClass) || |
|
370 | 1 | $this->multiBlamesAttribute === false) { |
|
371 | return null; |
||
372 | } |
||
373 | 1 | $multiBlamesClass = $this->multiBlamesClass; |
|
374 | 1 | $createdByAttribute = $this->createdByAttribute; |
|
375 | 1 | return $multiBlamesClass::findAll([$createdByAttribute => $this->$createdByAttribute]); |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get all records which without any blames. |
||
380 | * @return array all non-blameds. |
||
381 | */ |
||
382 | 1 | public function getNonBlameds() |
|
383 | { |
||
384 | 1 | $createdByAttribute = $this->createdByAttribute; |
|
385 | $cond = [ |
||
386 | 1 | $createdByAttribute => $this->$createdByAttribute, |
|
387 | 1 | $this->multiBlamesAttribute => static::getEmptyBlamesJson() |
|
388 | 1 | ]; |
|
389 | 1 | return static::find()->where($cond)->all(); |
|
390 | } |
||
391 | |||
392 | /** |
||
393 | * Initialize blames limit. |
||
394 | * @param ModelEvent $event |
||
395 | */ |
||
396 | 10 | public function onInitBlamesLimit($event) |
|
397 | { |
||
398 | 10 | $sender = $event->sender; |
|
399 | 10 | if (!is_int($sender->blamesLimit) || $sender->blamesLimit < 1 || $sender->blamesLimit > 64) { |
|
400 | $sender->blamesLimit = 10; |
||
401 | } |
||
402 | 10 | } |
|
403 | |||
404 | /** |
||
405 | * Get the json of empty blames array. |
||
406 | * @return string |
||
407 | */ |
||
408 | 1 | public static function getEmptyBlamesJson() |
|
412 | } |
||
413 |
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.