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 |
||
33 | class FileReaderTest extends TestCase |
||
34 | { |
||
35 | use CreateStreamTrait; |
||
36 | |||
37 | /** |
||
38 | * @param resource $stream |
||
39 | * @param ParserInterface $parser |
||
40 | * |
||
41 | * @return FileReader |
||
42 | */ |
||
43 | View Code Duplication | private function buildFactoryReader($stream, ParserInterface $parser) |
|
|
|||
44 | { |
||
45 | $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class); |
||
46 | $file->shouldReceive('getStream') |
||
47 | ->with('r') |
||
48 | ->andReturn($stream); |
||
49 | |||
50 | $format = m::mock(JsonFormat::class)->makePartial(); |
||
51 | |||
52 | $factory = m::mock(ParserFactoryInterface::class); |
||
53 | $factory->shouldReceive('getParser') |
||
54 | ->with($format) |
||
55 | ->andReturn($parser); |
||
56 | |||
57 | return new FileReader($file, $format, $factory); |
||
58 | } |
||
59 | |||
60 | public function testNodeStreamFileWillGetAStream() |
||
61 | { |
||
62 | $stream = m::mock(StreamInterface::class); |
||
63 | |||
64 | $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class); |
||
65 | $file->shouldReceive('getStream') |
||
66 | ->with('r') |
||
67 | ->andReturn($stream); |
||
68 | |||
69 | $format = new CsvFormat(); |
||
70 | $reader = new FileReader($file, $format); |
||
71 | |||
72 | static::assertInstanceOf(ReaderInterface::class, $reader); |
||
73 | } |
||
74 | |||
75 | public function testFileNodeWillGetAReadStream() |
||
93 | |||
94 | View Code Duplication | public function testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException() |
|
106 | |||
107 | public function testNodeWithFormatWillUseThatFormat() |
||
108 | { |
||
109 | $stream = fopen('php://memory', 'r'); |
||
110 | |||
111 | $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class); |
||
112 | $file->shouldReceive('getStream') |
||
113 | ->with('r') |
||
114 | ->andReturn($stream); |
||
115 | |||
116 | $format = new JsonFormat(['fileType' => JsonFormat::JSON_FILE_TYPE_EACH_LINE]); |
||
117 | |||
118 | $file->shouldReceive('getFormat') |
||
119 | ->andReturn($format); |
||
120 | |||
121 | $reader = new FileReader($file); |
||
122 | |||
123 | static::assertInstanceOf(ReaderInterface::class, $reader); |
||
124 | } |
||
125 | |||
126 | View Code Duplication | public function testProvidingAParserFactoryWillUseTheFactory() |
|
138 | |||
139 | public function testFetch() |
||
155 | |||
156 | public function testFetchAll() |
||
171 | } |
||
172 |
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.