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 | 84 | 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 | 31 | 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 | 76 | 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 | 72 | 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 | 12 | 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 | 72 | protected function populateItem($row) |
|
450 | |||
451 | /** |
||
452 | * @inheritdoc |
||
453 | */ |
||
454 | 8 | public function getRolesByUser($userId) |
|
472 | |||
473 | /** |
||
474 | * @inheritdoc |
||
475 | */ |
||
476 | 4 | public function getPermissionsByRole($roleName) |
|
494 | |||
495 | /** |
||
496 | * @inheritdoc |
||
497 | */ |
||
498 | 4 | public function getPermissionsByUser($userId) |
|
509 | |||
510 | /** |
||
511 | * Returns all permissions that are directly assigned to user. |
||
512 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
513 | * @return Permission[] all direct permissions that the user has. The array is indexed by the permission names. |
||
514 | * @since 2.0.7 |
||
515 | */ |
||
516 | 4 | protected function getDirectPermissionsByUser($userId) |
|
530 | |||
531 | /** |
||
532 | * Returns all permissions that the user inherits from the roles assigned to him. |
||
533 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
534 | * @return Permission[] all inherited permissions that the user has. The array is indexed by the permission names. |
||
535 | * @since 2.0.7 |
||
536 | */ |
||
537 | 4 | protected function getInheritedPermissionsByUser($userId) |
|
538 | { |
||
539 | 4 | $query = (new Query)->select('item_name') |
|
540 | 4 | ->from($this->assignmentTable) |
|
541 | 4 | ->where(['user_id' => (string) $userId]); |
|
542 | |||
543 | 4 | $childrenList = $this->getChildrenList(); |
|
544 | 4 | $result = []; |
|
545 | 4 | foreach ($query->column($this->db) as $roleName) { |
|
546 | 4 | $this->getChildrenRecursive($roleName, $childrenList, $result); |
|
547 | 4 | } |
|
548 | |||
549 | 4 | if (empty($result)) { |
|
550 | return []; |
||
551 | } |
||
552 | |||
553 | 4 | $query = (new Query)->from($this->itemTable)->where([ |
|
554 | 4 | 'type' => Item::TYPE_PERMISSION, |
|
555 | 4 | 'name' => array_keys($result), |
|
556 | 4 | ]); |
|
557 | 4 | $permissions = []; |
|
558 | 4 | foreach ($query->all($this->db) as $row) { |
|
559 | 4 | $permissions[$row['name']] = $this->populateItem($row); |
|
560 | 4 | } |
|
561 | 4 | return $permissions; |
|
562 | } |
||
563 | |||
564 | /** |
||
565 | * Returns the children for every parent. |
||
566 | * @return array the children list. Each array key is a parent item name, |
||
567 | * and the corresponding array value is a list of child item names. |
||
568 | */ |
||
569 | 8 | protected function getChildrenList() |
|
578 | |||
579 | /** |
||
580 | * Recursively finds all children and grand children of the specified item. |
||
581 | * @param string $name the name of the item whose children are to be looked for. |
||
582 | * @param array $childrenList the child list built via [[getChildrenList()]] |
||
583 | * @param array $result the children and grand children (in array keys) |
||
584 | */ |
||
585 | 8 | protected function getChildrenRecursive($name, $childrenList, &$result) |
|
594 | |||
595 | /** |
||
596 | * @inheritdoc |
||
597 | */ |
||
598 | 68 | public function getRule($name) |
|
610 | |||
611 | /** |
||
612 | * @inheritdoc |
||
613 | */ |
||
614 | 20 | public function getRules() |
|
629 | |||
630 | /** |
||
631 | * @inheritdoc |
||
632 | */ |
||
633 | public function getAssignment($roleName, $userId) |
||
634 | { |
||
635 | if (empty($userId)) { |
||
636 | return null; |
||
637 | } |
||
638 | |||
639 | $row = (new Query)->from($this->assignmentTable) |
||
640 | ->where(['user_id' => (string) $userId, 'item_name' => $roleName]) |
||
641 | ->one($this->db); |
||
642 | |||
643 | if ($row === false) { |
||
644 | return null; |
||
645 | } |
||
646 | |||
647 | return new Assignment([ |
||
648 | 'userId' => $row['user_id'], |
||
649 | 'roleName' => $row['item_name'], |
||
650 | 'createdAt' => $row['created_at'], |
||
651 | ]); |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * @inheritdoc |
||
656 | */ |
||
657 | 12 | public function getAssignments($userId) |
|
658 | { |
||
659 | 12 | if (empty($userId)) { |
|
660 | 4 | return []; |
|
661 | } |
||
662 | |||
663 | 12 | $query = (new Query) |
|
664 | 12 | ->from($this->assignmentTable) |
|
665 | 12 | ->where(['user_id' => (string) $userId]); |
|
666 | |||
667 | 12 | $assignments = []; |
|
668 | 12 | foreach ($query->all($this->db) as $row) { |
|
669 | 12 | $assignments[$row['item_name']] = new Assignment([ |
|
670 | 12 | 'userId' => $row['user_id'], |
|
671 | 12 | 'roleName' => $row['item_name'], |
|
672 | 12 | 'createdAt' => $row['created_at'], |
|
673 | 12 | ]); |
|
674 | 12 | } |
|
675 | |||
676 | 12 | return $assignments; |
|
677 | } |
||
678 | |||
679 | /** |
||
680 | * @inheritdoc |
||
681 | * @since 2.0.8 |
||
682 | */ |
||
683 | 4 | public function canAddChild($parent, $child) |
|
687 | |||
688 | /** |
||
689 | * @inheritdoc |
||
690 | */ |
||
691 | 68 | public function addChild($parent, $child) |
|
713 | |||
714 | /** |
||
715 | * @inheritdoc |
||
716 | */ |
||
717 | public function removeChild($parent, $child) |
||
727 | |||
728 | /** |
||
729 | * @inheritdoc |
||
730 | */ |
||
731 | public function removeChildren($parent) |
||
741 | |||
742 | /** |
||
743 | * @inheritdoc |
||
744 | */ |
||
745 | public function hasChild($parent, $child) |
||
752 | |||
753 | /** |
||
754 | * @inheritdoc |
||
755 | */ |
||
756 | 68 | public function getChildren($name) |
|
770 | |||
771 | /** |
||
772 | * Checks whether there is a loop in the authorization item hierarchy. |
||
773 | * @param Item $parent the parent item |
||
774 | * @param Item $child the child item to be added to the hierarchy |
||
775 | * @return boolean whether a loop exists |
||
776 | */ |
||
777 | 68 | protected function detectLoop($parent, $child) |
|
789 | |||
790 | /** |
||
791 | * @inheritdoc |
||
792 | */ |
||
793 | 68 | public function assign($role, $userId) |
|
810 | |||
811 | /** |
||
812 | * @inheritdoc |
||
813 | */ |
||
814 | public function revoke($role, $userId) |
||
815 | { |
||
816 | if (empty($userId)) { |
||
817 | return false; |
||
818 | } |
||
819 | |||
820 | return $this->db->createCommand() |
||
821 | ->delete($this->assignmentTable, ['user_id' => (string) $userId, 'item_name' => $role->name]) |
||
822 | ->execute() > 0; |
||
823 | } |
||
824 | |||
825 | /** |
||
826 | * @inheritdoc |
||
827 | */ |
||
828 | public function revokeAll($userId) |
||
829 | { |
||
830 | if (empty($userId)) { |
||
831 | return false; |
||
832 | } |
||
833 | |||
834 | return $this->db->createCommand() |
||
835 | ->delete($this->assignmentTable, ['user_id' => (string) $userId]) |
||
836 | ->execute() > 0; |
||
837 | } |
||
838 | |||
839 | /** |
||
840 | * @inheritdoc |
||
841 | */ |
||
842 | 84 | public function removeAll() |
|
850 | |||
851 | /** |
||
852 | * @inheritdoc |
||
853 | */ |
||
854 | 4 | public function removeAllPermissions() |
|
858 | |||
859 | /** |
||
860 | * @inheritdoc |
||
861 | */ |
||
862 | 4 | public function removeAllRoles() |
|
866 | |||
867 | /** |
||
868 | * Removes all auth items of the specified type. |
||
869 | * @param integer $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
870 | */ |
||
871 | 8 | protected function removeAllItems($type) |
|
896 | |||
897 | /** |
||
898 | * @inheritdoc |
||
899 | */ |
||
900 | 4 | public function removeAllRules() |
|
912 | |||
913 | /** |
||
914 | * @inheritdoc |
||
915 | */ |
||
916 | 84 | public function removeAllAssignments() |
|
920 | |||
921 | 84 | public function invalidateCache() |
|
930 | |||
931 | 8 | public function loadFromCache() |
|
965 | |||
966 | /** |
||
967 | * Returns all role assignment information for the specified role. |
||
968 | * @param string $roleName |
||
969 | * @return Assignment[] the assignments. An empty array will be |
||
970 | * returned if role is not assigned to any user. |
||
971 | * @since 2.0.7 |
||
972 | */ |
||
973 | 4 | public function getUserIdsByRole($roleName) |
|
983 | } |
||
984 |
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..