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 |
||
| 56 | trait MultipleBlameableTrait |
||
| 57 | { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string class name of multiple blameable class. |
||
| 61 | */ |
||
| 62 | public $multiBlamesClass = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string name of multiple blameable attribute. |
||
| 66 | */ |
||
| 67 | public $multiBlamesAttribute = 'blames'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var integer the limit of blames. it should be greater than or equal 1, and |
||
| 71 | * less than or equal 10. |
||
| 72 | */ |
||
| 73 | public $blamesLimit = 10; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var boolean determines whether blames list has been changed. |
||
| 77 | */ |
||
| 78 | public $blamesChanged = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string event name. |
||
| 82 | */ |
||
| 83 | public static $eventMultipleBlamesChanged = 'multipleBlamesChanged'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the rules associated with multiple blameable attribute. |
||
| 87 | * @return array rules. |
||
| 88 | */ |
||
| 89 | 8 | public function getMultipleBlameableAttributeRules() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Add specified blame. |
||
| 100 | * @param [multiBlamesClass]|string $blame |
||
|
|
|||
| 101 | * @return false|array |
||
| 102 | */ |
||
| 103 | 2 | public function addBlame($blame) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Create blame. |
||
| 129 | * @param \vistart\Models\models\BaseUserModel $user who will own this blame. |
||
| 130 | * @param array $config blame class config array. |
||
| 131 | */ |
||
| 132 | 1 | public static function createBlame($user, $config = []) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Add specified blame, or create it before adding if it didn't exist. |
||
| 144 | * @param [multiBlamesClass]|string|array $blame If this is string or |
||
| 145 | * [multiBlamesClass] instance, and the it existed, then will add it. If it |
||
| 146 | * didn't exist, and this is a array, it will be regarded as config array. |
||
| 147 | * Notice, This parameter passed by reference, so it must be a variable! |
||
| 148 | * @param \vistart\Models\models\BaseUserModel $user whose blame. |
||
| 149 | * If null, it will take this blameable model's user. |
||
| 150 | * @return false|array false if blame created failed or not enable this feature. |
||
| 151 | * blames guid array if created and added successfully. |
||
| 152 | * @throws \yii\base\InvalidConfigException |
||
| 153 | * @throws \yii\base\InvalidParamException |
||
| 154 | * @see addBlame() |
||
| 155 | */ |
||
| 156 | 1 | public function addOrCreateBlame(&$blame = null, $user = null) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Remove specified blame. |
||
| 186 | * @param [multiBlamesClass] $blame |
||
| 187 | * @return false|array all guids in json format. |
||
| 188 | */ |
||
| 189 | 1 | public function removeBlame($blame) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Remove all blames. |
||
| 211 | */ |
||
| 212 | 8 | public function removeAllBlames() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Count the blames. |
||
| 219 | * @return integer |
||
| 220 | */ |
||
| 221 | 2 | public function getBlamesCount() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get the guid array of blames. it may check all guids if valid before return. |
||
| 228 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 229 | * @return array all guids in json format. |
||
| 230 | */ |
||
| 231 | 2 | public function getBlameGuids($checkValid = false) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Event triggered when blames list changed. |
||
| 247 | * @param \vistart\Models\events\MultipleBlameableEvent $event |
||
| 248 | */ |
||
| 249 | 8 | public function onBlamesChanged($event) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Remove invalid blame guid from provided guid array. |
||
| 257 | * @param array $guids guid array of blames. |
||
| 258 | * @return array guid array of blames unset invalid. |
||
| 259 | */ |
||
| 260 | 8 | protected function unsetInvalidBlames($guids) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Set the guid array of blames, it may check all guids if valid. |
||
| 277 | * @param array $guids guid array of blames. |
||
| 278 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 279 | * @return false|array all guids. |
||
| 280 | */ |
||
| 281 | 8 | public function setBlameGuids($guids = [], $checkValid = true) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Get blame. |
||
| 296 | * @param string $blameGuid |
||
| 297 | * @return [multiBlamesClass] |
||
| 298 | */ |
||
| 299 | 2 | public static function getBlame($blameGuid) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * |
||
| 311 | * @param type $blameGuid |
||
| 312 | * @param type $user |
||
| 313 | * @return type |
||
| 314 | */ |
||
| 315 | 1 | public static function getOrCreateBlame($blameGuid, $user = null) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Get all ones to be blamed by `$blame`. |
||
| 331 | * @param [multiBlamesClass] $blame |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | 1 | public function getBlameds($blame) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Get all the blames of record. |
||
| 347 | * @return array all blames. |
||
| 348 | */ |
||
| 349 | 1 | public function getAllBlames() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get all records which without any blames. |
||
| 363 | * @return array all non-blameds. |
||
| 364 | */ |
||
| 365 | 1 | public function getNonBlameds() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Initialize blames limit. |
||
| 373 | * @param \yii\base\Event $event |
||
| 374 | */ |
||
| 375 | 8 | public function onInitBlamesLimit($event) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get the json of empty blames array. |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | 1 | public static function getEmptyBlamesJson() |
|
| 391 | } |
||
| 392 |
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.