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 |
||
12 | class HttplugFactoryTest extends TestCase |
||
13 | { |
||
14 | public function testCreateRequest() |
||
15 | { |
||
16 | $factory = new HttplugFactory(); |
||
17 | $r = $factory->createRequest('POST', 'https://nyholm.tech', ['Content-Type' => 'text/html'], 'foobar', '2.0'); |
||
18 | |||
19 | $this->assertEquals('POST', $r->getMethod()); |
||
20 | $this->assertEquals('https://nyholm.tech', $r->getUri()->__toString()); |
||
21 | $this->assertEquals('2.0', $r->getProtocolVersion()); |
||
22 | $this->assertEquals('foobar', $r->getBody()->__toString()); |
||
23 | |||
24 | $headers = $r->getHeaders(); |
||
25 | $this->assertCount(2, $headers); // Including HOST |
||
26 | $this->assertArrayHasKey('Content-Type', $headers); |
||
27 | $this->assertEquals('text/html', $headers['Content-Type'][0]); |
||
28 | } |
||
29 | |||
30 | public function testCreateResponse() |
||
31 | { |
||
32 | $factory = new HttplugFactory(); |
||
33 | $r = $factory->createResponse(217, 'Perfect', ['Content-Type' => 'text/html'], 'foobar', '2.0'); |
||
34 | |||
35 | $this->assertEquals(217, $r->getStatusCode()); |
||
36 | $this->assertEquals('Perfect', $r->getReasonPhrase()); |
||
37 | $this->assertEquals('2.0', $r->getProtocolVersion()); |
||
38 | $this->assertEquals('foobar', $r->getBody()->__toString()); |
||
39 | |||
40 | $headers = $r->getHeaders(); |
||
41 | $this->assertCount(1, $headers); |
||
42 | $this->assertArrayHasKey('Content-Type', $headers); |
||
43 | $this->assertEquals('text/html', $headers['Content-Type'][0]); |
||
44 | } |
||
45 | |||
46 | public function testCreateStream() |
||
47 | { |
||
48 | $factory = new HttplugFactory(); |
||
49 | $stream = $factory->createStream('foobar'); |
||
50 | |||
51 | $this->assertInstanceOf(StreamInterface::class, $stream); |
||
52 | $this->assertEquals('foobar', $stream->__toString()); |
||
53 | } |
||
54 | |||
55 | public function testCreateUri() |
||
56 | { |
||
57 | $factory = new HttplugFactory(); |
||
58 | $uri = $factory->createUri('https://nyholm.tech/foo'); |
||
59 | |||
60 | $this->assertInstanceOf(UriInterface::class, $uri); |
||
61 | $this->assertEquals('https://nyholm.tech/foo', $uri->__toString()); |
||
62 | } |
||
63 | } |
||
64 |