Complex classes like DbManager 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 DbManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DbManager extends BaseManager |
||
40 | { |
||
41 | /** |
||
42 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
43 | * After the DbManager object is created, if you want to change this property, you should only assign it |
||
44 | * with a DB connection object. |
||
45 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
46 | */ |
||
47 | public $db = 'db'; |
||
48 | /** |
||
49 | * @var string the name of the table storing authorization items. Defaults to "auth_item". |
||
50 | */ |
||
51 | public $itemTable = '{{%auth_item}}'; |
||
52 | /** |
||
53 | * @var string the name of the table storing authorization item hierarchy. Defaults to "auth_item_child". |
||
54 | */ |
||
55 | public $itemChildTable = '{{%auth_item_child}}'; |
||
56 | /** |
||
57 | * @var string the name of the table storing authorization item assignments. Defaults to "auth_assignment". |
||
58 | */ |
||
59 | public $assignmentTable = '{{%auth_assignment}}'; |
||
60 | /** |
||
61 | * @var string the name of the table storing rules. Defaults to "auth_rule". |
||
62 | */ |
||
63 | public $ruleTable = '{{%auth_rule}}'; |
||
64 | /** |
||
65 | * @var Cache|array|string the cache used to improve RBAC performance. This can be one of the following: |
||
66 | * |
||
67 | * - an application component ID (e.g. `cache`) |
||
68 | * - a configuration array |
||
69 | * - a [[\yii\caching\Cache]] object |
||
70 | * |
||
71 | * When this is not set, it means caching is not enabled. |
||
72 | * |
||
73 | * Note that by enabling RBAC cache, all auth items, rules and auth item parent-child relationships will |
||
74 | * be cached and loaded into memory. This will improve the performance of RBAC permission check. However, |
||
75 | * it does require extra memory and as a result may not be appropriate if your RBAC system contains too many |
||
76 | * auth items. You should seek other RBAC implementations (e.g. RBAC based on Redis storage) in this case. |
||
77 | * |
||
78 | * Also note that if you modify RBAC items, rules or parent-child relationships from outside of this component, |
||
79 | * you have to manually call [[invalidateCache()]] to ensure data consistency. |
||
80 | * |
||
81 | * @since 2.0.3 |
||
82 | */ |
||
83 | public $cache; |
||
84 | /** |
||
85 | * @var string the key used to store RBAC data in cache |
||
86 | * @see cache |
||
87 | * @since 2.0.3 |
||
88 | */ |
||
89 | public $cacheKey = 'rbac'; |
||
90 | |||
91 | /** |
||
92 | * @var Item[] all auth items (name => Item) |
||
93 | */ |
||
94 | protected $items; |
||
95 | /** |
||
96 | * @var Rule[] all auth rules (name => Rule) |
||
97 | */ |
||
98 | protected $rules; |
||
99 | /** |
||
100 | * @var array auth item parent-child relationships (childName => list of parents) |
||
101 | */ |
||
102 | protected $parents; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Initializes the application component. |
||
107 | * This method overrides the parent implementation by establishing the database connection. |
||
108 | */ |
||
109 | 92 | public function init() |
|
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | 8 | public function checkAccess($userId, $permissionName, $params = []) |
|
136 | |||
137 | /** |
||
138 | * Performs access check for the specified user based on the data loaded from cache. |
||
139 | * This method is internally called by [[checkAccess()]] when [[cache]] is enabled. |
||
140 | * @param string|int $user the user ID. This should can be either an integer or a string representing |
||
141 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
142 | * @param string $itemName the name of the operation that need access check |
||
143 | * @param array $params name-value pairs that would be passed to rules associated |
||
144 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
145 | * which holds the value of `$userId`. |
||
146 | * @param Assignment[] $assignments the assignments to the specified user |
||
147 | * @return bool whether the operations can be performed by the user. |
||
148 | * @since 2.0.3 |
||
149 | */ |
||
150 | 2 | protected function checkAccessFromCache($user, $itemName, $params, $assignments) |
|
178 | |||
179 | /** |
||
180 | * Performs access check for the specified user. |
||
181 | * This method is internally called by [[checkAccess()]]. |
||
182 | * @param string|int $user the user ID. This should can be either an integer or a string representing |
||
183 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
184 | * @param string $itemName the name of the operation that need access check |
||
185 | * @param array $params name-value pairs that would be passed to rules associated |
||
186 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
187 | * which holds the value of `$userId`. |
||
188 | * @param Assignment[] $assignments the assignments to the specified user |
||
189 | * @return bool whether the operations can be performed by the user. |
||
190 | */ |
||
191 | 6 | protected function checkAccessRecursive($user, $itemName, $params, $assignments) |
|
220 | |||
221 | /** |
||
222 | * @inheritdoc |
||
223 | */ |
||
224 | 35 | protected function getItem($name) |
|
244 | |||
245 | /** |
||
246 | * Returns a value indicating whether the database supports cascading update and delete. |
||
247 | * The default implementation will return false for SQLite database and true for all other databases. |
||
248 | * @return bool whether the database supports cascading update and delete. |
||
249 | */ |
||
250 | 24 | protected function supportsCascadeUpdate() |
|
254 | |||
255 | /** |
||
256 | * @inheritdoc |
||
257 | */ |
||
258 | 80 | protected function addItem($item) |
|
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | 4 | protected function removeItem($item) |
|
305 | |||
306 | /** |
||
307 | * @inheritdoc |
||
308 | */ |
||
309 | 8 | protected function updateItem($name, $item) |
|
340 | |||
341 | /** |
||
342 | * @inheritdoc |
||
343 | */ |
||
344 | 80 | protected function addRule($rule) |
|
365 | |||
366 | /** |
||
367 | * @inheritdoc |
||
368 | */ |
||
369 | 4 | protected function updateRule($name, $rule) |
|
392 | |||
393 | /** |
||
394 | * @inheritdoc |
||
395 | */ |
||
396 | 4 | protected function removeRule($rule) |
|
412 | |||
413 | /** |
||
414 | * @inheritdoc |
||
415 | */ |
||
416 | 16 | protected function getItems($type) |
|
429 | |||
430 | /** |
||
431 | * Populates an auth item with the data fetched from database |
||
432 | * @param array $row the data from the auth item table |
||
433 | * @return Item the populated auth item instance (either Role or Permission) |
||
434 | */ |
||
435 | 76 | protected function populateItem($row) |
|
453 | |||
454 | /** |
||
455 | * @inheritdoc |
||
456 | */ |
||
457 | 8 | public function getRolesByUser($userId) |
|
475 | |||
476 | /** |
||
477 | * @inheritdoc |
||
478 | */ |
||
479 | 4 | public function getChildRoles($roleName) |
|
498 | |||
499 | /** |
||
500 | * @inheritdoc |
||
501 | */ |
||
502 | 4 | public function getPermissionsByRole($roleName) |
|
520 | |||
521 | /** |
||
522 | * @inheritdoc |
||
523 | */ |
||
524 | 4 | public function getPermissionsByUser($userId) |
|
535 | |||
536 | /** |
||
537 | * Returns all permissions that are directly assigned to user. |
||
538 | * @param string|int $userId the user ID (see [[\yii\web\User::id]]) |
||
539 | * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names. |
||
540 | * @since 2.0.7 |
||
541 | */ |
||
542 | 4 | protected function getDirectPermissionsByUser($userId) |
|
556 | |||
557 | /** |
||
558 | * Returns all permissions that the user inherits from the roles assigned to him. |
||
559 | * @param string|int $userId the user ID (see [[\yii\web\User::id]]) |
||
560 | * @return Permission[] all inherited permissions that the user has. The array is indexed by the permission names. |
||
561 | * @since 2.0.7 |
||
562 | */ |
||
563 | 4 | protected function getInheritedPermissionsByUser($userId) |
|
589 | |||
590 | /** |
||
591 | * Returns the children for every parent. |
||
592 | * @return array the children list. Each array key is a parent item name, |
||
593 | * and the corresponding array value is a list of child item names. |
||
594 | */ |
||
595 | 12 | protected function getChildrenList() |
|
604 | |||
605 | /** |
||
606 | * Recursively finds all children and grand children of the specified item. |
||
607 | * @param string $name the name of the item whose children are to be looked for. |
||
608 | * @param array $childrenList the child list built via [[getChildrenList()]] |
||
609 | * @param array $result the children and grand children (in array keys) |
||
610 | */ |
||
611 | 12 | protected function getChildrenRecursive($name, $childrenList, &$result) |
|
620 | |||
621 | /** |
||
622 | * @inheritdoc |
||
623 | */ |
||
624 | 76 | public function getRule($name) |
|
636 | |||
637 | /** |
||
638 | * @inheritdoc |
||
639 | */ |
||
640 | 20 | public function getRules() |
|
655 | |||
656 | /** |
||
657 | * @inheritdoc |
||
658 | */ |
||
659 | public function getAssignment($roleName, $userId) |
||
679 | |||
680 | /** |
||
681 | * @inheritdoc |
||
682 | */ |
||
683 | 12 | public function getAssignments($userId) |
|
704 | |||
705 | /** |
||
706 | * @inheritdoc |
||
707 | * @since 2.0.8 |
||
708 | */ |
||
709 | 4 | public function canAddChild($parent, $child) |
|
713 | |||
714 | /** |
||
715 | * @inheritdoc |
||
716 | */ |
||
717 | 72 | public function addChild($parent, $child) |
|
739 | |||
740 | /** |
||
741 | * @inheritdoc |
||
742 | */ |
||
743 | public function removeChild($parent, $child) |
||
753 | |||
754 | /** |
||
755 | * @inheritdoc |
||
756 | */ |
||
757 | public function removeChildren($parent) |
||
767 | |||
768 | /** |
||
769 | * @inheritdoc |
||
770 | */ |
||
771 | public function hasChild($parent, $child) |
||
778 | |||
779 | /** |
||
780 | * @inheritdoc |
||
781 | */ |
||
782 | 72 | public function getChildren($name) |
|
796 | |||
797 | /** |
||
798 | * Checks whether there is a loop in the authorization item hierarchy. |
||
799 | * @param Item $parent the parent item |
||
800 | * @param Item $child the child item to be added to the hierarchy |
||
801 | * @return bool whether a loop exists |
||
802 | */ |
||
803 | 72 | protected function detectLoop($parent, $child) |
|
815 | |||
816 | /** |
||
817 | * @inheritdoc |
||
818 | */ |
||
819 | 72 | public function assign($role, $userId) |
|
836 | |||
837 | /** |
||
838 | * @inheritdoc |
||
839 | */ |
||
840 | public function revoke($role, $userId) |
||
850 | |||
851 | /** |
||
852 | * @inheritdoc |
||
853 | */ |
||
854 | public function revokeAll($userId) |
||
864 | |||
865 | /** |
||
866 | * @inheritdoc |
||
867 | */ |
||
868 | 92 | public function removeAll() |
|
876 | |||
877 | /** |
||
878 | * @inheritdoc |
||
879 | */ |
||
880 | 4 | public function removeAllPermissions() |
|
884 | |||
885 | /** |
||
886 | * @inheritdoc |
||
887 | */ |
||
888 | 4 | public function removeAllRoles() |
|
892 | |||
893 | /** |
||
894 | * Removes all auth items of the specified type. |
||
895 | * @param int $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
896 | */ |
||
897 | 8 | protected function removeAllItems($type) |
|
922 | |||
923 | /** |
||
924 | * @inheritdoc |
||
925 | */ |
||
926 | 4 | public function removeAllRules() |
|
938 | |||
939 | /** |
||
940 | * @inheritdoc |
||
941 | */ |
||
942 | 92 | public function removeAllAssignments() |
|
946 | |||
947 | 92 | public function invalidateCache() |
|
956 | |||
957 | 8 | public function loadFromCache() |
|
991 | |||
992 | /** |
||
993 | * Returns all role assignment information for the specified role. |
||
994 | * @param string $roleName |
||
995 | * @return Assignment[] the assignments. An empty array will be |
||
996 | * returned if role is not assigned to any user. |
||
997 | * @since 2.0.7 |
||
998 | */ |
||
999 | 4 | public function getUserIdsByRole($roleName) |
|
1009 | } |
||
1010 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..