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 InfluxDbEventListenerTest extends \PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | |||
16 | public function test_listening_for_udp_infuxdb_event() |
||
17 | { |
||
18 | $event = new InfluxDbEvent(new PointsCollection(), Database::PRECISION_SECONDS, ClientInterface::UDP_CLIENT); |
||
19 | |||
20 | $httpWriter = $this->prophesize(WriterClient::class); |
||
21 | $httpWriter->write(Argument::cetera()) |
||
22 | ->shouldNotBeCalled(); |
||
23 | |||
24 | $udpWriter = $this->prophesize(WriterClient::class); |
||
25 | $udpWriter->write(Argument::type(PointsCollection::class), Database::PRECISION_SECONDS) |
||
26 | ->shouldBeCalledTimes(1) |
||
27 | ->willReturn(true); |
||
28 | |||
29 | $listener = new InfluxDbEventListener($httpWriter->reveal(), $udpWriter->reveal()); |
||
30 | $listener->onInfluxDbEventDispatched($event); |
||
31 | } |
||
32 | |||
33 | public function test_listening_for_http_infuxdb_event() |
||
34 | { |
||
35 | $event = new InfluxDbEvent(new PointsCollection(), Database::PRECISION_SECONDS, ClientInterface::HTTP_CLIENT); |
||
36 | |||
37 | $udpWriter = $this->prophesize(WriterClient::class); |
||
38 | $udpWriter->write(Argument::cetera()) |
||
39 | ->shouldNotBeCalled(); |
||
40 | |||
41 | $httpWriter = $this->prophesize(WriterClient::class); |
||
42 | $httpWriter->write(Argument::type(PointsCollection::class), Database::PRECISION_SECONDS) |
||
43 | ->shouldBeCalledTimes(1) |
||
44 | ->willReturn(true); |
||
45 | |||
46 | $listener = new InfluxDbEventListener($httpWriter->reveal(), $udpWriter->reveal()); |
||
47 | $listener->onInfluxDbEventDispatched($event); |
||
48 | } |
||
49 | |||
50 | } |
||
51 |