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 | /*public function removeConstraint(SapiVersion $sapiVersion) |
||
144 | { |
||
145 | $this->apply(new ConstraintRemoved($this->uuid, $sapiVersion)); |
||
146 | }*/ |
||
147 | |||
148 | /** |
||
149 | * @param SapiVersion $sapiVersion |
||
150 | * @return bool |
||
151 | */ |
||
152 | private function queryEmpty(SapiVersion $sapiVersion): bool |
||
156 | |||
157 | /** |
||
158 | * @param SapiVersion $sapiVersion |
||
159 | * @param Query $query |
||
160 | * @return bool |
||
161 | */ |
||
162 | private function querySameValue(SapiVersion $sapiVersion, Query $query): bool |
||
166 | |||
167 | /** |
||
168 | * Add a permission to the role. |
||
169 | * |
||
170 | * @param UUID $uuid |
||
171 | * @param Permission $permission |
||
172 | */ |
||
173 | public function addPermission( |
||
181 | |||
182 | /** |
||
183 | * Remove a permission from the role. |
||
184 | * |
||
185 | * @param UUID $uuid |
||
186 | * @param Permission $permission |
||
187 | */ |
||
188 | public function removePermission( |
||
196 | |||
197 | /** |
||
198 | * @param UUID $labelId |
||
199 | */ |
||
200 | public function addLabel( |
||
207 | |||
208 | /** |
||
209 | * @param \ValueObjects\Identity\UUID $labelId |
||
210 | */ |
||
211 | public function removeLabel( |
||
218 | |||
219 | /** |
||
220 | * @param StringLiteral $userId |
||
221 | */ |
||
222 | public function addUser( |
||
229 | |||
230 | /** |
||
231 | * @param StringLiteral $userId |
||
232 | */ |
||
233 | public function removeUser( |
||
240 | |||
241 | /** |
||
242 | * Delete a role. |
||
243 | * |
||
244 | * @param UUID $uuid |
||
245 | */ |
||
246 | public function delete( |
||
251 | |||
252 | /** |
||
253 | * @param RoleCreated $roleCreated |
||
254 | */ |
||
255 | public function applyRoleCreated(RoleCreated $roleCreated) |
||
260 | |||
261 | /** |
||
262 | * @param RoleRenamed $roleRenamed |
||
263 | */ |
||
264 | public function applyRoleRenamed(RoleRenamed $roleRenamed) |
||
268 | |||
269 | /** |
||
270 | * @param ConstraintAdded $constraintAdded |
||
271 | */ |
||
272 | public function applyConstraintAdded(ConstraintAdded $constraintAdded) |
||
276 | |||
277 | /** |
||
278 | * @param ConstraintUpdated $constraintUpdated |
||
279 | */ |
||
280 | public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated) |
||
284 | |||
285 | /** |
||
286 | * @param ConstraintRemoved $constraintRemoved |
||
287 | */ |
||
288 | public function applyConstraintRemoved(ConstraintRemoved $constraintRemoved) |
||
292 | |||
293 | /** |
||
294 | * @param PermissionAdded $permissionAdded |
||
295 | */ |
||
296 | public function applyPermissionAdded(PermissionAdded $permissionAdded) |
||
302 | |||
303 | /** |
||
304 | * @param PermissionRemoved $permissionRemoved |
||
305 | */ |
||
306 | public function applyPermissionRemoved(PermissionRemoved $permissionRemoved) |
||
312 | |||
313 | /** |
||
314 | * @param LabelAdded $labelAdded |
||
315 | */ |
||
316 | public function applyLabelAdded(LabelAdded $labelAdded) |
||
321 | |||
322 | /** |
||
323 | * @param LabelRemoved $labelRemoved |
||
324 | */ |
||
325 | public function applyLabelRemoved(LabelRemoved $labelRemoved) |
||
330 | |||
331 | /** |
||
332 | * @param UserAdded $userAdded |
||
333 | */ |
||
334 | public function applyUserAdded(UserAdded $userAdded) |
||
339 | |||
340 | /** |
||
341 | * @param UserRemoved $userRemoved |
||
342 | */ |
||
343 | public function applyUserRemoved(UserRemoved $userRemoved) |
||
351 | } |
||
352 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.