Complex classes like Role 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 Role, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Role extends EventSourcedAggregateRoot |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var UUID |
||
| 28 | */ |
||
| 29 | private $uuid; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var StringLiteral |
||
| 33 | */ |
||
| 34 | private $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Query[] |
||
| 38 | */ |
||
| 39 | private $queries = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Permission[] |
||
| 43 | */ |
||
| 44 | private $permissions = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var UUID[] |
||
| 48 | */ |
||
| 49 | private $labelIds = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var StringLiteral[] |
||
| 53 | */ |
||
| 54 | private $userIds = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getAggregateRootId() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param UUID $uuid |
||
| 66 | * @param StringLiteral $name |
||
| 67 | * @return Role |
||
| 68 | */ |
||
| 69 | public static function create( |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Rename the role. |
||
| 85 | * |
||
| 86 | * @param UUID $uuid |
||
| 87 | * @param StringLiteral $name |
||
| 88 | */ |
||
| 89 | public function rename( |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set a constraint on the role. |
||
| 98 | * |
||
| 99 | * @param UUID $uuid |
||
| 100 | * @param StringLiteral $query |
||
| 101 | */ |
||
| 102 | public function setConstraint( |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param SapiVersion $sapiVersion |
||
| 122 | * @param Query $query |
||
| 123 | */ |
||
| 124 | public function addConstraint(SapiVersion $sapiVersion, Query $query): void |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param SapiVersion $sapiVersion |
||
| 133 | * @param Query $query |
||
| 134 | */ |
||
| 135 | public function updateConstraint(SapiVersion $sapiVersion, Query $query): void |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param SapiVersion $sapiVersion |
||
| 145 | */ |
||
| 146 | public function removeConstraint(SapiVersion $sapiVersion): void |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param SapiVersion $sapiVersion |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | private function queryEmpty(SapiVersion $sapiVersion): bool |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param SapiVersion $sapiVersion |
||
| 164 | * @param Query $query |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | private function querySameValue(SapiVersion $sapiVersion, Query $query): bool |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add a permission to the role. |
||
| 174 | * |
||
| 175 | * @param UUID $uuid |
||
| 176 | * @param Permission $permission |
||
| 177 | */ |
||
| 178 | public function addPermission( |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Remove a permission from the role. |
||
| 189 | * |
||
| 190 | * @param UUID $uuid |
||
| 191 | * @param Permission $permission |
||
| 192 | */ |
||
| 193 | public function removePermission( |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param UUID $labelId |
||
| 204 | */ |
||
| 205 | public function addLabel( |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param \ValueObjects\Identity\UUID $labelId |
||
| 215 | */ |
||
| 216 | public function removeLabel( |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param StringLiteral $userId |
||
| 226 | */ |
||
| 227 | public function addUser( |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param StringLiteral $userId |
||
| 237 | */ |
||
| 238 | public function removeUser( |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Delete a role. |
||
| 248 | * |
||
| 249 | * @param UUID $uuid |
||
| 250 | */ |
||
| 251 | public function delete( |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param RoleCreated $roleCreated |
||
| 259 | */ |
||
| 260 | public function applyRoleCreated(RoleCreated $roleCreated) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param RoleRenamed $roleRenamed |
||
| 268 | */ |
||
| 269 | public function applyRoleRenamed(RoleRenamed $roleRenamed) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param ConstraintAdded $constraintAdded |
||
| 276 | */ |
||
| 277 | public function applyConstraintAdded(ConstraintAdded $constraintAdded) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param ConstraintUpdated $constraintUpdated |
||
| 284 | */ |
||
| 285 | public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param ConstraintRemoved $constraintRemoved |
||
| 292 | */ |
||
| 293 | public function applyConstraintRemoved(ConstraintRemoved $constraintRemoved) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param PermissionAdded $permissionAdded |
||
| 300 | */ |
||
| 301 | public function applyPermissionAdded(PermissionAdded $permissionAdded) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param PermissionRemoved $permissionRemoved |
||
| 310 | */ |
||
| 311 | public function applyPermissionRemoved(PermissionRemoved $permissionRemoved) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param LabelAdded $labelAdded |
||
| 320 | */ |
||
| 321 | public function applyLabelAdded(LabelAdded $labelAdded) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param LabelRemoved $labelRemoved |
||
| 329 | */ |
||
| 330 | public function applyLabelRemoved(LabelRemoved $labelRemoved) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param UserAdded $userAdded |
||
| 338 | */ |
||
| 339 | public function applyUserAdded(UserAdded $userAdded) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param UserRemoved $userRemoved |
||
| 347 | */ |
||
| 348 | public function applyUserRemoved(UserRemoved $userRemoved) |
||
| 356 | } |
||
| 357 |