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 | 64 | public function init() |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 4 | 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 | 1 | 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 | 3 | protected function checkAccessRecursive($user, $itemName, $params, $assignments) |
|
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 15 | 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 | 8 | protected function supportsCascadeUpdate() |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | 56 | protected function addItem($item) |
|
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | */ |
||
283 | protected function removeItem($item) |
||
302 | |||
303 | /** |
||
304 | * @inheritdoc |
||
305 | */ |
||
306 | protected function updateItem($name, $item) |
||
337 | |||
338 | /** |
||
339 | * @inheritdoc |
||
340 | */ |
||
341 | 52 | 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 | 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 | 52 | 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 | * |
||
515 | * @since 2.0.7 |
||
516 | */ |
||
517 | 4 | protected function getDirectPermissionsByUser($userId) |
|
531 | |||
532 | /** |
||
533 | * Returns all permissions that the user inherits from the roles assigned to him. |
||
534 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
535 | * @return Permission[] all inherited permissions that the user has. The array is indexed by the permission names. |
||
536 | * |
||
537 | * @since 2.0.7 |
||
538 | */ |
||
539 | 4 | protected function getInheritedPermissionsByUser($userId) |
|
565 | |||
566 | /** |
||
567 | * Returns the children for every parent. |
||
568 | * @return array the children list. Each array key is a parent item name, |
||
569 | * and the corresponding array value is a list of child item names. |
||
570 | */ |
||
571 | 8 | protected function getChildrenList() |
|
580 | |||
581 | /** |
||
582 | * Recursively finds all children and grand children of the specified item. |
||
583 | * @param string $name the name of the item whose children are to be looked for. |
||
584 | * @param array $childrenList the child list built via [[getChildrenList()]] |
||
585 | * @param array $result the children and grand children (in array keys) |
||
586 | */ |
||
587 | 8 | protected function getChildrenRecursive($name, $childrenList, &$result) |
|
596 | |||
597 | /** |
||
598 | * @inheritdoc |
||
599 | */ |
||
600 | 20 | public function getRule($name) |
|
612 | |||
613 | /** |
||
614 | * @inheritdoc |
||
615 | */ |
||
616 | 8 | public function getRules() |
|
631 | |||
632 | /** |
||
633 | * @inheritdoc |
||
634 | */ |
||
635 | public function getAssignment($roleName, $userId) |
||
655 | |||
656 | /** |
||
657 | * @inheritdoc |
||
658 | */ |
||
659 | 8 | public function getAssignments($userId) |
|
680 | |||
681 | /** |
||
682 | * @inheritdoc |
||
683 | */ |
||
684 | 52 | public function addChild($parent, $child) |
|
706 | |||
707 | /** |
||
708 | * @inheritdoc |
||
709 | */ |
||
710 | public function removeChild($parent, $child) |
||
720 | |||
721 | /** |
||
722 | * @inheritdoc |
||
723 | */ |
||
724 | public function removeChildren($parent) |
||
734 | |||
735 | /** |
||
736 | * @inheritdoc |
||
737 | */ |
||
738 | public function hasChild($parent, $child) |
||
745 | |||
746 | /** |
||
747 | * @inheritdoc |
||
748 | */ |
||
749 | 52 | public function getChildren($name) |
|
763 | |||
764 | /** |
||
765 | * Checks whether there is a loop in the authorization item hierarchy. |
||
766 | * @param Item $parent the parent item |
||
767 | * @param Item $child the child item to be added to the hierarchy |
||
768 | * @return boolean whether a loop exists |
||
769 | */ |
||
770 | 52 | protected function detectLoop($parent, $child) |
|
782 | |||
783 | /** |
||
784 | * @inheritdoc |
||
785 | */ |
||
786 | 48 | public function assign($role, $userId) |
|
803 | |||
804 | /** |
||
805 | * @inheritdoc |
||
806 | */ |
||
807 | public function revoke($role, $userId) |
||
817 | |||
818 | /** |
||
819 | * @inheritdoc |
||
820 | */ |
||
821 | public function revokeAll($userId) |
||
831 | |||
832 | /** |
||
833 | * @inheritdoc |
||
834 | */ |
||
835 | 64 | public function removeAll() |
|
843 | |||
844 | /** |
||
845 | * @inheritdoc |
||
846 | */ |
||
847 | public function removeAllPermissions() |
||
851 | |||
852 | /** |
||
853 | * @inheritdoc |
||
854 | */ |
||
855 | public function removeAllRoles() |
||
859 | |||
860 | /** |
||
861 | * Removes all auth items of the specified type. |
||
862 | * @param integer $type the auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
||
863 | */ |
||
864 | protected function removeAllItems($type) |
||
889 | |||
890 | /** |
||
891 | * @inheritdoc |
||
892 | */ |
||
893 | public function removeAllRules() |
||
905 | |||
906 | /** |
||
907 | * @inheritdoc |
||
908 | */ |
||
909 | 64 | public function removeAllAssignments() |
|
913 | |||
914 | 64 | public function invalidateCache() |
|
923 | |||
924 | 4 | public function loadFromCache() |
|
958 | |||
959 | /** |
||
960 | * Returns all role assignment information for the specified role. |
||
961 | * @param string $roleName |
||
962 | * @return Assignment[] the assignments. An empty array will be |
||
963 | * returned if role is not assigned to any user. |
||
964 | */ |
||
965 | 4 | public function getUserIDsByRole($roleName) |
|
975 | } |
||
976 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.