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 | 46 | 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 | 8 | public function addBlame($blame) |
|
| 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 | 10 | public static function createBlame($user, $config = []) |
|
| 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 | 4 | public function addOrCreateBlame(&$blame = null, $user = null) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Remove specified blame. |
||
| 201 | * @param {$this->multiBlamesClass}|string $blame |
||
| 202 | * @return false|array all guids in json format. |
||
| 203 | */ |
||
| 204 | 1 | public function removeBlame($blame) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Remove all blames. |
||
| 226 | */ |
||
| 227 | 49 | public function removeAllBlames() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Count the blames. |
||
| 234 | * @return integer |
||
| 235 | */ |
||
| 236 | 8 | public function getBlamesCount() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get the guid array of blames. it may check all guids if valid before return. |
||
| 243 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 244 | * @return array all guids in json format. |
||
| 245 | */ |
||
| 246 | 8 | public function getBlameGuids($checkValid = false) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Event triggered when blames list changed. |
||
| 261 | * @param MultipleBlameableEvent $event |
||
| 262 | */ |
||
| 263 | 49 | public function onBlamesChanged($event) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Remove invalid blame guid from provided guid array. |
||
| 271 | * @param array $guids guid array of blames. |
||
| 272 | * @return array guid array of blames unset invalid. |
||
| 273 | */ |
||
| 274 | 49 | protected function unsetInvalidBlames($guids) |
|
| 275 | { |
||
| 276 | 49 | $unchecked = $guids; |
|
| 277 | 49 | $multiBlamesClass = $this->multiBlamesClass; |
|
| 278 | 49 | $mbi = $multiBlamesClass::buildNoInitModel(); |
|
| 279 | 49 | foreach ($guids as $key => $guid) { |
|
| 280 | 8 | $blame = $multiBlamesClass::find()->where([$mbi->guidAttribute => $guid])->exists(); |
|
| 281 | 8 | if (!$blame) { |
|
| 282 | unset($guids[$key]); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | 49 | $diff = array_diff($unchecked, $guids); |
|
| 286 | 49 | $eventName = static::$eventMultipleBlamesChanged; |
|
| 287 | 49 | $this->trigger($eventName, new MultipleBlameableEvent(['blamesChanged' => !empty($diff)])); |
|
| 288 | 49 | return $guids; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set the guid array of blames, it may check all guids if valid. |
||
| 293 | * @param array $guids guid array of blames. |
||
| 294 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 295 | * @return false|array all guids. |
||
| 296 | */ |
||
| 297 | 49 | public function setBlameGuids($guids = [], $checkValid = true) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get blame. |
||
| 312 | * @param string $blameGuid |
||
| 313 | * @return {$this->multiBlamesClass} |
||
| 314 | */ |
||
| 315 | 6 | public static function getBlame($blameGuid) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get all blames that owned this relation. |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | 2 | public function getOwnBlames() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Set blames which would own this relation. |
||
| 339 | * @param array $blames |
||
| 340 | */ |
||
| 341 | public function setOwnBlames($blames) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check blame owned this. |
||
| 349 | * @param {$this->multiBlamesClass} $blame |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | 2 | public function isBlameOwned($blame) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get or create blame. |
||
| 363 | * @param string|array|null $blameGuid |
||
| 364 | * @param BaseUserModel $user |
||
| 365 | * @return {$this->multiBlamesClass}|null |
||
| 366 | */ |
||
| 367 | 4 | public static function getOrCreateBlame($blameGuid, $user = null) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Get all ones to be blamed by `$blame`. |
||
| 380 | * @param {$this->multiBlamesClass} $blame |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | 2 | public static function getBlameds($blame) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get all the blames of record. |
||
| 399 | * @param BaseUserModel $user |
||
| 400 | * @return array all blames. |
||
| 401 | */ |
||
| 402 | 2 | public static function getAllBlames($user) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Get all records which without any blames. |
||
| 417 | * @param BaseUserModel $user |
||
| 418 | * @return array all non-blameds. |
||
| 419 | */ |
||
| 420 | 1 | public static function getNonBlameds($user = null) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Initialize blames limit. |
||
| 437 | * @param ModelEvent $event |
||
| 438 | */ |
||
| 439 | 49 | public function onInitBlamesLimit($event) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Get blames which do not own any model. |
||
| 449 | * @param BaseUserModel $user |
||
| 450 | */ |
||
| 451 | 1 | public static function getEmptyBlames($user) |
|
| 465 | } |
||
| 466 |
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.