Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Config |
||
13 | { |
||
14 | /* Keys used in a feature configuration. */ |
||
15 | const DESCRIPTION = 'description'; |
||
16 | const ENABLED = 'enabled'; |
||
17 | const USERS = 'users'; |
||
18 | const GROUPS = 'groups'; |
||
19 | const ADMIN = 'admin'; |
||
20 | const INTERNAL = 'internal'; |
||
21 | const PUBLIC_URL_OVERRIDE = 'public_url_override'; |
||
22 | const BUCKETING = 'bucketing'; |
||
23 | |||
24 | /* Special values for enabled property. */ |
||
25 | const ON = 'on'; /* Feature is fully enabled. */ |
||
26 | const OFF = 'off'; /* Feature is fully disabled. */ |
||
27 | |||
28 | /* Bucketing schemes. */ |
||
29 | const UAID = 'uaid'; |
||
30 | const USER = 'user'; |
||
31 | const RANDOM = 'random'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $name; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $cache; |
||
42 | |||
43 | /** |
||
44 | * @var World |
||
45 | */ |
||
46 | private $world; |
||
47 | |||
48 | private $description; |
||
49 | private $enabled; |
||
50 | private $users; |
||
51 | private $groups; |
||
52 | private $adminVariant; |
||
53 | private $internalVariant; |
||
54 | private $publicUrlOverride; |
||
55 | private $bucketing; |
||
56 | |||
57 | private $percentages; |
||
58 | |||
59 | /** |
||
60 | * @param string $name |
||
61 | * @param string|array $stanza |
||
62 | * @param World $world |
||
63 | */ |
||
64 | public function __construct($name, $stanza, World $world) |
||
94 | |||
95 | //////////////////////////////////////////////////////////////////////// |
||
96 | // Public API, though note that Feature.php is the only code that |
||
97 | // should be using this class directly. |
||
98 | |||
99 | /* |
||
100 | * Is this feature enabled for the default id and the logged in |
||
101 | * user, if any? |
||
102 | */ |
||
103 | public function isEnabled() |
||
109 | |||
110 | /* |
||
111 | * What variant is enabled for the default id and the logged in |
||
112 | * user, if any? |
||
113 | */ |
||
114 | public function variant() |
||
120 | |||
121 | /* |
||
122 | * Is this feature enabled for the given user? |
||
123 | */ |
||
124 | public function isEnabledFor(User $user) |
||
128 | |||
129 | /* |
||
130 | * Is this feature enabled, bucketing on the given bucketing |
||
131 | * ID? (Other methods of enabling a feature and specifying a |
||
132 | * variant such as users, groups, and query parameters, will still |
||
133 | * work.) |
||
134 | */ |
||
135 | public function isEnabledBucketingBy($bucketingId) |
||
140 | |||
141 | /* |
||
142 | * What variant is enabled for the given user? |
||
143 | */ |
||
144 | public function variantFor(User $user) |
||
148 | |||
149 | /* |
||
150 | * What variant is enabled, bucketing on the given bucketing ID, |
||
151 | * if any? |
||
152 | */ |
||
153 | public function variantBucketingBy($bucketingId) |
||
158 | |||
159 | /* |
||
160 | * Description of the feature. |
||
161 | */ |
||
162 | public function description() |
||
166 | |||
167 | |||
168 | //////////////////////////////////////////////////////////////////////// |
||
169 | // Internals |
||
170 | |||
171 | /* |
||
172 | * Get the name of the variant we should use. Returns OFF if the |
||
173 | * feature is not enabled for $id. When $inVariantMethod is |
||
174 | * true will also check the conditions that should hold for a |
||
175 | * correct call to variant or variantFor: they should not be |
||
176 | * called for features that are completely enabled (i.e. 'enabled' |
||
177 | * => 'on') since all such variant-specific code should have been |
||
178 | * cleaned up before changing the config and they should not be |
||
179 | * called if the feature is, in fact, disabled for the given id |
||
180 | * since those two methods should always be guarded by an |
||
181 | * isEnabled/isEnabledFor call. |
||
182 | * |
||
183 | * @param $bucketingID the id used to assign a variant based on |
||
184 | * the percentage of users that should see different variants. |
||
185 | * |
||
186 | * @param $userID the identity of the user to be used for the |
||
187 | * special 'admin', 'users', and 'groups' access checks. |
||
188 | * |
||
189 | * @param $inVariantMethod were we called from variant or |
||
190 | * variantFor, in which case we want to perform some certain |
||
191 | * sanity checks to make sure the code is being used correctly. |
||
192 | */ |
||
193 | private function chooseVariant($bucketingId, $userId, $inVariantMethod) |
||
244 | |||
245 | /* |
||
246 | * Return the globally accessible ID used by the one-arg isEnabled |
||
247 | * and variant methods based on the feature's bucketing property. |
||
248 | */ |
||
249 | private function bucketingId() |
||
270 | |||
271 | /* |
||
272 | * For internal requests or if the feature has public_url_override |
||
273 | * set to true, a specific variant can be specified in the |
||
274 | * 'features' query parameter. In all other cases return false, |
||
275 | * meaning nothing was specified. Note that foo:off will turn off |
||
276 | * the 'foo' feature. |
||
277 | */ |
||
278 | private function variantFromURL($userId) |
||
296 | |||
297 | /* |
||
298 | * Get the variant this user should see, if one was configured, |
||
299 | * false otherwise. |
||
300 | */ |
||
301 | private function variantForUser($userId) |
||
311 | |||
312 | /* |
||
313 | * Get the variant this user should see based on their group |
||
314 | * memberships, if one was configured, false otherwise. N.B. If |
||
315 | * the user is in multiple groups that are configured to see |
||
316 | * different variants, they'll get the variant for one of their |
||
317 | * groups but there's no saying which one. If this is a problem in |
||
318 | * practice we could make the configuration more complex. Or you |
||
319 | * can just provide a specific variant via the 'users' property. |
||
320 | */ |
||
321 | private function variantForGroup($userId) |
||
332 | |||
333 | /* |
||
334 | * What variant, if any, should we return if the current user is |
||
335 | * an admin. |
||
336 | */ |
||
337 | private function variantForAdmin($userId) |
||
346 | |||
347 | /* |
||
348 | * What variant, if any, should we return for internal requests. |
||
349 | */ |
||
350 | private function variantForInternal() |
||
359 | |||
360 | /* |
||
361 | * Finally, the normal case: use the percentage of users who |
||
362 | * should see each variant to map a randomish number to a |
||
363 | * particular variant. |
||
364 | */ |
||
365 | private function variantByPercentage($id) |
||
377 | |||
378 | /* |
||
379 | * A randomish number in [0, 1) based on the feature name and $id |
||
380 | * unless we are bucketing completely at random. |
||
381 | */ |
||
382 | private function randomish($id) |
||
387 | |||
388 | //////////////////////////////////////////////////////////////////////// |
||
389 | // Configuration parsing |
||
390 | |||
391 | private function parseDescription($stanza) |
||
395 | |||
396 | /* |
||
397 | * Parse the 'enabled' property of the feature's config stanza. |
||
398 | */ |
||
399 | private function parseEnabled($stanza) |
||
418 | |||
419 | /* |
||
420 | * Returns an array of pairs with the first element of the pair |
||
421 | * being the upper-boundary of the variants percentage and the |
||
422 | * second element being the name of the variant. |
||
423 | */ |
||
424 | private function computePercentages() |
||
444 | |||
445 | /* |
||
446 | * Parse the value of the 'users' and 'groups' properties of the |
||
447 | * feature's config stanza, returning an array mappinng the user |
||
448 | * or group names to they variant they should see. |
||
449 | */ |
||
450 | private function parseUsersOrGroups($stanza, $what) |
||
484 | |||
485 | /* |
||
486 | * Parse the variant name value for the 'admin' and 'internal' |
||
487 | * properties. If non-falsy, must be one of the keys in the |
||
488 | * enabled map unless enabled is 'on' or 'off'. |
||
489 | */ |
||
490 | private function parseVariantName($stanza, $what) |
||
506 | |||
507 | /** |
||
508 | * @param array $stanza |
||
509 | * @return mixed|null |
||
510 | */ |
||
511 | private function parsePublicURLOverride($stanza) |
||
515 | |||
516 | /** |
||
517 | * @param array $stanza |
||
518 | * @return mixed|null |
||
519 | */ |
||
520 | private function parseBucketBy($stanza) |
||
524 | |||
525 | //////////////////////////////////////////////////////////////////////// |
||
526 | // Genericish utilities |
||
527 | |||
528 | /* |
||
529 | * Is the given object an array value that could have been created |
||
530 | * with array(...) with no =>'s in the ...? |
||
531 | */ |
||
532 | private static function isList($a) |
||
536 | |||
537 | /** |
||
538 | * @param mixed $x |
||
539 | * @return array |
||
540 | */ |
||
541 | private static function asArray($x) |
||
545 | |||
546 | /** |
||
547 | * @param $message |
||
548 | */ |
||
549 | private function error($message) |
||
553 | } |
||
554 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.