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 RoleTrait 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 RoleTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait RoleTrait |
||
13 | { |
||
14 | /** |
||
15 | * Returns all roles in the database cluster. |
||
16 | * |
||
17 | * @param string $rolename (optional) The role name to exclude from the select |
||
18 | * |
||
19 | * @return \PHPPgAdmin\ADORecordSet Either one or All roles |
||
20 | */ |
||
21 | public function getRoles($rolename = '') |
||
35 | |||
36 | /** |
||
37 | * Returns information about a single role. |
||
38 | * |
||
39 | * @param string $rolename The name of the role to retrieve |
||
40 | * |
||
41 | * @return \PHPPgAdmin\ADORecordSet The role's data |
||
42 | */ |
||
43 | public function getRole($rolename) |
||
54 | |||
55 | /** |
||
56 | * Returns all users in the database cluster. |
||
57 | * |
||
58 | * @return \PHPPgAdmin\ADORecordSet All users |
||
59 | */ |
||
60 | public function getUsers() |
||
68 | |||
69 | /** |
||
70 | * Returns information about a single user. |
||
71 | * |
||
72 | * @param string $username The username of the user to retrieve |
||
73 | * |
||
74 | * @return \PHPPgAdmin\ADORecordSet The user's data |
||
75 | */ |
||
76 | public function getUser($username) |
||
86 | |||
87 | /** |
||
88 | * Creates a new role. |
||
89 | * |
||
90 | * @param string $rolename The name of the role to create |
||
91 | * @param string $password A password for the role |
||
92 | * @param bool $superuser Boolean whether or not the role is a superuser |
||
93 | * @param bool $createdb Boolean whether or not the role can create databases |
||
94 | * @param bool $createrole Boolean whether or not the role can create other roles |
||
95 | * @param bool $inherits Boolean whether or not the role inherits the privileges from parent roles |
||
96 | * @param bool $login Boolean whether or not the role will be allowed to login |
||
97 | * @param number $connlimit Number of concurrent connections the role can make |
||
98 | * @param string $expiry String Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire |
||
99 | * @param array $new_roles_to_add (array) Roles to which the new role will be immediately added as a new member |
||
100 | * @param array $new_members_of_role (array) Roles which are automatically added as members of the new role |
||
101 | * @param array $new_admins_of_role (array) Roles which are automatically added as admin members of the new role |
||
102 | * |
||
103 | * @return int 0 if operation was successful |
||
104 | */ |
||
105 | public function createRole( |
||
164 | |||
165 | /** |
||
166 | * Helper function that computes encypted PostgreSQL passwords. |
||
167 | * |
||
168 | * @param string $username The username |
||
169 | * @param string $password The password |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function _encryptPassword($username, $password) |
||
177 | |||
178 | /** |
||
179 | * Adjusts a role's info and renames it. |
||
180 | * |
||
181 | * @param string $rolename The name of the role to adjust |
||
182 | * @param string $password A password for the role |
||
183 | * @param bool $superuser Boolean whether or not the role is a superuser |
||
184 | * @param bool $createdb Boolean whether or not the role can create databases |
||
185 | * @param bool $createrole Boolean whether or not the role can create other roles |
||
186 | * @param bool $inherits Boolean whether or not the role inherits the privileges from parent roles |
||
187 | * @param bool $login Boolean whether or not the role will be allowed to login |
||
188 | * @param number $connlimit Number of concurrent connections the role can make |
||
189 | * @param string $expiry string Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire |
||
190 | * @param array $new_roles_to_add (array) Roles to which the role will be immediately added as a new member |
||
191 | * @param array $new_members_of_role (array) Roles which are automatically added as members of the role |
||
192 | * @param array $new_admins_of_role (array) Roles which are automatically added as admin members of the role |
||
193 | * @param string $original_parent_roles Original roles whose the role belongs to, comma separated |
||
194 | * @param string $original_members Original roles that are members of the role, comma separated |
||
195 | * @param string $original_admins Original roles that are admin members of the role, comma separated |
||
196 | * @param string $newrolename The new name of the role |
||
197 | * |
||
198 | * @return bool|int 0 success |
||
199 | */ |
||
200 | public function setRenameRole( |
||
258 | |||
259 | /** |
||
260 | * Renames a role. |
||
261 | * |
||
262 | * @param string $rolename The name of the role to rename |
||
263 | * @param string $newrolename The new name of the role |
||
264 | * |
||
265 | * @return int 0 if operation was successful |
||
266 | */ |
||
267 | public function renameRole($rolename, $newrolename) |
||
276 | |||
277 | View Code Duplication | private function _dealWithOldParentRoles($original_parent_roles, $new_roles_to_add, $rolename) |
|
304 | |||
305 | View Code Duplication | private function _dealWithOriginalMembers($original_members, $new_members_of_role, $rolename) |
|
330 | |||
331 | View Code Duplication | private function _dealWithOriginalAdmins($original_admins, $new_admins_of_role, $rolename) |
|
354 | |||
355 | private function _alterRole($rolename, $password, $connlimit, $expiry, $superuser, $createdb, $createrole, $inherits, $login) |
||
386 | |||
387 | /** |
||
388 | * Adjusts a role's info. |
||
389 | * |
||
390 | * @param string $rolename The name of the role to adjust |
||
391 | * @param string $password A password for the role |
||
392 | * @param bool $superuser Boolean whether or not the role is a superuser |
||
393 | * @param bool $createdb Boolean whether or not the role can create databases |
||
394 | * @param bool $createrole Boolean whether or not the role can create other roles |
||
395 | * @param bool $inherits Boolean whether or not the role inherits the privileges from parent roles |
||
396 | * @param bool $login Boolean whether or not the role will be allowed to login |
||
397 | * @param number $connlimit Number of concurrent connections the role can make |
||
398 | * @param string $expiry string Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire |
||
399 | * @param array $new_roles_to_add (array) Roles to which the role will be immediately added as a new member |
||
400 | * @param array $new_members_of_role (array) Roles which are automatically added as members of the role |
||
401 | * @param array $new_admins_of_role (array) Roles which are automatically added as admin members of the role |
||
402 | * @param string $original_parent_roles Original roles whose the role belongs to, comma separated |
||
403 | * @param string $original_members Original roles that are members of the role, comma separated |
||
404 | * @param string $original_admins Original roles that are admin members of the role, comma separated |
||
405 | * |
||
406 | * @return int 0 if operation was successful |
||
407 | */ |
||
408 | public function setRole( |
||
462 | |||
463 | /** |
||
464 | * Grants membership in a role. |
||
465 | * |
||
466 | * @param string $role The name of the target role |
||
467 | * @param string $rolename The name of the role that will belong to the target role |
||
468 | * @param int $admin (optional) Flag to grant the admin option |
||
469 | * |
||
470 | * @return int 0 if operation was successful |
||
471 | */ |
||
472 | View Code Duplication | public function grantRole($role, $rolename, $admin = 0) |
|
484 | |||
485 | /** |
||
486 | * Revokes membership in a role. |
||
487 | * |
||
488 | * @param string $role The name of the target role |
||
489 | * @param string $rolename The name of the role that will not belong to the target role |
||
490 | * @param int $admin (optional) Flag to revoke only the admin option |
||
491 | * @param string $type (optional) Type of revoke: RESTRICT | CASCADE |
||
492 | * |
||
493 | * @return int 0 if operation was successful |
||
494 | */ |
||
495 | View Code Duplication | public function revokeRole($role, $rolename, $admin = 0, $type = 'RESTRICT') |
|
509 | |||
510 | /** |
||
511 | * Removes a role. |
||
512 | * |
||
513 | * @param string $rolename The name of the role to drop |
||
514 | * |
||
515 | * @return int 0 if operation was successful |
||
516 | */ |
||
517 | public function dropRole($rolename) |
||
525 | |||
526 | /** |
||
527 | * Creates a new user. |
||
528 | * |
||
529 | * @param string $username The username of the user to create |
||
530 | * @param string $password A password for the user |
||
531 | * @param bool $createdb boolean Whether or not the user can create databases |
||
532 | * @param bool $createuser boolean Whether or not the user can create other users |
||
533 | * @param string $expiry string Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire |
||
534 | * @param array $groups The groups to create the user in |
||
535 | * |
||
536 | * @return int 0 if operation was successful |
||
537 | * |
||
538 | * @internal param $group (array) The groups to create the user in |
||
539 | */ |
||
540 | View Code Duplication | public function createUser($username, $password, $createdb, $createuser, $expiry, $groups) |
|
567 | |||
568 | /** |
||
569 | * Adjusts a user's info and renames the user. |
||
570 | * |
||
571 | * @param string $username The username of the user to modify |
||
572 | * @param string $password A new password for the user |
||
573 | * @param bool $createdb boolean Whether or not the user can create databases |
||
574 | * @param bool $createuser boolean Whether or not the user can create other users |
||
575 | * @param string $expiry string Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire. |
||
576 | * @param string $newname The new name of the user |
||
577 | * |
||
578 | * @return bool|int 0 success |
||
579 | */ |
||
580 | public function setRenameUser($username, $password, $createdb, $createuser, $expiry, $newname) |
||
606 | |||
607 | /** |
||
608 | * Renames a user. |
||
609 | * |
||
610 | * @param string $username The username of the user to rename |
||
611 | * @param string $newname The new name of the user |
||
612 | * |
||
613 | * @return int 0 if operation was successful |
||
614 | */ |
||
615 | public function renameUser($username, $newname) |
||
624 | |||
625 | // Tablespace functions |
||
626 | |||
627 | /** |
||
628 | * Adjusts a user's info. |
||
629 | * |
||
630 | * @param string $username The username of the user to modify |
||
631 | * @param string $password A new password for the user |
||
632 | * @param bool $createdb boolean Whether or not the user can create databases |
||
633 | * @param bool $createuser boolean Whether or not the user can create other users |
||
634 | * @param string $expiry string Format 'YYYY-MM-DD HH:MM:SS'. '' means never expire. |
||
635 | * |
||
636 | * @return int 0 if operation was successful |
||
637 | */ |
||
638 | public function setUser($username, $password, $createdb, $createuser, $expiry) |
||
660 | |||
661 | /** |
||
662 | * Removes a user. |
||
663 | * |
||
664 | * @param string $username The username of the user to drop |
||
665 | * |
||
666 | * @return int 0 if operation was successful |
||
667 | */ |
||
668 | public function dropUser($username) |
||
676 | |||
677 | /** |
||
678 | * Changes a role's password. |
||
679 | * |
||
680 | * @param string $rolename The role name |
||
681 | * @param string $password The new password |
||
682 | * |
||
683 | * @return int 0 if operation was successful |
||
684 | */ |
||
685 | View Code Duplication | public function changePassword($rolename, $password) |
|
695 | |||
696 | /** |
||
697 | * Adds a group member. |
||
698 | * |
||
699 | * @param string $groname The name of the group |
||
700 | * @param string $user The name of the user to add to the group |
||
701 | * |
||
702 | * @return int 0 if operation was successful |
||
703 | */ |
||
704 | public function addGroupMember($groname, $user) |
||
713 | |||
714 | /** |
||
715 | * Returns all role names which the role belongs to. |
||
716 | * |
||
717 | * @param string $rolename The role name |
||
718 | * |
||
719 | * @return \PHPPgAdmin\ADORecordSet All role names |
||
720 | */ |
||
721 | public function getMemberOf($rolename) |
||
735 | |||
736 | // Administration functions |
||
737 | |||
738 | /** |
||
739 | * Returns all role names that are members of a role. |
||
740 | * |
||
741 | * @param string $rolename The role name |
||
742 | * @param string $admin (optional) Find only admin members |
||
743 | * |
||
744 | * @return \PHPPgAdmin\ADORecordSet All role names |
||
745 | */ |
||
746 | public function getMembers($rolename, $admin = 'f') |
||
759 | |||
760 | /** |
||
761 | * Removes a group member. |
||
762 | * |
||
763 | * @param string $groname The name of the group |
||
764 | * @param string $user The name of the user to remove from the group |
||
765 | * |
||
766 | * @return int 0 if operation was successful |
||
767 | */ |
||
768 | public function dropGroupMember($groname, $user) |
||
777 | |||
778 | /** |
||
779 | * Return users in a specific group. |
||
780 | * |
||
781 | * @param string $groname The name of the group |
||
782 | * |
||
783 | * @return \PHPPgAdmin\ADORecordSet All users in the group |
||
784 | */ |
||
785 | public function getGroup($groname) |
||
796 | |||
797 | /** |
||
798 | * Returns all groups in the database cluser. |
||
799 | * |
||
800 | * @return \PHPPgAdmin\ADORecordSet All groups |
||
801 | */ |
||
802 | public function getGroups() |
||
808 | |||
809 | /** |
||
810 | * Creates a new group. |
||
811 | * |
||
812 | * @param string $groname The name of the group |
||
813 | * @param array $users An array of users to add to the group |
||
814 | * |
||
815 | * @return int 0 if operation was successful |
||
816 | */ |
||
817 | public function createGroup($groname, $users) |
||
830 | |||
831 | /** |
||
832 | * Removes a group. |
||
833 | * |
||
834 | * @param string $groname The name of the group to drop |
||
835 | * |
||
836 | * @return int 0 if operation was successful |
||
837 | */ |
||
838 | public function dropGroup($groname) |
||
846 | |||
847 | abstract public function fieldClean(&$str); |
||
848 | |||
849 | abstract public function beginTransaction(); |
||
850 | |||
851 | abstract public function rollbackTransaction(); |
||
852 | |||
853 | abstract public function endTransaction(); |
||
854 | |||
855 | abstract public function execute($sql); |
||
856 | |||
857 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
858 | |||
859 | abstract public function selectSet($sql); |
||
860 | |||
861 | abstract public function clean(&$str); |
||
862 | |||
863 | abstract public function hasGrantOption(); |
||
864 | |||
865 | abstract public function getFunction($function_oid); |
||
866 | |||
867 | abstract public function fieldArrayClean(&$arr); |
||
868 | } |
||
869 |
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.