Complex classes like FeatureAbstract 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 FeatureAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class FeatureAbstract implements RolloutableInterface |
||
15 | { |
||
16 | |||
17 | const FEATURE_CONFIGSTRING_SECTION_DELIMITER = '|'; |
||
18 | |||
19 | const FEATURE_CONFIGSTRING_ENTRY_DELIMITER = ','; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $name = ''; |
||
25 | |||
26 | /** |
||
27 | * @var integer |
||
28 | */ |
||
29 | protected $percentage = 0; |
||
30 | |||
31 | /** |
||
32 | * @var integer[] |
||
33 | */ |
||
34 | protected $users = array(); |
||
35 | |||
36 | /** |
||
37 | * @var string[] |
||
38 | */ |
||
39 | protected $roles = array(); |
||
40 | |||
41 | /** |
||
42 | * @var string[] |
||
43 | */ |
||
44 | protected $groups = array(); |
||
45 | |||
46 | /** |
||
47 | * Configure the feature with a single string |
||
48 | * |
||
49 | * Format of config string should be: "100|1,2,3,4|ROLE_ADMIN,ROLE_PREMIUM|caretaker,supporter,staff" |
||
50 | * - where "100" is the percentage of user for that the feature should be enabled. |
||
51 | * - where "1,2,3,4" are a comma-separated list of user IDs that should have this feature. |
||
52 | * - where "ROLE_ADMIN,ROLE_PREMIUM" are a comma-separated list of the role names that should have this feature. |
||
53 | * - where "caretaker,supporter,staff" are a comma-separated list of the role names that should have this feature. |
||
54 | * |
||
55 | * Empty section are allowed and silently ignored as long as the format of the string stays the same: |
||
56 | * e.g. "20||ROLE_PREMIUM|" is valid (20 percent and additionally al users with ROLE_PREMIUM will get the feature) |
||
57 | * e.g. "|||" is valid and will completely disable this feature, but it is recommend to use "0|||" instead. |
||
58 | * |
||
59 | * @param string $configString |
||
60 | * @return bool Successfully parsed the config string or not |
||
61 | */ |
||
62 | public function configureByConfigString($configString) |
||
104 | |||
105 | /** |
||
106 | * Resets the feature config |
||
107 | * @return RolloutableInterface $this |
||
108 | */ |
||
109 | public function clearConfig() |
||
117 | |||
118 | /** |
||
119 | * Check if this feature is active for the given userId |
||
120 | * @param RolloutAbstract $rollout |
||
121 | * @param DeterminableUserInterface|null $user |
||
122 | * @internal param int $userId |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function isActive(RolloutAbstract $rollout, DeterminableUserInterface $user = null) |
||
149 | |||
150 | /** |
||
151 | * @param integer $userId |
||
152 | * @return bool |
||
153 | */ |
||
154 | protected function isUserInPercentage($userId) |
||
159 | |||
160 | /** |
||
161 | * @param integer $userId |
||
162 | * @return bool |
||
163 | */ |
||
164 | protected function isUserInActiveUsers($userId) |
||
168 | |||
169 | /** |
||
170 | * @param DeterminableUserInterface $user |
||
171 | * @param RolloutAbstract $rollout |
||
172 | * @return bool |
||
173 | */ |
||
174 | protected function isUserInActiveRole(DeterminableUserInterface $user, RolloutAbstract $rollout) |
||
183 | |||
184 | /** |
||
185 | * @param DeterminableUserInterface $user |
||
186 | * @param RolloutAbstract $rollout |
||
187 | * @return bool |
||
188 | */ |
||
189 | protected function isUserInActiveGroup(DeterminableUserInterface $user, RolloutAbstract $rollout) |
||
198 | |||
199 | /** |
||
200 | * Returns the name of the feature as string. A features name has to be unique! |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getName() |
||
207 | |||
208 | /** |
||
209 | * Returns the percentage of users that should be enabled |
||
210 | * @return integer |
||
211 | */ |
||
212 | public function getPercentage() |
||
216 | |||
217 | /** |
||
218 | * Returns an array of user IDs that should be explicitly enabled for this feature |
||
219 | * @return integer[] |
||
220 | */ |
||
221 | public function getUsers() |
||
225 | |||
226 | /** |
||
227 | * Returns an array of role names that should be enabled for this feature |
||
228 | * @return string[] |
||
229 | */ |
||
230 | public function getRoles() |
||
234 | |||
235 | /** |
||
236 | * Returns an array of group names that should be enabled for this feature |
||
237 | * @return string[] |
||
238 | */ |
||
239 | public function getGroups() |
||
243 | |||
244 | /** |
||
245 | * Set the percentage of users that should be enabled |
||
246 | * @param integer $percentage |
||
247 | * @return RolloutableInterface $this |
||
248 | */ |
||
249 | public function setPercentage($percentage) |
||
254 | |||
255 | /** |
||
256 | * Sets the array of user IDs that should be explicitly enabled for this feature |
||
257 | * @param integer[] $userIds |
||
258 | * @return RolloutableInterface $this |
||
259 | */ |
||
260 | public function setUsers(array $userIds) |
||
265 | |||
266 | /** |
||
267 | * Adds a user to the list of user IDs that should be explicitly enabled for this feature |
||
268 | * @param integer $userId |
||
269 | * @return RolloutableInterface $this |
||
270 | */ |
||
271 | public function addUser($userId) |
||
278 | |||
279 | /** |
||
280 | * Removes a user from the list of user IDs that should be explicitly enabled for this feature |
||
281 | * @param integer $userId |
||
282 | * @return RolloutableInterface $this |
||
283 | */ |
||
284 | public function removeUser($userId) |
||
293 | |||
294 | /** |
||
295 | * Sets the array of role names that should be enabled for this feature |
||
296 | * @param string[] $roles |
||
297 | * @return RolloutableInterface $this |
||
298 | */ |
||
299 | public function setRoles(array $roles) |
||
304 | |||
305 | /** |
||
306 | * Adds a role to the list of role names that should be enabled for this feature |
||
307 | * @param string $roleName |
||
308 | * @return RolloutableInterface $this |
||
309 | */ |
||
310 | public function addRole($roleName) |
||
317 | |||
318 | /** |
||
319 | * Removes a role from the list of role names that should be enabled for this feature |
||
320 | * @param string $roleName |
||
321 | * @return RolloutableInterface $this |
||
322 | */ |
||
323 | public function removeRole($roleName) |
||
332 | |||
333 | /** |
||
334 | * Sets the array of group names that should be enabled for this feature |
||
335 | * @param string[] $groups |
||
336 | * @return RolloutableInterface $this |
||
337 | */ |
||
338 | public function setGroups(array $groups) |
||
343 | |||
344 | /** |
||
345 | * Adds a group to the list of group names that should be enabled for this feature |
||
346 | * @param string $groupName |
||
347 | * @return RolloutableInterface $this |
||
348 | */ |
||
349 | public function addGroup($groupName) |
||
356 | |||
357 | /** |
||
358 | * Removes a group from the list of group names that should be enabled for this feature |
||
359 | * @param string $groupName |
||
360 | * @return RolloutableInterface $this |
||
361 | */ |
||
362 | public function removeGroup($groupName) |
||
371 | |||
372 | /** |
||
373 | * Checks the user IDs and returns only the valid ones |
||
374 | * @param array $userIds |
||
375 | * @return array $checkedUserIds |
||
376 | */ |
||
377 | protected function checkUserIds(array $userIds) |
||
387 | |||
388 | /** |
||
389 | * Checks the roles and returns only the valid ones |
||
390 | * @param array $roles |
||
391 | * @return array $checkedRoles |
||
392 | */ |
||
393 | protected function checkRoles(array $roles) |
||
403 | |||
404 | /** |
||
405 | * Checks the groups and returns only the valid ones |
||
406 | * @param array $groups |
||
407 | * @return array $checkedGroups |
||
408 | */ |
||
409 | protected function checkGroups(array $groups) |
||
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | public function __toString() |
||
435 | } |
||
436 |