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 |
||
37 | class DbManager extends BaseManager |
||
38 | { |
||
39 | /** |
||
40 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
41 | * After the DbManager object is created, if you want to change this property, you should only assign it |
||
42 | * with a DB connection object. |
||
43 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
44 | */ |
||
45 | public $db = 'db'; |
||
46 | /** |
||
47 | * @var string the name of the table storing authorization items. Defaults to "auth_item". |
||
48 | */ |
||
49 | public $itemTable = '{{%auth_item}}'; |
||
50 | /** |
||
51 | * @var string the name of the table storing authorization item hierarchy. Defaults to "auth_item_child". |
||
52 | */ |
||
53 | public $itemChildTable = '{{%auth_item_child}}'; |
||
54 | /** |
||
55 | * @var string the name of the table storing authorization item assignments. Defaults to "auth_assignment". |
||
56 | */ |
||
57 | public $assignmentTable = '{{%auth_assignment}}'; |
||
58 | /** |
||
59 | * @var string the name of the table storing rules. Defaults to "auth_rule". |
||
60 | */ |
||
61 | public $ruleTable = '{{%auth_rule}}'; |
||
62 | /** |
||
63 | * @var Cache|array|string the cache used to improve RBAC performance. This can be one of the following: |
||
64 | * |
||
65 | * - an application component ID (e.g. `cache`) |
||
66 | * - a configuration array |
||
67 | * - a [[\yii\caching\Cache]] object |
||
68 | * |
||
69 | * When this is not set, it means caching is not enabled. |
||
70 | * |
||
71 | * Note that by enabling RBAC cache, all auth items, rules and auth item parent-child relationships will |
||
72 | * be cached and loaded into memory. This will improve the performance of RBAC permission check. However, |
||
73 | * it does require extra memory and as a result may not be appropriate if your RBAC system contains too many |
||
74 | * auth items. You should seek other RBAC implementations (e.g. RBAC based on Redis storage) in this case. |
||
75 | * |
||
76 | * Also note that if you modify RBAC items, rules or parent-child relationships from outside of this component, |
||
77 | * you have to manually call [[invalidateCache()]] to ensure data consistency. |
||
78 | * |
||
79 | * @since 2.0.3 |
||
80 | */ |
||
81 | public $cache; |
||
82 | /** |
||
83 | * @var string the key used to store RBAC data in cache |
||
84 | * @see cache |
||
85 | * @since 2.0.3 |
||
86 | */ |
||
87 | public $cacheKey = 'rbac'; |
||
88 | |||
89 | /** |
||
90 | * @var Item[] all auth items (name => Item) |
||
91 | */ |
||
92 | protected $items; |
||
93 | /** |
||
94 | * @var Rule[] all auth rules (name => Rule) |
||
95 | */ |
||
96 | protected $rules; |
||
97 | /** |
||
98 | * @var array auth item parent-child relationships (childName => list of parents) |
||
99 | */ |
||
100 | protected $parents; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Initializes the application component. |
||
105 | * This method overrides the parent implementation by establishing the database connection. |
||
106 | */ |
||
107 | 88 | public function init() |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 8 | public function checkAccess($userId, $permissionName, $params = []) |
|
129 | |||
130 | /** |
||
131 | * Performs access check for the specified user based on the data loaded from cache. |
||
132 | * This method is internally called by [[checkAccess()]] when [[cache]] is enabled. |
||
133 | * @param string|integer $user the user ID. This should can be either an integer or a string representing |
||
134 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
135 | * @param string $itemName the name of the operation that need access check |
||
136 | * @param array $params name-value pairs that would be passed to rules associated |
||
137 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
138 | * which holds the value of `$userId`. |
||
139 | * @param Assignment[] $assignments the assignments to the specified user |
||
140 | * @return boolean whether the operations can be performed by the user. |
||
141 | * @since 2.0.3 |
||
142 | */ |
||
143 | 2 | protected function checkAccessFromCache($user, $itemName, $params, $assignments) |
|
171 | |||
172 | /** |
||
173 | * Performs access check for the specified user. |
||
174 | * This method is internally called by [[checkAccess()]]. |
||
175 | * @param string|integer $user the user ID. This should can be either an integer or a string representing |
||
176 | * the unique identifier of a user. See [[\yii\web\User::id]]. |
||
177 | * @param string $itemName the name of the operation that need access check |
||
178 | * @param array $params name-value pairs that would be passed to rules associated |
||
179 | * with the tasks and roles assigned to the user. A param with name 'user' is added to this array, |
||
180 | * which holds the value of `$userId`. |
||
181 | * @param Assignment[] $assignments the assignments to the specified user |
||
182 | * @return boolean whether the operations can be performed by the user. |
||
183 | */ |
||
184 | 6 | protected function checkAccessRecursive($user, $itemName, $params, $assignments) |
|
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 35 | protected function getItem($name) |
|
241 | |||
242 | /** |
||
243 | * Returns a value indicating whether the database supports cascading update and delete. |
||
244 | * The default implementation will return false for SQLite database and true for all other databases. |
||
245 | * @return boolean whether the database supports cascading update and delete. |
||
246 | */ |
||
247 | 24 | protected function supportsCascadeUpdate() |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | 80 | protected function addItem($item) |
|
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | */ |
||
283 | 4 | protected function removeItem($item) |
|
302 | |||
303 | /** |
||
304 | * @inheritdoc |
||
305 | */ |
||
306 | 8 | protected function updateItem($name, $item) |
|
337 | |||
338 | /** |
||
339 | * @inheritdoc |
||
340 | */ |
||
341 | 76 | protected function addRule($rule) |
|
362 | |||
363 | /** |
||
364 | * @inheritdoc |
||
365 | */ |
||
366 | 4 | protected function updateRule($name, $rule) |
|
389 | |||
390 | /** |
||
391 | * @inheritdoc |
||
392 | */ |
||
393 | 4 | protected function removeRule($rule) |
|
409 | |||
410 | /** |
||
411 | * @inheritdoc |
||
412 | */ |
||
413 | 16 | protected function getItems($type) |
|
426 | |||
427 | /** |
||
428 | * Populates an auth item with the data fetched from database |
||
429 | * @param array $row the data from the auth item table |
||
430 | * @return Item the populated auth item instance (either Role or Permission) |
||
431 | */ |
||
432 | 76 | protected function populateItem($row) |
|
450 | |||
451 | /** |
||
452 | * @inheritdoc |
||
453 | */ |
||
454 | 8 | public function getRolesByUser($userId) |
|
472 | |||
473 | /** |
||
474 | * @inheritdoc |
||
475 | */ |
||
476 | 4 | public function getChildRoles($roleName) |
|
495 | |||
496 | /** |
||
497 | * @inheritdoc |
||
498 | */ |
||
499 | 4 | public function getPermissionsByRole($roleName) |
|
517 | |||
518 | /** |
||
519 | * @inheritdoc |
||
520 | */ |
||
521 | 4 | public function getPermissionsByUser($userId) |
|
532 | |||
533 | /** |
||
534 | * Returns all permissions that are directly assigned to user. |
||
535 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
536 | * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names. |
||
537 | * @since 2.0.7 |
||
538 | */ |
||
539 | 4 | protected function getDirectPermissionsByUser($userId) |
|
553 | |||
554 | /** |
||
555 | * Returns all permissions that the user inherits from the roles assigned to him. |
||
556 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
557 | * @return Permission[] all inherited permissions that the user has. The array is indexed by the permission names. |
||
558 | * @since 2.0.7 |
||
559 | */ |
||
560 | 4 | protected function getInheritedPermissionsByUser($userId) |
|
586 | |||
587 | /** |
||
588 | * Returns the children for every parent. |
||
589 | * @return array the children list. Each array key is a parent item name, |
||
590 | * and the corresponding array value is a list of child item names. |
||
591 | */ |
||
592 | 12 | protected function getChildrenList() |
|
601 | |||
602 | /** |
||
603 | * Recursively finds all children and grand children of the specified item. |
||
604 | * @param string $name the name of the item whose children are to be looked for. |
||
605 | * @param array $childrenList the child list built via [[getChildrenList()]] |
||
606 | * @param array $result the children and grand children (in array keys) |
||
607 | */ |
||
608 | 12 | protected function getChildrenRecursive($name, $childrenList, &$result) |
|
617 | |||
618 | /** |
||
619 | * @inheritdoc |
||
620 | */ |
||
621 | 72 | public function getRule($name) |
|
633 | |||
634 | /** |
||
635 | * @inheritdoc |
||
636 | */ |
||
637 | 20 | public function getRules() |
|
652 | |||
653 | /** |
||
654 | * @inheritdoc |
||
655 | */ |
||
656 | public function getAssignment($roleName, $userId) |
||
676 | |||
677 | /** |
||
678 | * @inheritdoc |
||
679 | */ |
||
680 | 12 | public function getAssignments($userId) |
|
701 | |||
702 | /** |
||
703 | * @inheritdoc |
||
704 | * @since 2.0.8 |
||
705 | */ |
||
706 | 4 | public function canAddChild($parent, $child) |
|
710 | |||
711 | /** |
||
712 | * @inheritdoc |
||
713 | */ |
||
714 | 72 | public function addChild($parent, $child) |
|
736 | |||
737 | /** |
||
738 | * @inheritdoc |
||
739 | */ |
||
740 | public function removeChild($parent, $child) |
||
750 | |||
751 | /** |
||
752 | * @inheritdoc |
||
753 | */ |
||
754 | public function removeChildren($parent) |
||
764 | |||
765 | /** |
||
766 | * @inheritdoc |
||
767 | */ |
||
768 | public function hasChild($parent, $child) |
||
775 | |||
776 | /** |
||
777 | * @inheritdoc |
||
778 | */ |
||
779 | 72 | public function getChildren($name) |
|
793 | |||
794 | /** |
||
795 | * Checks whether there is a loop in the authorization item hierarchy. |
||
796 | * @param Item $parent the parent item |
||
797 | * @param Item $child the child item to be added to the hierarchy |
||
798 | * @return boolean whether a loop exists |
||
799 | */ |
||
800 | 72 | protected function detectLoop($parent, $child) |
|
812 | |||
813 | /** |
||
814 | * @inheritdoc |
||
815 | */ |
||
816 | 72 | public function assign($role, $userId) |
|
833 | |||
834 | /** |
||
835 | * @inheritdoc |
||
836 | */ |
||
837 | public function revoke($role, $userId) |
||
847 | |||
848 | /** |
||
849 | * @inheritdoc |
||
850 | */ |
||
851 | public function revokeAll($userId) |
||
861 | |||
862 | /** |
||
863 | * @inheritdoc |
||
864 | */ |
||
865 | 88 | public function removeAll() |
|
873 | |||
874 | /** |
||
875 | * @inheritdoc |
||
876 | */ |
||
877 | 4 | public function removeAllPermissions() |
|
881 | |||
882 | /** |
||
883 | * @inheritdoc |
||
884 | */ |
||
885 | 4 | public function removeAllRoles() |
|
889 | |||
890 | /** |
||
891 | * Removes all auth items of the specified type. |
||
892 | * @param integer $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
893 | */ |
||
894 | 8 | protected function removeAllItems($type) |
|
919 | |||
920 | /** |
||
921 | * @inheritdoc |
||
922 | */ |
||
923 | 4 | public function removeAllRules() |
|
935 | |||
936 | /** |
||
937 | * @inheritdoc |
||
938 | */ |
||
939 | 88 | public function removeAllAssignments() |
|
943 | |||
944 | 88 | public function invalidateCache() |
|
953 | |||
954 | 8 | public function loadFromCache() |
|
988 | |||
989 | /** |
||
990 | * Returns all role assignment information for the specified role. |
||
991 | * @param string $roleName |
||
992 | * @return Assignment[] the assignments. An empty array will be |
||
993 | * returned if role is not assigned to any user. |
||
994 | * @since 2.0.7 |
||
995 | */ |
||
996 | 4 | public function getUserIdsByRole($roleName) |
|
1006 | } |
||
1007 |
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..