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 RequirementPasswordGenerator 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 RequirementPasswordGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class RequirementPasswordGenerator extends ComputerPasswordGenerator |
||
| 16 | { |
||
| 17 | private $minimumCounts = array(); |
||
| 18 | private $maximumCounts = array(); |
||
| 19 | private $validOptions = array(); |
||
| 20 | private $dirtyCheck = true; |
||
| 21 | |||
| 22 | /** |
||
| 23 | */ |
||
| 24 | public function __construct() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Generate one password based on options. |
||
| 38 | * |
||
| 39 | * @return string password |
||
| 40 | * @throws ImpossibleMinMaxLimitsException |
||
| 41 | * @throws \Hackzilla\PasswordGenerator\Exception\CharactersNotFoundException |
||
| 42 | */ |
||
| 43 | public function generatePassword() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Password minimum count for option. |
||
| 62 | * |
||
| 63 | * @param string $option Use class constants |
||
| 64 | * |
||
| 65 | * @return int|null |
||
| 66 | */ |
||
| 67 | public function getMinimumCount($option) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Password maximum count for option. |
||
| 74 | * |
||
| 75 | * @param string $option Use class constants |
||
| 76 | * |
||
| 77 | * @return int|null |
||
| 78 | */ |
||
| 79 | public function getMaximumCount($option) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set minimum count of option for desired password(s). |
||
| 86 | * |
||
| 87 | * @param string $option Use class constants |
||
| 88 | * @param int|null $characterCount |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | * |
||
| 92 | * @throws InvalidOptionException |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function setMinimumCount($option, $characterCount) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Set maximum count of option for desired password(s). |
||
| 119 | * |
||
| 120 | * @param string $option Use class constants |
||
| 121 | * @param int|null $characterCount |
||
| 122 | * |
||
| 123 | * @return $this |
||
| 124 | * |
||
| 125 | * @throws InvalidOptionException |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function setMaximumCount($option, $characterCount) |
|
| 149 | |||
| 150 | public function validLimits() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $option |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function validOption($option) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Generate $count number of passwords. |
||
| 209 | * |
||
| 210 | * @param int $count Number of passwords to return |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | * |
||
| 214 | * @throws \InvalidArgumentException |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function generatePasswords($count = 1) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Check password is valid when comparing to minimum and maximum counts of options. |
||
| 235 | * |
||
| 236 | * @param string $password |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function validatePassword($password) |
||
| 258 | } |
||
| 259 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.