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 |
||
16 | class JwtKeyTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | /** |
||
19 | * @test |
||
20 | * @expectedException \InvalidArgumentException |
||
21 | */ |
||
22 | public function constructionWillFailWhenSecretNorLoaderPassed() |
||
26 | |||
27 | /** |
||
28 | * @test |
||
29 | */ |
||
30 | public function canOmitSecretWhenPassingLoader() |
||
38 | |||
39 | /** |
||
40 | * @test |
||
41 | * @expectedException \InvalidArgumentException |
||
42 | */ |
||
43 | public function cannotPassBothSecretAndLoader() |
||
47 | |||
48 | /** |
||
49 | * @test |
||
50 | */ |
||
51 | public function serializingWillClearSecret() |
||
60 | |||
61 | /** |
||
62 | * @test |
||
63 | */ |
||
64 | public function validateTokenWillCallVerifySignatureOnToken() |
||
70 | |||
71 | /** |
||
72 | * @test |
||
73 | */ |
||
74 | public function canLoadSecretFromLoader() |
||
86 | |||
87 | /** |
||
88 | * @test |
||
89 | */ |
||
90 | View Code Duplication | public function willGetRsaSignatureValidatorWhenTypeIsNotSpecified() |
|
99 | |||
100 | /** |
||
101 | * @test |
||
102 | */ |
||
103 | View Code Duplication | public function willGetRsaSignatureValidatorWhenTypeIsRsa() |
|
112 | |||
113 | /** |
||
114 | * @test |
||
115 | */ |
||
116 | View Code Duplication | public function validationWillFailWhenPrincipleIsMissing() |
|
130 | |||
131 | |||
132 | |||
133 | /** |
||
134 | * @test |
||
135 | */ |
||
136 | View Code Duplication | public function validationWillFailWhenSubjectMissing() |
|
150 | |||
151 | /** |
||
152 | * @test |
||
153 | * @expectedException \InvalidArgumentException |
||
154 | */ |
||
155 | public function validationWillFailWhenExpiredByExp() |
||
160 | |||
161 | /** |
||
162 | * @test |
||
163 | * @expectedException \InvalidArgumentException |
||
164 | */ |
||
165 | public function validationWillFailWhenExpiredByIatAndMinIssueTime() |
||
170 | |||
171 | /** |
||
172 | * @test |
||
173 | * @expectedException \InvalidArgumentException |
||
174 | */ |
||
175 | public function validationWillFailWhenNotValidYet() |
||
180 | |||
181 | /** |
||
182 | * @test |
||
183 | * @expectedException \InvalidArgumentException |
||
184 | */ |
||
185 | public function validationWillFailWhenIssuerDoesNotMatch() |
||
190 | |||
191 | /** |
||
192 | * @test |
||
193 | * @expectedException \InvalidArgumentException |
||
194 | */ |
||
195 | public function validationWillFailWhenAudienceDoesNotMatch() |
||
200 | |||
201 | |||
202 | /** |
||
203 | * @test |
||
204 | * @expectedException \InvalidArgumentException |
||
205 | */ |
||
206 | public function validationWillFailWhenIssuerIsConfiguredAndNotInClaims() |
||
211 | |||
212 | /** |
||
213 | * @test |
||
214 | * @expectedException \InvalidArgumentException |
||
215 | */ |
||
216 | public function validationWillFailWhenMinIssueTimeIsConfiguredAndIatNotInClaims() |
||
221 | |||
222 | /** |
||
223 | * @test |
||
224 | * @expectedException \InvalidArgumentException |
||
225 | */ |
||
226 | public function validationWillFailWhenAudienceIsConfiguredAndNotInClaims() |
||
231 | |||
232 | /** |
||
233 | * @test |
||
234 | * @expectedException \InvalidArgumentException |
||
235 | */ |
||
236 | public function validationWillFailWhenIgnoreOtherReservedAndArbitraryClaimsAreRequiredButNotInClaims() |
||
243 | |||
244 | /** |
||
245 | * @test |
||
246 | * @expectedException \InvalidArgumentException |
||
247 | */ |
||
248 | public function headerValidationWillFailWhenAlgoIsMissing() |
||
255 | |||
256 | /** |
||
257 | * @test |
||
258 | * @expectedException \InvalidArgumentException |
||
259 | */ |
||
260 | public function headerValidationWillFailWhenTypeIsMissing() |
||
267 | |||
268 | /** |
||
269 | * @test |
||
270 | * @expectedException \InvalidArgumentException |
||
271 | */ |
||
272 | public function headerValidationWillFailWhenAlgorithmDoesntMatchKey() |
||
279 | |||
280 | /** |
||
281 | * @test |
||
282 | * @expectedException \InvalidArgumentException |
||
283 | */ |
||
284 | public function headerValidationWillFailWhenTypeIsNotJwt() |
||
291 | |||
292 | /** |
||
293 | * @param string $secret |
||
294 | * @param JwtKey|null $key |
||
295 | * |
||
296 | * @return JwtToken |
||
297 | */ |
||
298 | private function createTokenMock($secret, JwtKey $key = null) |
||
319 | } |
||
320 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.