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<int> |
||
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<string> |
||
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 | * @return void |
||
119 | * @throws ValidationException |
||
120 | */ |
||
121 | 28 | View Code Duplication | public function checkMinimumLength() |
129 | |||
130 | /** |
||
131 | * Checks the minimum maximum length requirement. |
||
132 | * @return void |
||
133 | * @throws ValidationException |
||
134 | */ |
||
135 | 26 | View Code Duplication | public function checkMaximumLength() |
143 | |||
144 | /** |
||
145 | * Checks the lowercase character(s) requirement. |
||
146 | * @return void |
||
147 | * @throws ValidationException |
||
148 | */ |
||
149 | 24 | View Code Duplication | public function checkLowerCase() |
157 | |||
158 | /** |
||
159 | * Checks the upper case character(s) requirement. |
||
160 | * @return void |
||
161 | * @throws ValidationException |
||
162 | */ |
||
163 | 22 | View Code Duplication | public function checkUpperCase() |
171 | |||
172 | /** |
||
173 | * Checks the numeric character(s) requirement. |
||
174 | * @return void |
||
175 | * @throws ValidationException |
||
176 | */ |
||
177 | 20 | View Code Duplication | public function checkNumericCharacters() |
185 | |||
186 | /** |
||
187 | * Checks the special character(s) requirement. |
||
188 | * @return void |
||
189 | * @throws ValidationException |
||
190 | */ |
||
191 | 18 | View Code Duplication | public function checkSpecialCharacters() |
199 | |||
200 | /** |
||
201 | * Validates if a string is not in a array (password history database). |
||
202 | * @return void |
||
203 | * @throws ValidationException |
||
204 | */ |
||
205 | 16 | public function checkNotIn() |
|
221 | |||
222 | /** |
||
223 | * Validates the upper case requirements. |
||
224 | * @return boolean |
||
225 | */ |
||
226 | 4 | View Code Duplication | private function validateUpperCase() |
236 | |||
237 | /** |
||
238 | * Validates the lower case requirements. |
||
239 | * @return boolean |
||
240 | */ |
||
241 | 4 | View Code Duplication | private function validateLowerCase() |
251 | |||
252 | /** |
||
253 | * Validates the special character requirements. |
||
254 | * @return boolean |
||
255 | */ |
||
256 | 4 | View Code Duplication | private function validateSpecialCharacters() |
264 | |||
265 | /** |
||
266 | * Validates the numeric case requirements. |
||
267 | * @return boolean |
||
268 | */ |
||
269 | 4 | View Code Duplication | private function validateNumericCharacters() |
277 | |||
278 | /** |
||
279 | * Validates the minimum string length requirements. |
||
280 | * @return boolean |
||
281 | */ |
||
282 | 6 | View Code Duplication | private function validateLengthMin() |
289 | |||
290 | /** |
||
291 | * Validates the maximum string length requirements. |
||
292 | * @return boolean |
||
293 | */ |
||
294 | 5 | View Code Duplication | private function validateLengthMax() |
301 | |||
302 | /** |
||
303 | * Validates the not_in requirements against a simple array. |
||
304 | * @return void |
||
305 | * @throws ValidationException |
||
306 | */ |
||
307 | 2 | private function validateNotInArray() |
|
314 | |||
315 | /** |
||
316 | * Validates the not_in requirements against an implementation of PasswordHistoryInterface. |
||
317 | * @return void |
||
318 | * @throws ValidationException |
||
319 | */ |
||
320 | 2 | private function validateNotInPasswordHistoryImplementation() |
|
326 | |||
327 | /** |
||
328 | * Count the number of occurrences of a character or string in a string. |
||
329 | * @param array<mixed> $needles The character/string to count occurrences of. |
||
330 | * @param string $haystack The string to check against. |
||
331 | * @return int The number of occurrences. |
||
332 | */ |
||
333 | 8 | private function countOccurrences(array $needles, $haystack) |
|
341 | } |
||
342 |