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 = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Add specified blame, or create it before adding if it didn't exist. |
||
| 145 | * @param [multiBlamesClass]|string|array $blame If this is string or |
||
| 146 | * [multiBlamesClass] instance, and the it existed, then will add it. If it |
||
| 147 | * didn't exist, and this is a array, it will be regarded as config array. |
||
| 148 | * Notice, This parameter passed by reference, so it must be a variable! |
||
| 149 | * @param \vistart\Models\models\BaseUserModel $user whose blame. |
||
| 150 | * If null, it will take this blameable model's user. |
||
| 151 | * @return false|array false if blame created failed or not enable this feature. |
||
| 152 | * blames guid array if created and added successfully. |
||
| 153 | * @throws \yii\base\InvalidConfigException |
||
| 154 | * @throws \yii\base\InvalidParamException |
||
| 155 | * @see addBlame() |
||
| 156 | */ |
||
| 157 | 1 | public function addOrCreateBlame(&$blame = null, $user = null) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Remove specified blame. |
||
| 187 | * @param [multiBlamesClass] $blame |
||
| 188 | * @return false|array all guids in json format. |
||
| 189 | */ |
||
| 190 | 1 | public function removeBlame($blame) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Remove all blames. |
||
| 212 | */ |
||
| 213 | 8 | public function removeAllBlames() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Count the blames. |
||
| 220 | * @return integer |
||
| 221 | */ |
||
| 222 | 2 | public function getBlamesCount() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get the guid array of blames. it may check all guids if valid before return. |
||
| 229 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 230 | * @return array all guids in json format. |
||
| 231 | */ |
||
| 232 | 2 | public function getBlameGuids($checkValid = false) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Event triggered when blames list changed. |
||
| 248 | * @param \vistart\Models\events\MultipleBlameableEvent $event |
||
| 249 | */ |
||
| 250 | 8 | public function onBlamesChanged($event) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Remove invalid blame guid from provided guid array. |
||
| 258 | * @param array $guids guid array of blames. |
||
| 259 | * @return array guid array of blames unset invalid. |
||
| 260 | */ |
||
| 261 | 8 | protected function unsetInvalidBlames($guids) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Set the guid array of blames, it may check all guids if valid. |
||
| 279 | * @param array $guids guid array of blames. |
||
| 280 | * @param boolean $checkValid determines whether checking the blame is valid. |
||
| 281 | * @return false|array all guids. |
||
| 282 | */ |
||
| 283 | 8 | public function setBlameGuids($guids = [], $checkValid = true) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get blame. |
||
| 298 | * @param string $blameGuid |
||
| 299 | * @return [multiBlamesClass] |
||
| 300 | */ |
||
| 301 | 2 | public static function getBlame($blameGuid) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get or create blame. |
||
| 313 | * @param string|array $blameGuid |
||
| 314 | * @param \vistart\Models\models\BaseUserClass $user |
||
| 315 | * @return [multiBlamesClass]|null |
||
| 316 | */ |
||
| 317 | 1 | public static function getOrCreateBlame($blameGuid, $user = null) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get all ones to be blamed by `$blame`. |
||
| 333 | * @param [multiBlamesClass] $blame |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | 1 | public function getBlameds($blame) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get all the blames of record. |
||
| 349 | * @return array all blames. |
||
| 350 | */ |
||
| 351 | 1 | public function getAllBlames() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get all records which without any blames. |
||
| 365 | * @return array all non-blameds. |
||
| 366 | */ |
||
| 367 | 1 | public function getNonBlameds() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Initialize blames limit. |
||
| 379 | * @param \yii\base\Event $event |
||
| 380 | */ |
||
| 381 | 8 | public function onInitBlamesLimit($event) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get the json of empty blames array. |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 1 | public static function getEmptyBlamesJson() |
|
| 397 | } |
||
| 398 |
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.