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 |
||
27 | class GigyaGrantTest extends TestCase |
||
28 | { |
||
29 | /** @var mixed */ |
||
30 | private $gigya; |
||
31 | |||
32 | /** |
||
33 | * @param string $apiKey |
||
34 | * @param string $secret |
||
35 | * @param string|null $userKey |
||
36 | * |
||
37 | * @return GigyaGrant |
||
38 | */ |
||
39 | public function getGrant($apiKey, $secret, $userKey = null) |
||
44 | |||
45 | public function testInstanceOf() |
||
50 | |||
51 | View Code Duplication | public function testGetTokenWhenOneHasNotBeenSet() |
|
|
|||
52 | { |
||
53 | $grant = $this->getGrant('key', 'secret'); |
||
54 | |||
55 | $response = m::mock(ResponseInterface::class); |
||
56 | |||
57 | $this->gigya->shouldReceive('socialize->getToken') |
||
58 | ->with([ |
||
59 | 'client_id' => 'key', |
||
60 | 'client_secret' => 'secret', |
||
61 | 'grant_type' => 'none', |
||
62 | ], ['auth' => 'none']) |
||
63 | ->andReturn($response); |
||
64 | |||
65 | $response->shouldReceive('getErrorCode') |
||
66 | ->andReturn(ErrorCode::OK); |
||
67 | $data = m::mock(Collection::class); |
||
68 | $response->shouldReceive('getData') |
||
69 | ->andReturn($data); |
||
70 | $data->shouldReceive('get') |
||
71 | ->with('access_token') |
||
72 | ->andReturn('some_token'); |
||
73 | $data->shouldReceive('get') |
||
74 | ->with('expires_in', null) |
||
75 | ->andReturn(3600); |
||
76 | |||
77 | $token = $grant->getToken(); |
||
78 | |||
79 | $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600))); |
||
80 | |||
81 | static::assertEquals('some_token', $token->getToken()); |
||
82 | static::assertLessThanOrEqual($expires, $token->getExpires()); |
||
83 | static::assertFalse($token->isExpired()); |
||
84 | |||
85 | static::assertSame($token, $grant->getToken(), 'Calling getToken again, should return the same token'); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testGetTokenWithNoExpiry() |
|
89 | { |
||
90 | $grant = $this->getGrant('key', 'secret'); |
||
91 | |||
92 | $response = m::mock(ResponseInterface::class); |
||
93 | |||
94 | $this->gigya->shouldReceive('socialize->getToken') |
||
95 | ->with([ |
||
96 | 'client_id' => 'key', |
||
97 | 'client_secret' => 'secret', |
||
98 | 'grant_type' => 'none', |
||
99 | ], ['auth' => 'none']) |
||
100 | ->andReturn($response); |
||
101 | |||
102 | $response->shouldReceive('getErrorCode') |
||
103 | ->andReturn(ErrorCode::OK); |
||
104 | $data = m::mock(Collection::class); |
||
105 | $response->shouldReceive('getData') |
||
106 | ->andReturn($data); |
||
107 | $data->shouldReceive('get') |
||
108 | ->with('access_token') |
||
109 | ->andReturn('some_token'); |
||
110 | $data->shouldReceive('get') |
||
111 | ->with('expires_in', null) |
||
112 | ->andReturn(null); |
||
113 | |||
114 | $token = $grant->getToken(); |
||
115 | |||
116 | static::assertEquals('some_token', $token->getToken()); |
||
117 | static::assertNull($token->getExpires()); |
||
118 | static::assertFalse($token->isExpired()); |
||
119 | |||
120 | static::assertSame($token, $grant->getToken(), 'Calling getToken again, should return the same token'); |
||
121 | } |
||
122 | |||
123 | public function testGetTokenThatExpiresCallGetTokenAgain() |
||
169 | |||
170 | public function testNonOkResponseReturnsANullToken() |
||
191 | } |
||
192 |
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.