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 |
||
15 | class PasswordRestrictionsValidatorTest extends ConstraintValidatorTestCase |
||
16 | { |
||
17 | const PARAMETER_MIN_LENGTH = 8; |
||
18 | const PARAMETER_MAX_LENGTH = 16; |
||
19 | const PARAMETER_MIN_DIGITS = 3; |
||
20 | const PARAMETER_MIN_UPPERCASE = 2; |
||
21 | const PARAMETER_MIN_SPECIAL_CHARACTERS = 1; |
||
22 | |||
23 | /** |
||
24 | * @return PasswordRestrictionsValidator |
||
25 | */ |
||
26 | protected function createValidator() |
||
30 | |||
31 | /** |
||
32 | * Set a validator with a limited number of parameters to overrule the default one from createValidator. |
||
33 | * |
||
34 | * @param int $minDigits |
||
35 | * @param int $minUppercase |
||
36 | * @param int $minSpecialCharacters |
||
37 | * @param int $minLength |
||
38 | * @param int $maxLength |
||
39 | */ |
||
40 | protected function setValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength) |
||
45 | |||
46 | /** |
||
47 | * @param string $password |
||
48 | * @param string|null $message |
||
49 | * @param array $parameters |
||
50 | * @param null $code |
||
51 | * |
||
52 | * @dataProvider dataPasswordsWithAllParameters |
||
53 | */ |
||
54 | public function testPasswordWithAllParametersSet($password, $message = null, array $parameters = [], $code = null) |
||
58 | |||
59 | /** |
||
60 | * @param string $password |
||
61 | * @param string|null $message |
||
62 | * @param array $parameters |
||
63 | * @param null $code |
||
64 | * |
||
65 | * @dataProvider dataPasswordsToShort |
||
66 | */ |
||
67 | public function testPasswordToShortOnlySet($password, $message = null, array $parameters = [], $code = null) |
||
72 | |||
73 | /** |
||
74 | * @param string $password |
||
75 | * @param string|null $message |
||
76 | * @param array $parameters |
||
77 | * @param null $code |
||
78 | * |
||
79 | * @dataProvider dataPasswordsToLong |
||
80 | */ |
||
81 | public function testPasswordToLongOnlySet($password, $message = null, array $parameters = [], $code = null) |
||
86 | |||
87 | /** |
||
88 | * @param string $password |
||
89 | * @param string|null $message |
||
90 | * @param array $parameters |
||
91 | * @param null $code |
||
92 | * |
||
93 | * @dataProvider dataPasswordsMinimumDigits |
||
94 | */ |
||
95 | public function testPasswordMinimumDigitsOnlySet($password, $message = null, array $parameters = [], $code = null) |
||
100 | |||
101 | /** |
||
102 | * @param string $password |
||
103 | * @param string|null $message |
||
104 | * @param array $parameters |
||
105 | * @param null $code |
||
106 | * |
||
107 | * @dataProvider dataPasswordsMinimumUppercase |
||
108 | */ |
||
109 | public function testPasswordMinimumUppercaseOnlySet($password, $message = null, array $parameters = [], $code = null) |
||
114 | |||
115 | /** |
||
116 | * @param string $password |
||
117 | * @param string|null $message |
||
118 | * @param array $parameters |
||
119 | * @param null $code |
||
120 | * |
||
121 | * @dataProvider dataPasswordsMinimumSpecialCharacters |
||
122 | */ |
||
123 | public function testPasswordMinimumSpecialCharactersOnlySet($password, $message = null, array $parameters = [], $code = null) |
||
128 | |||
129 | /** |
||
130 | * @param string $password |
||
131 | * @param string|null $message |
||
132 | * @param array $parameters |
||
133 | * @param null $code |
||
134 | * |
||
135 | * @dataProvider dataPasswordsLengthRange |
||
136 | */ |
||
137 | public function testPasswordLengthRangeSet($password, $message = null, array $parameters = [], $code = null) |
||
142 | |||
143 | /** |
||
144 | * Uses the set validator combined with data to assert. |
||
145 | * |
||
146 | * @param string $password |
||
147 | * @param string|null $message |
||
148 | * @param array $parameters |
||
149 | * @param null $code |
||
150 | */ |
||
151 | private function buildAndTestPasswordRestrictions($password, $message = null, array $parameters = [], $code = null) |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function dataPasswordsWithAllParameters() |
||
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | */ |
||
185 | public function dataPasswordsToShort() |
||
193 | |||
194 | /** |
||
195 | * @return array |
||
196 | */ |
||
197 | public function dataPasswordsToLong() |
||
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | public function dataPasswordsMinimumDigits() |
||
215 | |||
216 | /** |
||
217 | * @return array |
||
218 | */ |
||
219 | View Code Duplication | public function dataPasswordsMinimumUppercase() |
|
226 | |||
227 | /** |
||
228 | * @return array |
||
229 | */ |
||
230 | View Code Duplication | public function dataPasswordsMinimumSpecialCharacters() |
|
237 | |||
238 | /** |
||
239 | * Combine the data of the too long and too short test for an extra length |
||
240 | * range test. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function dataPasswordsLengthRange() |
||
248 | } |
||
249 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: