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 = []) |
|
131 | |||
132 | /** |
||
133 | * Performs access check for the specified user based on the data loaded from cache. |
||
134 | * This method is internally called by [[checkAccess()]] when [[cache]] is enabled. |
||
135 | * @param string|int $user the user ID. This should can be either an integer or a string representing |
||
136 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
137 | * @param string $itemName the name of the operation that need access check |
||
138 | * @param array $params name-value pairs that would be passed to rules associated |
||
139 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
140 | * which holds the value of `$userId`. |
||
141 | * @param Assignment[] $assignments the assignments to the specified user |
||
142 | * @return bool whether the operations can be performed by the user. |
||
143 | * @since 2.0.3 |
||
144 | */ |
||
145 | 2 | protected function checkAccessFromCache($user, $itemName, $params, $assignments) |
|
173 | |||
174 | /** |
||
175 | * Performs access check for the specified user. |
||
176 | * This method is internally called by [[checkAccess()]]. |
||
177 | * @param string|int $user the user ID. This should can be either an integer or a string representing |
||
178 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
179 | * @param string $itemName the name of the operation that need access check |
||
180 | * @param array $params name-value pairs that would be passed to rules associated |
||
181 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
182 | * which holds the value of `$userId`. |
||
183 | * @param Assignment[] $assignments the assignments to the specified user |
||
184 | * @return bool whether the operations can be performed by the user. |
||
185 | */ |
||
186 | 6 | protected function checkAccessRecursive($user, $itemName, $params, $assignments) |
|
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | 35 | protected function getItem($name) |
|
239 | |||
240 | /** |
||
241 | * Returns a value indicating whether the database supports cascading update and delete. |
||
242 | * The default implementation will return false for SQLite database and true for all other databases. |
||
243 | * @return bool whether the database supports cascading update and delete. |
||
244 | */ |
||
245 | 24 | protected function supportsCascadeUpdate() |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 80 | protected function addItem($item) |
|
277 | |||
278 | /** |
||
279 | * @inheritdoc |
||
280 | */ |
||
281 | 4 | protected function removeItem($item) |
|
300 | |||
301 | /** |
||
302 | * @inheritdoc |
||
303 | */ |
||
304 | 8 | protected function updateItem($name, $item) |
|
335 | |||
336 | /** |
||
337 | * @inheritdoc |
||
338 | */ |
||
339 | 80 | protected function addRule($rule) |
|
360 | |||
361 | /** |
||
362 | * @inheritdoc |
||
363 | */ |
||
364 | 4 | protected function updateRule($name, $rule) |
|
387 | |||
388 | /** |
||
389 | * @inheritdoc |
||
390 | */ |
||
391 | 4 | protected function removeRule($rule) |
|
407 | |||
408 | /** |
||
409 | * @inheritdoc |
||
410 | */ |
||
411 | 16 | protected function getItems($type) |
|
424 | |||
425 | /** |
||
426 | * Populates an auth item with the data fetched from database |
||
427 | * @param array $row the data from the auth item table |
||
428 | * @return Item the populated auth item instance (either Role or Permission) |
||
429 | */ |
||
430 | 76 | protected function populateItem($row) |
|
448 | |||
449 | /** |
||
450 | * @inheritdoc |
||
451 | */ |
||
452 | 8 | public function getRolesByUser($userId) |
|
470 | |||
471 | /** |
||
472 | * @inheritdoc |
||
473 | */ |
||
474 | 4 | public function getChildRoles($roleName) |
|
493 | |||
494 | /** |
||
495 | * @inheritdoc |
||
496 | */ |
||
497 | 4 | public function getPermissionsByRole($roleName) |
|
515 | |||
516 | /** |
||
517 | * @inheritdoc |
||
518 | */ |
||
519 | 4 | public function getPermissionsByUser($userId) |
|
530 | |||
531 | /** |
||
532 | * Returns all permissions that are directly assigned to user. |
||
533 | * @param string|int $userId the user ID (see [[\yii\web\User::id]]) |
||
534 | * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names. |
||
535 | * @since 2.0.7 |
||
536 | */ |
||
537 | 4 | protected function getDirectPermissionsByUser($userId) |
|
551 | |||
552 | /** |
||
553 | * Returns all permissions that the user inherits from the roles assigned to him. |
||
554 | * @param string|int $userId the user ID (see [[\yii\web\User::id]]) |
||
555 | * @return Permission[] all inherited permissions that the user has. The array is indexed by the permission names. |
||
556 | * @since 2.0.7 |
||
557 | */ |
||
558 | 4 | protected function getInheritedPermissionsByUser($userId) |
|
584 | |||
585 | /** |
||
586 | * Returns the children for every parent. |
||
587 | * @return array the children list. Each array key is a parent item name, |
||
588 | * and the corresponding array value is a list of child item names. |
||
589 | */ |
||
590 | 12 | protected function getChildrenList() |
|
599 | |||
600 | /** |
||
601 | * Recursively finds all children and grand children of the specified item. |
||
602 | * @param string $name the name of the item whose children are to be looked for. |
||
603 | * @param array $childrenList the child list built via [[getChildrenList()]] |
||
604 | * @param array $result the children and grand children (in array keys) |
||
605 | */ |
||
606 | 12 | protected function getChildrenRecursive($name, $childrenList, &$result) |
|
615 | |||
616 | /** |
||
617 | * @inheritdoc |
||
618 | */ |
||
619 | 76 | public function getRule($name) |
|
631 | |||
632 | /** |
||
633 | * @inheritdoc |
||
634 | */ |
||
635 | 20 | public function getRules() |
|
650 | |||
651 | /** |
||
652 | * @inheritdoc |
||
653 | */ |
||
654 | public function getAssignment($roleName, $userId) |
||
674 | |||
675 | /** |
||
676 | * @inheritdoc |
||
677 | */ |
||
678 | 12 | public function getAssignments($userId) |
|
699 | |||
700 | /** |
||
701 | * @inheritdoc |
||
702 | * @since 2.0.8 |
||
703 | */ |
||
704 | 4 | public function canAddChild($parent, $child) |
|
708 | |||
709 | /** |
||
710 | * @inheritdoc |
||
711 | */ |
||
712 | 72 | public function addChild($parent, $child) |
|
734 | |||
735 | /** |
||
736 | * @inheritdoc |
||
737 | */ |
||
738 | public function removeChild($parent, $child) |
||
748 | |||
749 | /** |
||
750 | * @inheritdoc |
||
751 | */ |
||
752 | public function removeChildren($parent) |
||
762 | |||
763 | /** |
||
764 | * @inheritdoc |
||
765 | */ |
||
766 | public function hasChild($parent, $child) |
||
773 | |||
774 | /** |
||
775 | * @inheritdoc |
||
776 | */ |
||
777 | 72 | public function getChildren($name) |
|
791 | |||
792 | /** |
||
793 | * Checks whether there is a loop in the authorization item hierarchy. |
||
794 | * @param Item $parent the parent item |
||
795 | * @param Item $child the child item to be added to the hierarchy |
||
796 | * @return bool whether a loop exists |
||
797 | */ |
||
798 | 72 | protected function detectLoop($parent, $child) |
|
810 | |||
811 | /** |
||
812 | * @inheritdoc |
||
813 | */ |
||
814 | 72 | public function assign($role, $userId) |
|
831 | |||
832 | /** |
||
833 | * @inheritdoc |
||
834 | */ |
||
835 | public function revoke($role, $userId) |
||
845 | |||
846 | /** |
||
847 | * @inheritdoc |
||
848 | */ |
||
849 | public function revokeAll($userId) |
||
859 | |||
860 | /** |
||
861 | * @inheritdoc |
||
862 | */ |
||
863 | 92 | public function removeAll() |
|
871 | |||
872 | /** |
||
873 | * @inheritdoc |
||
874 | */ |
||
875 | 4 | public function removeAllPermissions() |
|
879 | |||
880 | /** |
||
881 | * @inheritdoc |
||
882 | */ |
||
883 | 4 | public function removeAllRoles() |
|
887 | |||
888 | /** |
||
889 | * Removes all auth items of the specified type. |
||
890 | * @param int $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
891 | */ |
||
892 | 8 | protected function removeAllItems($type) |
|
917 | |||
918 | /** |
||
919 | * @inheritdoc |
||
920 | */ |
||
921 | 4 | public function removeAllRules() |
|
933 | |||
934 | /** |
||
935 | * @inheritdoc |
||
936 | */ |
||
937 | 92 | public function removeAllAssignments() |
|
941 | |||
942 | 92 | public function invalidateCache() |
|
951 | |||
952 | 8 | public function loadFromCache() |
|
986 | |||
987 | /** |
||
988 | * Returns all role assignment information for the specified role. |
||
989 | * @param string $roleName |
||
990 | * @return Assignment[] the assignments. An empty array will be |
||
991 | * returned if role is not assigned to any user. |
||
992 | * @since 2.0.7 |
||
993 | */ |
||
994 | 4 | public function getUserIdsByRole($roleName) |
|
1004 | } |
||
1005 |
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..