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 HttpsAuthTest extends TestCase |
||
28 | { |
||
29 | public function testInstanceOf() |
||
34 | |||
35 | View Code Duplication | public function testGetEventsHandlesBeforeAndSignsRequest() |
|
|
|||
36 | { |
||
37 | $auth = new HttpsAuth('key', 'secret'); |
||
38 | static::assertEquals( |
||
39 | ['before' => ['sign', RequestEvents::SIGN_REQUEST]], |
||
40 | $auth->getEvents() |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | View Code Duplication | public function testKeyAndSecretIsPassedToParams() |
|
45 | { |
||
46 | $request = m::mock(RequestInterface::class); |
||
47 | $event = m::mock(BeforeEvent::class); |
||
48 | $event->shouldReceive('getRequest') |
||
49 | ->andReturn($request); |
||
50 | |||
51 | $query = m::mock(Query::class); |
||
52 | $config = m::mock(Collection::class); |
||
53 | |||
54 | $request->shouldReceive('getScheme') |
||
55 | ->andReturn('https'); |
||
56 | $request->shouldReceive('getConfig') |
||
57 | ->andReturn($config); |
||
58 | $request->shouldReceive('getQuery') |
||
59 | ->andReturn($query); |
||
60 | |||
61 | $query->shouldReceive('offsetSet') |
||
62 | ->with('apiKey', 'key'); |
||
63 | $query->shouldReceive('offsetSet') |
||
64 | ->with('secret', 'secret'); |
||
65 | $config->shouldReceive('get') |
||
66 | ->with('auth') |
||
67 | ->andReturn('gigya'); |
||
68 | |||
69 | $auth = new HttpsAuth('key', 'secret'); |
||
70 | $auth->sign($event); |
||
71 | } |
||
72 | |||
73 | public function testKeySecretAndUserKeyIsPassedToParams() |
||
103 | |||
104 | View Code Duplication | public function testAccessors() |
|
105 | { |
||
111 | |||
112 | View Code Duplication | public function testSubscriberDoesNotDoAnythingForNonHttpsRequests() |
|
125 | |||
126 | View Code Duplication | public function testSubscriberDoesNotDoAnythingForNonGigyaAuthRequests() |
|
146 | } |
||
147 |
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.