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:
| 1 | <?php |
||
| 21 | class Validator |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The Plexity object (contains the validation configuration) |
||
| 26 | * @var Plexity |
||
| 27 | */ |
||
| 28 | private $configuration; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Numeric values list |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $numbers = [ |
||
| 35 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Special Character list |
||
| 40 | * @see https://www.owasp.org/index.php/Password_special_characters |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $specialCharacters = [ |
||
| 44 | ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '.', |
||
| 45 | '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '\\', '^', '_', '`', |
||
| 46 | '{', '|', '}', '~', |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Validates all the configured rules and responds as requested. |
||
| 51 | * @return boolean |
||
| 52 | * @throws ValidationException |
||
| 53 | */ |
||
| 54 | 26 | public function validate(Plexity $configuration) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Checks the minimum length requirement. |
||
| 69 | * @throws ValidationException |
||
| 70 | */ |
||
| 71 | 26 | View Code Duplication | public function checkMinimumLength() |
| 79 | |||
| 80 | /** |
||
| 81 | * Checks the minimum maximum length requirement. |
||
| 82 | * @throws ValidationException |
||
| 83 | */ |
||
| 84 | 24 | View Code Duplication | public function checkMaximumLength() |
| 92 | |||
| 93 | /** |
||
| 94 | * Checks the lowercase character(s) requirement. |
||
| 95 | * @throws ValidationException |
||
| 96 | */ |
||
| 97 | 22 | View Code Duplication | public function checkLowerCase() |
| 105 | |||
| 106 | /** |
||
| 107 | * Checks the upper case character(s) requirement. |
||
| 108 | * @throws ValidationException |
||
| 109 | */ |
||
| 110 | 20 | View Code Duplication | public function checkUpperCase() |
| 118 | |||
| 119 | /** |
||
| 120 | * Checks the numeric character(s) requirement. |
||
| 121 | * @throws ValidationException |
||
| 122 | */ |
||
| 123 | 18 | View Code Duplication | public function checkNumericCharacters() |
| 131 | |||
| 132 | /** |
||
| 133 | * Checks the special character(s) requirement. |
||
| 134 | * @throws ValidationException |
||
| 135 | */ |
||
| 136 | 16 | View Code Duplication | public function checkSpecialCharacters() |
| 144 | |||
| 145 | /** |
||
| 146 | * Validates if a string is not in a array (password history database). |
||
| 147 | * @throws ValidationException |
||
| 148 | */ |
||
| 149 | 14 | View Code Duplication | public function checkNotIn() |
| 157 | |||
| 158 | /** |
||
| 159 | * Validates the upper case requirements. |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | 4 | View Code Duplication | private function validateUpperCase() |
| 172 | |||
| 173 | /** |
||
| 174 | * Validates the lower case requirements. |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | 4 | View Code Duplication | private function validateLowerCase() |
| 187 | |||
| 188 | /** |
||
| 189 | * Validates the special character requirements. |
||
| 190 | * @return boolean |
||
| 191 | */ |
||
| 192 | 4 | View Code Duplication | private function validateSpecialCharacters() |
| 199 | |||
| 200 | /** |
||
| 201 | * Validates the numeric case requirements. |
||
| 202 | * @return boolean |
||
| 203 | */ |
||
| 204 | 4 | View Code Duplication | private function validateNumericCharacters() |
| 211 | |||
| 212 | /** |
||
| 213 | * Validates the minimum string length requirements. |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | 6 | View Code Duplication | private function validateLengthMin() |
| 223 | |||
| 224 | /** |
||
| 225 | * Validates the maximum string length requirements. |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | 5 | View Code Duplication | private function validateLengthMax() |
| 235 | |||
| 236 | /** |
||
| 237 | * Validates the not_in requirements. |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | 2 | View Code Duplication | private function validateNotIn() |
| 247 | |||
| 248 | /** |
||
| 249 | * Count the number of occurences of a character or string in a string. |
||
| 250 | * @param array $needles The character/string to count occurrences of. |
||
| 251 | * @param string $haystack The string to check against. |
||
| 252 | * @return int The number of occurrences. |
||
| 253 | */ |
||
| 254 | 8 | private function countOccurrences(array $needles, $haystack) |
|
| 262 | } |
||
| 263 |