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 | final class MappedHeaderStrategyTest extends TestCase |
||
14 | { |
||
15 | const HEADER_MAP = [ |
||
16 | 'id' => 'Book ID', |
||
17 | 'author' => 'Author', |
||
18 | 'title' => 'Title', |
||
19 | 'genre' => 'Genre', |
||
20 | 'price' => 'Price', |
||
21 | 'publish_date' => 'Publish Date', |
||
22 | 'description' => 'Description', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @test |
||
27 | * @covers ::getHeaders |
||
28 | */ |
||
29 | public function getHeaders() |
||
35 | |||
36 | /** |
||
37 | * @test |
||
38 | * @covers ::isHeaderRow |
||
39 | */ |
||
40 | public function rowIsHeaderRow() |
||
45 | |||
46 | /** |
||
47 | * @test |
||
48 | * @covers ::isHeaderRow |
||
49 | */ |
||
50 | View Code Duplication | public function rowIsNotHeaderRow() |
|
57 | |||
58 | /** |
||
59 | * @test |
||
60 | * @covers ::createDataRow |
||
61 | */ |
||
62 | View Code Duplication | public function createDataRow() |
|
63 | { |
||
64 | $row = [ |
||
65 | 'bk101', |
||
66 | 'Gambardella, Matthew', |
||
67 | 'XML Developer\'s Guide', |
||
68 | 'Computer', |
||
69 | '44.95', |
||
70 | '2000-10-01', |
||
71 | 'An in-depth look at creating applications with XML.', |
||
72 | ]; |
||
73 | $strategy = $this->getStrategy(); |
||
74 | $this->assertSame( |
||
75 | [ |
||
76 | 'Book ID' => 'bk101', |
||
77 | 'Author' => 'Gambardella, Matthew', |
||
78 | 'Title' => 'XML Developer\'s Guide', |
||
79 | 'Genre' => 'Computer', |
||
80 | 'Price' => '44.95', |
||
81 | 'Publish Date' => '2000-10-01', |
||
82 | 'Description' => 'An in-depth look at creating applications with XML.', |
||
83 | ], |
||
84 | $strategy->createDataRow($row) |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | private function getFileObject() : SplFileObject |
|
95 | |||
96 | private function getStrategy() : MappedHeaderStrategy |
||
100 | } |
||
101 |
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.