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 |
||
20 | class AuthenticatorTest extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | // @codingStandardsIgnoreStart |
||
23 | |||
24 | /** |
||
25 | * Created using jwt.io |
||
26 | */ |
||
27 | const TEST_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleU9uZSJ9.eyJwcm4iOiJqb2huIiwiaXNzIjoiaHR0cDovL2FwaS5zZXJ2ZXIxLmNvbS9vYXV0aDIvdG9rZW4ifQ._jXjAWMzwwG1v5N3ZOEUoLGSINtmwLsvQdfYkYAcWiY'; |
||
28 | |||
29 | const JKEY_CLASS = 'KleijnWeb\JwtBundle\Authenticator\JwtKey'; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $keyConfig = [ |
||
35 | 'keyOne' => |
||
36 | [ |
||
37 | 'issuer' => 'http://api.server1.com/oauth2/token', |
||
38 | 'secret' => 'A Pre-Shared Key', |
||
39 | 'type' => 'HS256', |
||
40 | ], |
||
41 | 'keyTwo' => |
||
42 | [ |
||
43 | 'issuer' => 'http://api.server2.com/oauth2/token', |
||
44 | 'type' => 'RS256', |
||
45 | 'secret' => 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0F', |
||
46 | ], |
||
47 | ]; |
||
48 | |||
49 | // @codingStandardsIgnoreEnd |
||
50 | |||
51 | /** |
||
52 | * @var JwtKey[] |
||
53 | */ |
||
54 | private $keys = []; |
||
55 | |||
56 | protected function setUp() |
||
57 | { |
||
58 | foreach (self::$keyConfig as $keyId => $config) { |
||
59 | $config['kid'] = $keyId; |
||
60 | $this->keys[$keyId] = new JwtKey($config); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @test |
||
66 | */ |
||
67 | public function getGetKeysUsingIndexesInConfig() |
||
74 | |||
75 | /** |
||
76 | * @test |
||
77 | */ |
||
78 | public function willGetSingleKeyWhenKeyIdIsNull() |
||
87 | |||
88 | /** |
||
89 | * @test |
||
90 | * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException |
||
91 | */ |
||
92 | public function willFailWhenTryingToGetKeyWithoutIdWhenThereAreMoreThanOne() |
||
98 | |||
99 | /** |
||
100 | * @test |
||
101 | * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException |
||
102 | */ |
||
103 | public function willFailWhenTryingToGetUnknownKey() |
||
109 | |||
110 | /** |
||
111 | * @test |
||
112 | */ |
||
113 | public function authenticateTokenWillSetUserFetchedFromUserProviderOnToken() |
||
130 | |||
131 | /** |
||
132 | * @test |
||
133 | * @expectedException \UnexpectedValueException |
||
134 | */ |
||
135 | View Code Duplication | public function authenticateTokenWillFailIfCredentialsAreNotJwtToken() |
|
146 | |||
147 | /** |
||
148 | * @test |
||
149 | */ |
||
150 | View Code Duplication | public function supportsPreAuthToken() |
|
158 | |||
159 | /** |
||
160 | * @test |
||
161 | * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
||
162 | */ |
||
163 | public function willFailWhenApiKeyNotFoundInHeader() |
||
169 | |||
170 | /** |
||
171 | * @test |
||
172 | */ |
||
173 | public function canGetAnonTokenWithClaims() |
||
183 | |||
184 | /** |
||
185 | * @param array $claims |
||
186 | * |
||
187 | * @return JwtToken |
||
188 | */ |
||
189 | private function createToken(array $claims) |
||
201 | } |
||
202 |
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.