Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Autopromote 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 Autopromote, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Autopromote { |
||
| 29 | /** |
||
| 30 | * Get the groups for the given user based on $wgAutopromote. |
||
| 31 | * |
||
| 32 | * @param User $user The user to get the groups for |
||
| 33 | * @return array Array of groups to promote to. |
||
| 34 | */ |
||
| 35 | public static function getAutopromoteGroups( User $user ) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the groups for the given user based on the given criteria. |
||
| 53 | * |
||
| 54 | * Does not return groups the user already belongs to or has once belonged. |
||
| 55 | * |
||
| 56 | * @param User $user The user to get the groups for |
||
| 57 | * @param string $event Key in $wgAutopromoteOnce (each one has groups/criteria) |
||
| 58 | * |
||
| 59 | * @return array Groups the user should be promoted to. |
||
| 60 | * |
||
| 61 | * @see $wgAutopromoteOnce |
||
| 62 | */ |
||
| 63 | public static function getAutopromoteOnceGroups( User $user, $event ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Recursively check a condition. Conditions are in the form |
||
| 92 | * array( '&' or '|' or '^' or '!', cond1, cond2, ... ) |
||
| 93 | * where cond1, cond2, ... are themselves conditions; *OR* |
||
| 94 | * APCOND_EMAILCONFIRMED, *OR* |
||
| 95 | * array( APCOND_EMAILCONFIRMED ), *OR* |
||
| 96 | * array( APCOND_EDITCOUNT, number of edits ), *OR* |
||
| 97 | * array( APCOND_AGE, seconds since registration ), *OR* |
||
| 98 | * similar constructs defined by extensions. |
||
| 99 | * This function evaluates the former type recursively, and passes off to |
||
| 100 | * self::checkCondition for evaluation of the latter type. |
||
| 101 | * |
||
| 102 | * @param mixed $cond A condition, possibly containing other conditions |
||
| 103 | * @param User $user The user to check the conditions against |
||
| 104 | * @return bool Whether the condition is true |
||
| 105 | */ |
||
| 106 | private static function recCheckCondition( $cond, User $user ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * As recCheckCondition, but *not* recursive. The only valid conditions |
||
| 155 | * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/ |
||
| 156 | * APCOND_AGE. Other types will throw an exception if no extension evaluates them. |
||
| 157 | * |
||
| 158 | * @param array $cond A condition, which must not contain other conditions |
||
| 159 | * @param User $user The user to check the condition against |
||
| 160 | * @throws MWException |
||
| 161 | * @return bool Whether the condition is true for the user |
||
| 162 | */ |
||
| 163 | private static function checkCondition( $cond, User $user ) { |
||
| 209 | } |
||
| 210 |