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 | public function init() |
||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | 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 | 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 | protected function checkAccessRecursive($user, $itemName, $params, $assignments) |
||
220 | |||
221 | /** |
||
222 | * @inheritdoc |
||
223 | */ |
||
224 | 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 | protected function supportsCascadeUpdate() |
||
254 | |||
255 | /** |
||
256 | * @inheritdoc |
||
257 | */ |
||
258 | protected function addItem($item) |
||
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | protected function removeItem($item) |
||
305 | |||
306 | /** |
||
307 | * @inheritdoc |
||
308 | */ |
||
309 | protected function updateItem($name, $item) |
||
340 | |||
341 | /** |
||
342 | * @inheritdoc |
||
343 | */ |
||
344 | protected function addRule($rule) |
||
365 | |||
366 | /** |
||
367 | * @inheritdoc |
||
368 | */ |
||
369 | protected function updateRule($name, $rule) |
||
392 | |||
393 | /** |
||
394 | * @inheritdoc |
||
395 | */ |
||
396 | protected function removeRule($rule) |
||
412 | |||
413 | /** |
||
414 | * @inheritdoc |
||
415 | */ |
||
416 | 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 | protected function populateItem($row) |
||
453 | |||
454 | /** |
||
455 | * @inheritdoc |
||
456 | */ |
||
457 | public function getRolesByUser($userId) |
||
475 | |||
476 | /** |
||
477 | * @inheritdoc |
||
478 | */ |
||
479 | public function getChildRoles($roleName) |
||
498 | |||
499 | /** |
||
500 | * @inheritdoc |
||
501 | */ |
||
502 | public function getPermissionsByRole($roleName) |
||
520 | |||
521 | /** |
||
522 | * @inheritdoc |
||
523 | */ |
||
524 | 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 | 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 | 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 | 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 | protected function getChildrenRecursive($name, $childrenList, &$result) |
||
620 | |||
621 | /** |
||
622 | * @inheritdoc |
||
623 | */ |
||
624 | public function getRule($name) |
||
644 | |||
645 | /** |
||
646 | * @inheritdoc |
||
647 | */ |
||
648 | public function getRules() |
||
667 | |||
668 | /** |
||
669 | * @inheritdoc |
||
670 | */ |
||
671 | public function getAssignment($roleName, $userId) |
||
691 | |||
692 | /** |
||
693 | * @inheritdoc |
||
694 | */ |
||
695 | public function getAssignments($userId) |
||
716 | |||
717 | /** |
||
718 | * @inheritdoc |
||
719 | * @since 2.0.8 |
||
720 | */ |
||
721 | public function canAddChild($parent, $child) |
||
725 | |||
726 | /** |
||
727 | * @inheritdoc |
||
728 | */ |
||
729 | public function addChild($parent, $child) |
||
751 | |||
752 | /** |
||
753 | * @inheritdoc |
||
754 | */ |
||
755 | public function removeChild($parent, $child) |
||
765 | |||
766 | /** |
||
767 | * @inheritdoc |
||
768 | */ |
||
769 | public function removeChildren($parent) |
||
779 | |||
780 | /** |
||
781 | * @inheritdoc |
||
782 | */ |
||
783 | public function hasChild($parent, $child) |
||
790 | |||
791 | /** |
||
792 | * @inheritdoc |
||
793 | */ |
||
794 | public function getChildren($name) |
||
808 | |||
809 | /** |
||
810 | * Checks whether there is a loop in the authorization item hierarchy. |
||
811 | * @param Item $parent the parent item |
||
812 | * @param Item $child the child item to be added to the hierarchy |
||
813 | * @return bool whether a loop exists |
||
814 | */ |
||
815 | protected function detectLoop($parent, $child) |
||
827 | |||
828 | /** |
||
829 | * @inheritdoc |
||
830 | */ |
||
831 | public function assign($role, $userId) |
||
848 | |||
849 | /** |
||
850 | * @inheritdoc |
||
851 | */ |
||
852 | public function revoke($role, $userId) |
||
862 | |||
863 | /** |
||
864 | * @inheritdoc |
||
865 | */ |
||
866 | public function revokeAll($userId) |
||
876 | |||
877 | /** |
||
878 | * @inheritdoc |
||
879 | */ |
||
880 | public function removeAll() |
||
888 | |||
889 | /** |
||
890 | * @inheritdoc |
||
891 | */ |
||
892 | public function removeAllPermissions() |
||
896 | |||
897 | /** |
||
898 | * @inheritdoc |
||
899 | */ |
||
900 | public function removeAllRoles() |
||
904 | |||
905 | /** |
||
906 | * Removes all auth items of the specified type. |
||
907 | * @param int $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
908 | */ |
||
909 | protected function removeAllItems($type) |
||
934 | |||
935 | /** |
||
936 | * @inheritdoc |
||
937 | */ |
||
938 | public function removeAllRules() |
||
950 | |||
951 | /** |
||
952 | * @inheritdoc |
||
953 | */ |
||
954 | public function removeAllAssignments() |
||
958 | |||
959 | public function invalidateCache() |
||
968 | |||
969 | public function loadFromCache() |
||
1003 | |||
1004 | /** |
||
1005 | * Returns all role assignment information for the specified role. |
||
1006 | * @param string $roleName |
||
1007 | * @return Assignment[] the assignments. An empty array will be |
||
1008 | * returned if role is not assigned to any user. |
||
1009 | * @since 2.0.7 |
||
1010 | */ |
||
1011 | public function getUserIdsByRole($roleName) |
||
1021 | } |
||
1022 |
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..