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 FlatAuthorization 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 FlatAuthorization, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Myth\Auth; |
||
37 | class FlatAuthorization implements AuthorizeInterface { |
||
38 | |||
39 | protected $groupModel; |
||
40 | |||
41 | protected $permissionModel; |
||
42 | |||
43 | protected $error = NULL; |
||
44 | |||
45 | protected $user_model = null; |
||
46 | |||
47 | //-------------------------------------------------------------------- |
||
48 | |||
49 | public function __construct($groupModel = null, $permModel = null) |
||
56 | |||
57 | //-------------------------------------------------------------------- |
||
58 | |||
59 | public function error() |
||
63 | |||
64 | //-------------------------------------------------------------------- |
||
65 | |||
66 | /** |
||
67 | * Allows the consuming application to pass in a reference to the |
||
68 | * model that should be used. |
||
69 | * |
||
70 | * @param $model |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function useModel($model) |
||
79 | |||
80 | //-------------------------------------------------------------------- |
||
81 | |||
82 | //-------------------------------------------------------------------- |
||
83 | // Actions |
||
84 | //-------------------------------------------------------------------- |
||
85 | |||
86 | /** |
||
87 | * Checks to see if a user is in a group. |
||
88 | * |
||
89 | * Groups can be either a string, with the name of the group, an INT |
||
90 | * with the ID of the group, or an array of strings/ids that the |
||
91 | * user must belong to ONE of. (It's an OR check not an AND check) |
||
92 | * |
||
93 | * @param $groups |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function inGroup( $groups, $user_id ) |
||
139 | |||
140 | //-------------------------------------------------------------------- |
||
141 | |||
142 | /** |
||
143 | * Checks a user's groups to see if they have the specified permission. |
||
144 | * |
||
145 | * @param int|string $permission |
||
146 | * @param int|string $user_id |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function hasPermission( $permission, $user_id ) |
||
179 | |||
180 | //-------------------------------------------------------------------- |
||
181 | |||
182 | /** |
||
183 | * Makes a member a part of a group. |
||
184 | * |
||
185 | * @param $user_id |
||
186 | * @param $group // Either ID or name |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | View Code Duplication | public function addUserToGroup( $user_id, $group ) |
|
226 | |||
227 | //-------------------------------------------------------------------- |
||
228 | |||
229 | /** |
||
230 | * Removes a single user from a group. |
||
231 | * |
||
232 | * @param $user_id |
||
233 | * @param $group |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | View Code Duplication | public function removeUserFromGroup( $user_id, $group ) |
|
273 | |||
274 | //-------------------------------------------------------------------- |
||
275 | |||
276 | /** |
||
277 | * Adds a single permission to a single group. |
||
278 | * |
||
279 | * @param int|string $permission |
||
280 | * @param int|string $group |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | View Code Duplication | public function addPermissionToGroup( $permission, $group ) |
|
311 | |||
312 | //-------------------------------------------------------------------- |
||
313 | |||
314 | /** |
||
315 | * Removes a single permission from a group. |
||
316 | * |
||
317 | * @param int|string $permission |
||
318 | * @param int|string $group |
||
319 | * |
||
320 | * @return mixed |
||
321 | */ |
||
322 | View Code Duplication | public function removePermissionFromGroup( $permission, $group ) |
|
349 | |||
350 | //-------------------------------------------------------------------- |
||
351 | |||
352 | /** |
||
353 | * Assigns a single permission to a user, irregardless of permissions |
||
354 | * assigned by roles. This is saved to the user's meta information. |
||
355 | * |
||
356 | * @param int|string $permission |
||
357 | * @param int $user_id |
||
358 | * |
||
359 | * @return int|bool |
||
360 | */ |
||
361 | public function addPermissionToUser( $permission, $user_id ) |
||
403 | |||
404 | //-------------------------------------------------------------------- |
||
405 | |||
406 | /** |
||
407 | * Removes a single permission from a user. Only applies to permissions |
||
408 | * that have been assigned with addPermissionToUser, not to permissions |
||
409 | * inherited based on groups they belong to. |
||
410 | * |
||
411 | * @param int/string $permission |
||
412 | * @param int $user_id |
||
413 | */ |
||
414 | public function removePermissionFromUser( $permission, $user_id ) |
||
452 | |||
453 | //-------------------------------------------------------------------- |
||
454 | |||
455 | /** |
||
456 | * Checks to see if a user has private permission assigned to it. |
||
457 | * |
||
458 | * @param $user_id |
||
459 | * @param $permission |
||
460 | * |
||
461 | * @return bool|null |
||
462 | */ |
||
463 | public function doesUserHavePermission($user_id, $permission) |
||
493 | |||
494 | //-------------------------------------------------------------------- |
||
495 | |||
496 | |||
497 | |||
498 | //-------------------------------------------------------------------- |
||
499 | // Groups |
||
500 | //-------------------------------------------------------------------- |
||
501 | |||
502 | /** |
||
503 | * Grabs the details about a single group. |
||
504 | * |
||
505 | * @param $group |
||
506 | * |
||
507 | * @return object|null |
||
508 | */ |
||
509 | public function group( $group ) |
||
518 | |||
519 | //-------------------------------------------------------------------- |
||
520 | |||
521 | /** |
||
522 | * Grabs an array of all groups. |
||
523 | * |
||
524 | * @return array of objects |
||
525 | */ |
||
526 | public function groups() |
||
530 | |||
531 | //-------------------------------------------------------------------- |
||
532 | |||
533 | /** |
||
534 | * @param $name |
||
535 | * @param string $description |
||
536 | * |
||
537 | * @return mixed |
||
538 | */ |
||
539 | View Code Duplication | public function createGroup( $name, $description = '' ) |
|
557 | |||
558 | //-------------------------------------------------------------------- |
||
559 | |||
560 | /** |
||
561 | * Deletes a single group. |
||
562 | * |
||
563 | * @param int $group_id |
||
564 | * |
||
565 | * @return bool |
||
566 | */ |
||
567 | public function deleteGroup( $group_id ) |
||
578 | |||
579 | //-------------------------------------------------------------------- |
||
580 | |||
581 | /** |
||
582 | * Updates a single group's information. |
||
583 | * |
||
584 | * @param $id |
||
585 | * @param $name |
||
586 | * @param string $description |
||
587 | * |
||
588 | * @return mixed |
||
589 | */ |
||
590 | View Code Duplication | public function updateGroup( $id, $name, $description = '' ) |
|
610 | |||
611 | //-------------------------------------------------------------------- |
||
612 | |||
613 | /** |
||
614 | * Given a group, will return the group ID. The group can be either |
||
615 | * the ID or the name of the group. |
||
616 | * |
||
617 | * @param int|string $group |
||
618 | * |
||
619 | * @return int|false |
||
620 | */ |
||
621 | protected function getGroupID( $group ) |
||
639 | |||
640 | //-------------------------------------------------------------------- |
||
641 | |||
642 | //-------------------------------------------------------------------- |
||
643 | // Permissions |
||
644 | //-------------------------------------------------------------------- |
||
645 | |||
646 | /** |
||
647 | * Returns the details about a single permission. |
||
648 | * |
||
649 | * @param int|string $permission |
||
650 | * |
||
651 | * @return object|null |
||
652 | */ |
||
653 | public function permission( $permission ) |
||
662 | |||
663 | //-------------------------------------------------------------------- |
||
664 | |||
665 | /** |
||
666 | * Returns an array of all permissions in the system. |
||
667 | * |
||
668 | * @return mixed |
||
669 | */ |
||
670 | public function permissions() |
||
674 | |||
675 | //-------------------------------------------------------------------- |
||
676 | |||
677 | /** |
||
678 | * Creates a single permission. |
||
679 | * |
||
680 | * @param $name |
||
681 | * @param string $description |
||
682 | * |
||
683 | * @return mixed |
||
684 | */ |
||
685 | View Code Duplication | public function createPermission( $name, $description = '' ) |
|
703 | |||
704 | //-------------------------------------------------------------------- |
||
705 | |||
706 | /** |
||
707 | * Deletes a single permission and removes that permission from all groups. |
||
708 | * |
||
709 | * @param $permission |
||
710 | * |
||
711 | * @return mixed |
||
712 | */ |
||
713 | public function deletePermission( $permission_id ) |
||
727 | |||
728 | //-------------------------------------------------------------------- |
||
729 | |||
730 | /** |
||
731 | * Updates the details for a single permission. |
||
732 | * |
||
733 | * @param int $id |
||
734 | * @param string $name |
||
735 | * @param string $description |
||
736 | * |
||
737 | * @return bool |
||
738 | */ |
||
739 | View Code Duplication | public function updatePermission( $id, $name, $description = '' ) |
|
759 | |||
760 | //-------------------------------------------------------------------- |
||
761 | |||
762 | /** |
||
763 | * Verifies that a permission (either ID or the name) exists and returns |
||
764 | * the permission ID. |
||
765 | * |
||
766 | * @param int|string $permission |
||
767 | * |
||
768 | * @return int|null |
||
769 | */ |
||
770 | protected function getPermissionID( $permission ) |
||
790 | |||
791 | //-------------------------------------------------------------------- |
||
792 | |||
793 | } |
||
794 |