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 Validator 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 Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Validator |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * RegEx for uppercase character detection. |
||
27 | */ |
||
28 | const REGEX_UPPER_CASE = "/[A-Z]/"; |
||
29 | |||
30 | /** |
||
31 | * RegEx for lowercase character detection. |
||
32 | */ |
||
33 | const REGEX_LOWER_CASE = "/[a-z]/"; |
||
34 | |||
35 | /** |
||
36 | * The Plexity object (contains the validation configuration) |
||
37 | * @var Plexity |
||
38 | */ |
||
39 | private $configuration; |
||
40 | |||
41 | /** |
||
42 | * Numeric values list |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $numbers = [ |
||
46 | 1, |
||
47 | 2, |
||
48 | 3, |
||
49 | 4, |
||
50 | 5, |
||
51 | 6, |
||
52 | 7, |
||
53 | 8, |
||
54 | 9, |
||
55 | 0 |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * Special Character list |
||
60 | * @see https://www.owasp.org/index.php/Password_special_characters |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $specialCharacters = [ |
||
64 | ' ', |
||
65 | '!', |
||
66 | '"', |
||
67 | '#', |
||
68 | '$', |
||
69 | '%', |
||
70 | '&', |
||
71 | '\'', |
||
72 | '(', |
||
73 | ')', |
||
74 | '*', |
||
75 | '+', |
||
76 | ',', |
||
77 | '.', |
||
78 | '/', |
||
79 | ':', |
||
80 | ';', |
||
81 | '<', |
||
82 | '=', |
||
83 | '>', |
||
84 | '?', |
||
85 | '@', |
||
86 | '[', |
||
87 | ']', |
||
88 | '\\', |
||
89 | '^', |
||
90 | '_', |
||
91 | '`', |
||
92 | '{', |
||
93 | '|', |
||
94 | '}', |
||
95 | '~', |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * Validates all the configured rules and responds as requested. |
||
100 | * @return boolean |
||
101 | * @throws ValidationException |
||
102 | */ |
||
103 | 28 | public function validate(Plexity $configuration) |
|
115 | |||
116 | /** |
||
117 | * Checks the minimum length requirement. |
||
118 | * @throws ValidationException |
||
119 | */ |
||
120 | 28 | View Code Duplication | public function checkMinimumLength() |
128 | |||
129 | /** |
||
130 | * Checks the minimum maximum length requirement. |
||
131 | * @throws ValidationException |
||
132 | */ |
||
133 | 26 | View Code Duplication | public function checkMaximumLength() |
141 | |||
142 | /** |
||
143 | * Checks the lowercase character(s) requirement. |
||
144 | * @throws ValidationException |
||
145 | */ |
||
146 | 24 | View Code Duplication | public function checkLowerCase() |
154 | |||
155 | /** |
||
156 | * Checks the upper case character(s) requirement. |
||
157 | * @throws ValidationException |
||
158 | */ |
||
159 | 22 | View Code Duplication | public function checkUpperCase() |
167 | |||
168 | /** |
||
169 | * Checks the numeric character(s) requirement. |
||
170 | * @throws ValidationException |
||
171 | */ |
||
172 | 20 | View Code Duplication | public function checkNumericCharacters() |
180 | |||
181 | /** |
||
182 | * Checks the special character(s) requirement. |
||
183 | * @throws ValidationException |
||
184 | */ |
||
185 | 18 | View Code Duplication | public function checkSpecialCharacters() |
193 | |||
194 | /** |
||
195 | * Validates if a string is not in a array (password history database). |
||
196 | * @throws ValidationException |
||
197 | */ |
||
198 | 16 | public function checkNotIn() |
|
218 | |||
219 | /** |
||
220 | * Validates the upper case requirements. |
||
221 | * @return boolean |
||
222 | */ |
||
223 | 4 | View Code Duplication | private function validateUpperCase() |
233 | |||
234 | /** |
||
235 | * Validates the lower case requirements. |
||
236 | * @return boolean |
||
237 | */ |
||
238 | 4 | View Code Duplication | private function validateLowerCase() |
248 | |||
249 | /** |
||
250 | * Validates the special character requirements. |
||
251 | * @return boolean |
||
252 | */ |
||
253 | 4 | View Code Duplication | private function validateSpecialCharacters() |
261 | |||
262 | /** |
||
263 | * Validates the numeric case requirements. |
||
264 | * @return boolean |
||
265 | */ |
||
266 | 4 | View Code Duplication | private function validateNumericCharacters() |
274 | |||
275 | /** |
||
276 | * Validates the minimum string length requirements. |
||
277 | * @return boolean |
||
278 | */ |
||
279 | 6 | View Code Duplication | private function validateLengthMin() |
286 | |||
287 | /** |
||
288 | * Validates the maximum string length requirements. |
||
289 | * @return boolean |
||
290 | */ |
||
291 | 5 | View Code Duplication | private function validateLengthMax() |
298 | |||
299 | /** |
||
300 | * Validates the not_in requirements against a simple array. |
||
301 | * @return boolean |
||
302 | */ |
||
303 | 2 | View Code Duplication | private function validateNotInArray() |
311 | |||
312 | /** |
||
313 | * Validates the not_in requirements against an implementation of PasswordHistoryInterface. |
||
314 | * @return boolean |
||
315 | */ |
||
316 | 2 | private function validateNotInPasswordHistoryImplementation() |
|
320 | |||
321 | /** |
||
322 | * Count the number of occurrences of a character or string in a string. |
||
323 | * @param array $needles The character/string to count occurrences of. |
||
324 | * @param string $haystack The string to check against. |
||
325 | * @return int The number of occurrences. |
||
326 | */ |
||
327 | 8 | private function countOccurrences(array $needles, $haystack) |
|
335 | } |
||
336 |