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 |
||
25 | class ValidGigyaResponseMiddlewareTest extends TestCase |
||
26 | { |
||
27 | public function testValidResponse() |
||
28 | { |
||
29 | $handler = new MockHandler([ |
||
30 | new Response(200, [], TestFixtures::getFixture('accounts.search_simple')), |
||
31 | ]); |
||
32 | |||
33 | $middleware = ValidGigyaResponseMiddleware::middleware(); |
||
34 | |||
35 | $func = $middleware($handler); |
||
36 | $func(new Request('GET', 'https://foo.com'), [])->wait(); |
||
37 | } |
||
38 | |||
39 | View Code Duplication | public function testMissingFieldWillThrowAnException() |
|
|
|||
40 | { |
||
41 | $handler = new MockHandler([ |
||
42 | new Response(200, [], TestFixtures::getFixture('missing_field')), |
||
43 | ]); |
||
44 | |||
45 | $this->expectException(UnknownResponseException::class); |
||
46 | $this->expectExceptionMessage("The contents of the response could not be determined. Missing required field: 'statusReason'"); |
||
47 | |||
48 | $middleware = ValidGigyaResponseMiddleware::middleware(); |
||
49 | |||
50 | $func = $middleware($handler); |
||
51 | $func(new Request('GET', 'https://foo.com'), [])->wait(); |
||
52 | } |
||
53 | |||
54 | View Code Duplication | public function testNoBodyWillFail() |
|
55 | { |
||
56 | $handler = new MockHandler([ |
||
57 | new Response(200), |
||
58 | ]); |
||
59 | |||
60 | $this->expectException(UnknownResponseException::class); |
||
61 | $this->expectExceptionMessage('The contents of the response could not be determined'); |
||
62 | |||
63 | $middleware = ValidGigyaResponseMiddleware::middleware(); |
||
64 | |||
65 | $func = $middleware($handler); |
||
66 | $func(new Request('GET', 'https://foo.com'), [])->wait(); |
||
67 | } |
||
68 | |||
69 | View Code Duplication | public function testInvalidBody() |
|
83 | |||
84 | public function testUnknownResponseContainsTheOriginalResponse() |
||
98 | } |
||
99 |
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.