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 HumanPasswordGenerator 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 HumanPasswordGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class HumanPasswordGenerator extends AbstractPasswordGenerator |
||
| 14 | { |
||
| 15 | const OPTION_WORDS = 'WORDS'; |
||
| 16 | const OPTION_MIN_WORD_LENGTH = 'MIN'; |
||
| 17 | const OPTION_MAX_WORD_LENGTH = 'MAX'; |
||
| 18 | const OPTION_LENGTH = 'LENGTH'; |
||
| 19 | |||
| 20 | const PARAMETER_DICTIONARY_FILE = 'DICTIONARY'; |
||
| 21 | const PARAMETER_WORD_CACHE = 'CACHE'; |
||
| 22 | const PARAMETER_WORD_SEPARATOR = 'SEPARATOR'; |
||
| 23 | |||
| 24 | private $minWordLength; |
||
| 25 | private $maxWordLength; |
||
| 26 | |||
| 27 | public function __construct() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Generate word list for us in generating passwords. |
||
| 40 | * |
||
| 41 | * @return string[] Words |
||
| 42 | * |
||
| 43 | * @throws WordsNotFoundException |
||
| 44 | */ |
||
| 45 | public function generateWordList() |
||
| 78 | |||
| 79 | private function findWordListLength() |
||
| 93 | |||
| 94 | private function generateWordListSubset($min, $max) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Generate one password based on options. |
||
| 114 | * |
||
| 115 | * @return string password |
||
| 116 | * |
||
| 117 | * @throws WordsNotFoundException |
||
| 118 | * @throws ImpossiblePasswordLengthException |
||
| 119 | */ |
||
| 120 | public function generatePassword() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param null|int $minLength |
||
| 185 | * @param null|int $maxLength |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | * |
||
| 189 | * @throws NotEnoughWordsException |
||
| 190 | */ |
||
| 191 | public function randomWord($minLength = null, $maxLength = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get number of words in desired password. |
||
| 213 | * |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | public function getWordCount() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set number of words in desired password(s). |
||
| 223 | * |
||
| 224 | * @param int $characterCount |
||
| 225 | * |
||
| 226 | * @return $this |
||
| 227 | * |
||
| 228 | * @throws \InvalidArgumentException |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function setWordCount($characterCount) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * get max word length. |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function getMaxWordLength() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * set max word length. |
||
| 260 | * |
||
| 261 | * @param int $length |
||
| 262 | * |
||
| 263 | * @return $this |
||
| 264 | * |
||
| 265 | * @throws \InvalidArgumentException |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function setMaxWordLength($length) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * get min word length. |
||
| 283 | * |
||
| 284 | * @return int |
||
| 285 | */ |
||
| 286 | public function getMinWordLength() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * set min word length. |
||
| 296 | * |
||
| 297 | * @param int $length |
||
| 298 | * |
||
| 299 | * @return $this |
||
| 300 | * |
||
| 301 | * @throws \InvalidArgumentException |
||
| 302 | */ |
||
| 303 | View Code Duplication | public function setMinWordLength($length) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Set word list. |
||
| 319 | * |
||
| 320 | * @param string $filename |
||
| 321 | * |
||
| 322 | * @return $this |
||
| 323 | * |
||
| 324 | * @throws \InvalidArgumentException |
||
| 325 | * @throws FileNotFoundException |
||
| 326 | */ |
||
| 327 | public function setWordList($filename) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get word list filename. |
||
| 343 | * |
||
| 344 | * @throws FileNotFoundException |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getWordList() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get word separator. |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getWordSeparator() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set word separator. |
||
| 369 | * |
||
| 370 | * @param string $separator |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | * |
||
| 374 | * @throws \InvalidArgumentException |
||
| 375 | */ |
||
| 376 | public function setWordSeparator($separator) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Password length |
||
| 389 | * |
||
| 390 | * @return integer |
||
| 391 | */ |
||
| 392 | public function getLength() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Set length of desired password(s) |
||
| 399 | * |
||
| 400 | * @param integer $characterCount |
||
| 401 | * |
||
| 402 | * @return $this |
||
| 403 | * |
||
| 404 | * @throws \InvalidArgumentException |
||
| 405 | */ |
||
| 406 | View Code Duplication | public function setLength($characterCount) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Calculate how long the password would be using minimum word length |
||
| 419 | * |
||
| 420 | * @return int |
||
| 421 | */ |
||
| 422 | public function getMinPasswordLength() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Calculate how long the password would be using maximum word length |
||
| 431 | * |
||
| 432 | * @return int |
||
| 433 | */ |
||
| 434 | public function getMaxPasswordLength() |
||
| 440 | } |
||
| 441 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.