Complex classes like UserTypesBehavior 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 UserTypesBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class UserTypesBehavior extends Behavior |
||
22 | { |
||
23 | /** |
||
24 | * @var UserTypeQuery|null |
||
25 | */ |
||
26 | private $userTypes; |
||
27 | |||
28 | /** |
||
29 | * @param array $criteria |
||
30 | * @return UserTypeQuery |
||
31 | */ |
||
32 | public function userTypeQuery($criteria = []): UserTypeQuery |
||
46 | |||
47 | /** |
||
48 | * Get a query with associated user types |
||
49 | * |
||
50 | * @param array $criteria |
||
51 | * @return UserTypeQuery |
||
52 | */ |
||
53 | public function getUserTypes($criteria = []): UserTypeQuery |
||
68 | |||
69 | /** |
||
70 | * Set an array or query of user types to a user |
||
71 | * |
||
72 | * @param $userTypes |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function setUserTypes($userTypes) |
||
88 | |||
89 | /** |
||
90 | * Add an array of user types to a user. Note: This does not save the user type associations. |
||
91 | * |
||
92 | * @param $types |
||
93 | * @return $this |
||
94 | */ |
||
95 | protected function addUserTypes(array $types) |
||
116 | |||
117 | /** |
||
118 | * Add a user type to a user. Note: This does not save the user type associations. |
||
119 | * |
||
120 | * @param UserType $type |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function addUserType(UserType $type) |
||
133 | |||
134 | /** |
||
135 | * @param mixed $type |
||
136 | * @return UserType |
||
137 | */ |
||
138 | protected function resolveUserType($type): UserType |
||
150 | |||
151 | /******************************************* |
||
152 | * ASSOCIATE and/or DISASSOCIATE |
||
153 | *******************************************/ |
||
154 | |||
155 | /** |
||
156 | * @param UserTypeQuery $query |
||
157 | * @param Organization $organization |
||
158 | * @return bool |
||
159 | * @throws \Throwable |
||
160 | * @throws \yii\db\StaleObjectException |
||
161 | */ |
||
162 | public function saveUserTypes(UserTypeQuery $query, Organization $organization): bool |
||
228 | |||
229 | /** |
||
230 | * @param UserTypeQuery $query |
||
231 | * @param Organization $organization |
||
232 | * @return bool |
||
233 | * @throws \Throwable |
||
234 | */ |
||
235 | public function associateUserTypes(UserTypeQuery $query, Organization $organization): bool |
||
275 | |||
276 | /** |
||
277 | * @param UserTypeQuery $query |
||
278 | * @param Organization $organization |
||
279 | * @return bool |
||
280 | * @throws \Throwable |
||
281 | */ |
||
282 | public function dissociateUserTypes(UserTypeQuery $query, Organization $organization): bool |
||
320 | |||
321 | /** |
||
322 | * @param int $userId |
||
323 | * @param int $organizationId |
||
324 | * @return Query |
||
325 | */ |
||
326 | protected function associationIdQuery(int $userId, int $organizationId): Query |
||
336 | |||
337 | /** |
||
338 | * @param int $userId |
||
339 | * @param int $organizationId |
||
340 | * @return string|null |
||
341 | */ |
||
342 | protected function associationId(int $userId, int $organizationId) |
||
347 | } |
||
348 |
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.