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 |
||
13 | class RequestTest extends TestCase |
||
14 | { |
||
15 | public function testRequestUriMayBeString() |
||
20 | |||
21 | public function testRequestUriMayBeUri() |
||
27 | |||
28 | public function testValidateRequestUri() |
||
35 | |||
36 | public function testCanConstructWithBody() |
||
42 | |||
43 | public function testNullBody() |
||
49 | |||
50 | public function testFalseyBody() |
||
56 | |||
57 | View Code Duplication | public function testConstructorDoesNotReadStreamBody() |
|
1 ignored issue
–
show
|
|||
58 | { |
||
59 | $body = $this->getMockBuilder(StreamInterface::class)->getMock(); |
||
60 | $body->expects($this->never()) |
||
61 | ->method('__toString'); |
||
62 | |||
63 | $r = new Request('GET', '/', [], $body); |
||
64 | $this->assertSame($body, $r->getBody()); |
||
65 | } |
||
66 | |||
67 | public function testWithUri() |
||
77 | |||
78 | public function testSameInstanceWhenSameUri() |
||
84 | |||
85 | public function testWithRequestTarget() |
||
92 | |||
93 | View Code Duplication | public function testRequestTargetDoesNotAllowSpaces() |
|
1 ignored issue
–
show
|
|||
94 | { |
||
95 | $this->expectException(\InvalidArgumentException::class); |
||
96 | $this->expectExceptionMessage('Invalid request target provided; cannot contain whitespace'); |
||
97 | |||
98 | $r1 = new Request('GET', '/'); |
||
99 | $r1->withRequestTarget('/foo bar'); |
||
100 | } |
||
101 | |||
102 | public function testRequestTargetDefaultsToSlash() |
||
111 | |||
112 | public function testBuildsRequestTarget() |
||
117 | |||
118 | public function testBuildsRequestTargetWithFalseyQuery() |
||
123 | |||
124 | public function testHostIsAddedFirst() |
||
132 | |||
133 | public function testCanGetHeaderAsCsv() |
||
141 | |||
142 | View Code Duplication | public function testHostIsNotOverwrittenWhenPreservingHost() |
|
149 | |||
150 | View Code Duplication | public function testOverridesHostWithUri() |
|
157 | |||
158 | public function testAggregatesHeaders() |
||
167 | |||
168 | View Code Duplication | public function testSupportNumericHeaders() |
|
176 | |||
177 | public function testAddsPortToHeader() |
||
182 | |||
183 | public function testAddsPortToHeaderAndReplacePreviousPort() |
||
189 | |||
190 | View Code Duplication | public function testCannotHaveHeaderWithEmptyName() |
|
1 ignored issue
–
show
|
|||
191 | { |
||
197 | |||
198 | public function testCanHaveHeaderWithEmptyValue() |
||
204 | } |
||
205 |
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.