Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
34 | class Role extends RestController |
||
35 | { |
||
36 | /** |
||
37 | * Role service. |
||
38 | * |
||
39 | * @var \eZ\Publish\API\Repository\RoleService |
||
40 | */ |
||
41 | protected $roleService; |
||
42 | |||
43 | /** |
||
44 | * User service. |
||
45 | * |
||
46 | * @var \eZ\Publish\API\Repository\UserService |
||
47 | */ |
||
48 | protected $userService; |
||
49 | |||
50 | /** |
||
51 | * Location service. |
||
52 | * |
||
53 | * @var \eZ\Publish\API\Repository\LocationService |
||
54 | */ |
||
55 | protected $locationService; |
||
56 | |||
57 | /** |
||
58 | * Construct controller. |
||
59 | * |
||
60 | * @param \eZ\Publish\API\Repository\RoleService $roleService |
||
61 | * @param \eZ\Publish\API\Repository\UserService $userService |
||
62 | * @param \eZ\Publish\API\Repository\LocationService $locationService |
||
63 | */ |
||
64 | public function __construct( |
||
73 | |||
74 | /** |
||
75 | * Create new role. |
||
76 | * |
||
77 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
78 | */ |
||
79 | View Code Duplication | public function createRole(Request $request) |
|
116 | |||
117 | /** |
||
118 | * Creates a new RoleDraft for an existing Role. |
||
119 | * |
||
120 | * @since 6.1 |
||
121 | * |
||
122 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedRole |
||
123 | */ |
||
124 | View Code Duplication | public function createRoleDraft(Request $request) |
|
161 | |||
162 | /** |
||
163 | * Loads list of roles. |
||
164 | * |
||
165 | * @return \eZ\Publish\Core\REST\Server\Values\RoleList |
||
166 | */ |
||
167 | public function listRoles(Request $request) |
||
190 | |||
191 | /** |
||
192 | * Loads role. |
||
193 | * |
||
194 | * @param $roleId |
||
195 | * |
||
196 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
197 | */ |
||
198 | public function loadRole($roleId) |
||
202 | |||
203 | /** |
||
204 | * Loads a role draft. |
||
205 | * |
||
206 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
207 | * |
||
208 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
209 | */ |
||
210 | public function loadRoleDraft($roleId) |
||
221 | |||
222 | /** |
||
223 | * Updates a role. |
||
224 | * |
||
225 | * @param $roleId |
||
226 | * |
||
227 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
228 | */ |
||
229 | public function updateRole($roleId, Request $request) |
||
243 | |||
244 | /** |
||
245 | * Updates a role draft. |
||
246 | * |
||
247 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
248 | * |
||
249 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
250 | */ |
||
251 | public function updateRoleDraft($roleId, Request $request) |
||
271 | |||
272 | /** |
||
273 | * Publishes a role draft. |
||
274 | * |
||
275 | * @param mixed $roleId Original role ID, or ID of the role draft itself |
||
276 | * @return Values\RestRole |
||
277 | */ |
||
278 | public function publishRoleDraft($roleId) |
||
294 | |||
295 | /** |
||
296 | * Delete a role by ID. |
||
297 | * |
||
298 | * @param $roleId |
||
299 | * |
||
300 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
301 | */ |
||
302 | public function deleteRole($roleId) |
||
310 | |||
311 | /** |
||
312 | * Loads the policies for the role. |
||
313 | * |
||
314 | * @param $roleId |
||
315 | * |
||
316 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
317 | */ |
||
318 | public function loadPolicies($roleId, Request $request) |
||
324 | |||
325 | /** |
||
326 | * Deletes all policies from a role. |
||
327 | * |
||
328 | * @param $roleId |
||
329 | * |
||
330 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
331 | */ |
||
332 | public function deletePolicies($roleId) |
||
342 | |||
343 | /** |
||
344 | * Loads a policy. |
||
345 | * |
||
346 | * @param $roleId |
||
347 | * @param $policyId |
||
348 | * |
||
349 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
350 | * |
||
351 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
352 | */ |
||
353 | public function loadPolicy($roleId, $policyId, Request $request) |
||
364 | |||
365 | /** |
||
366 | * Adds a policy to role. |
||
367 | * |
||
368 | * @param $roleId |
||
369 | * |
||
370 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedPolicy |
||
371 | */ |
||
372 | public function addPolicy($roleId, Request $request) |
||
405 | |||
406 | /** |
||
407 | * Updates a policy. |
||
408 | * |
||
409 | * @param $roleId |
||
410 | * @param $policyId |
||
411 | * |
||
412 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
413 | * |
||
414 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
415 | */ |
||
416 | public function updatePolicy($roleId, $policyId, Request $request) |
||
441 | |||
442 | /** |
||
443 | * Delete a policy from role. |
||
444 | * |
||
445 | * @param $roleId |
||
446 | * @param $policyId |
||
447 | * |
||
448 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
449 | * |
||
450 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
451 | */ |
||
452 | View Code Duplication | public function deletePolicy($roleId, $policyId, Request $request) |
|
472 | |||
473 | /** |
||
474 | * Assigns role to user. |
||
475 | * |
||
476 | * @param $userId |
||
477 | * |
||
478 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
479 | */ |
||
480 | public function assignRoleToUser($userId, Request $request) |
||
502 | |||
503 | /** |
||
504 | * Assigns role to user group. |
||
505 | * |
||
506 | * @param $groupPath |
||
507 | * |
||
508 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
509 | */ |
||
510 | public function assignRoleToUserGroup($groupPath, Request $request) |
||
535 | |||
536 | /** |
||
537 | * Un-assigns role from user. |
||
538 | * |
||
539 | * @param $userId |
||
540 | * @param $roleId |
||
541 | * |
||
542 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
543 | */ |
||
544 | public function unassignRoleFromUser($userId, $roleId) |
||
555 | |||
556 | /** |
||
557 | * Un-assigns role from user group. |
||
558 | * |
||
559 | * @param $groupPath |
||
560 | * @param $roleId |
||
561 | * |
||
562 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
563 | */ |
||
564 | public function unassignRoleFromUserGroup($groupPath, $roleId) |
||
577 | |||
578 | /** |
||
579 | * Loads role assignments for user. |
||
580 | * |
||
581 | * @param $userId |
||
582 | * |
||
583 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
584 | */ |
||
585 | public function loadRoleAssignmentsForUser($userId) |
||
593 | |||
594 | /** |
||
595 | * Loads role assignments for user group. |
||
596 | * |
||
597 | * @param $groupPath |
||
598 | * |
||
599 | * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignmentList |
||
600 | */ |
||
601 | public function loadRoleAssignmentsForUserGroup($groupPath) |
||
611 | |||
612 | /** |
||
613 | * Returns a role assignment to the given user. |
||
614 | * |
||
615 | * @param $userId |
||
616 | * @param $roleId |
||
617 | * |
||
618 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
619 | * |
||
620 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserRoleAssignment |
||
621 | */ |
||
622 | public function loadRoleAssignmentForUser($userId, $roleId, Request $request) |
||
635 | |||
636 | /** |
||
637 | * Returns a role assignment to the given user group. |
||
638 | * |
||
639 | * @param $groupPath |
||
640 | * @param $roleId |
||
641 | * |
||
642 | * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException |
||
643 | * |
||
644 | * @return \eZ\Publish\Core\REST\Server\Values\RestUserGroupRoleAssignment |
||
645 | */ |
||
646 | public function loadRoleAssignmentForUserGroup($groupPath, $roleId, Request $request) |
||
661 | |||
662 | /** |
||
663 | * Search all policies which are applied to a given user. |
||
664 | * |
||
665 | * @return \eZ\Publish\Core\REST\Server\Values\PolicyList |
||
666 | */ |
||
667 | public function listPoliciesForUser(Request $request) |
||
676 | |||
677 | /** |
||
678 | * Maps a RoleCreateStruct to a RoleUpdateStruct. |
||
679 | * |
||
680 | * Needed since both structs are encoded into the same media type on input. |
||
681 | * |
||
682 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $createStruct |
||
683 | * |
||
684 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
685 | */ |
||
686 | protected function mapToUpdateStruct(RoleCreateStruct $createStruct) |
||
694 | } |
||
695 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.