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 | abstract class ParserTypeTestCase extends TestCase |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected static $url = 'http://example.org'; |
||
19 | |||
20 | /** |
||
21 | * @return string |
||
22 | */ |
||
23 | abstract protected function getParserType(); |
||
24 | |||
25 | /** |
||
26 | * @param Scraper $scraper |
||
27 | * |
||
28 | * @return ParserInterface |
||
29 | */ |
||
30 | View Code Duplication | protected function getParser(Scraper $scraper) |
|
37 | |||
38 | /** |
||
39 | * @param string $parser |
||
40 | * |
||
41 | * @return Scraper |
||
42 | */ |
||
43 | protected function getScraperEntity($parser) |
||
53 | |||
54 | /** |
||
55 | * @return string[] |
||
56 | */ |
||
57 | View Code Duplication | public function getFixtureNames() |
|
77 | |||
78 | /** |
||
79 | * @dataProvider getFixtureNames |
||
80 | * |
||
81 | * @param string $fixtureName |
||
82 | */ |
||
83 | public function testFixtures($fixtureName) |
||
87 | /** |
||
88 | * @param ItemFixture $fixture |
||
89 | */ |
||
90 | protected function assertOriginalId(ItemFixture $fixture) |
||
97 | /** |
||
98 | * @param ItemFixture $fixture |
||
99 | */ |
||
100 | protected function assertOriginalUrl(ItemFixture $fixture) |
||
107 | /** |
||
108 | * @param ItemFixture $fixture |
||
109 | */ |
||
110 | View Code Duplication | protected function assertFixture(ItemFixture $fixture) |
|
133 | |||
134 | /** |
||
135 | * Asserts a value. |
||
136 | * |
||
137 | * @param $key |
||
138 | * @param $expectedValue |
||
139 | * @param $actualValue |
||
140 | */ |
||
141 | View Code Duplication | protected function assertValue($key, $expectedValue, $actualValue) |
|
156 | |||
157 | /** |
||
158 | * Normalizes values before asserting them. |
||
159 | * |
||
160 | * @param string $key |
||
161 | * @param mixed $expectedValue |
||
162 | * @param mixed $actualValue |
||
163 | */ |
||
164 | View Code Duplication | protected function normalizeValues($key, &$expectedValue, &$actualValue) |
|
184 | |||
185 | /** |
||
186 | * @param string $parserType |
||
187 | * @param string $fixtureName |
||
188 | * |
||
189 | * @return ItemFixture |
||
190 | */ |
||
191 | View Code Duplication | protected function getItemFixture($parserType, $fixtureName) |
|
202 | |||
203 | /** |
||
204 | * @param Scraper $scraper |
||
205 | * @param string $parserType |
||
206 | * @param string $fixtureName |
||
207 | * |
||
208 | * @return ScrapedItemBag |
||
209 | */ |
||
210 | protected function getActualItemFixture(Scraper $scraper, $parserType, $fixtureName) |
||
222 | |||
223 | /** |
||
224 | * @param Scraper $scraper |
||
225 | * @param string $parserType |
||
226 | * @param string $fixtureName |
||
227 | * |
||
228 | * @return ScrapedItemBag |
||
229 | */ |
||
230 | protected function getExpectedItemFixture(Scraper $scraper, $parserType, $fixtureName) |
||
255 | } |
||
256 |
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.