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 |
||
28 | abstract class AbstractFeedTypeTestCase extends TestCase |
||
29 | { |
||
30 | /** |
||
31 | * @var string[] |
||
32 | */ |
||
33 | protected $loadedFixtures = []; |
||
34 | |||
35 | /** |
||
36 | * @inheritdoc |
||
37 | */ |
||
38 | protected function setUp() |
||
44 | |||
45 | /** |
||
46 | * Removes all downloaded fixtures (they're saved in tmp folder). |
||
47 | */ |
||
48 | protected function tearDown() |
||
56 | |||
57 | /** |
||
58 | * @param ItemFixture $fixture |
||
59 | */ |
||
60 | protected function assertOriginalId(ItemFixture $fixture) |
||
67 | |||
68 | /** |
||
69 | * @param ItemFixture $fixture |
||
70 | */ |
||
71 | protected function assertOriginalUrl(ItemFixture $fixture) |
||
78 | |||
79 | /** |
||
80 | * @param ItemFixture $fixture |
||
81 | */ |
||
82 | View Code Duplication | protected function assertFixture(ItemFixture $fixture) |
|
112 | |||
113 | /** |
||
114 | * Asserts a value. |
||
115 | * |
||
116 | * @param $key |
||
117 | * @param $expectedValue |
||
118 | * @param $actualValue |
||
119 | */ |
||
120 | View Code Duplication | protected function assertValue($key, $expectedValue, $actualValue) |
|
135 | |||
136 | /** |
||
137 | * Normalizes values before asserting them. |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @param mixed $expectedValue |
||
141 | * @param mixed $actualValue |
||
142 | */ |
||
143 | View Code Duplication | protected function normalizeValues($key, &$expectedValue, &$actualValue) |
|
163 | |||
164 | /** |
||
165 | * @param string $name The fixture name |
||
166 | * @param string $feedType The feed type |
||
167 | * |
||
168 | * @throws \RuntimeException |
||
169 | * |
||
170 | * @return ItemFixture |
||
171 | */ |
||
172 | View Code Duplication | protected function getItemFixture($name, $feedType) |
|
183 | |||
184 | /** |
||
185 | * @param Feed $feedEntity |
||
186 | * @param string $name The fixture name |
||
187 | * @param string $feedType The feed type |
||
188 | * |
||
189 | * @throws \RuntimeException |
||
190 | * |
||
191 | * @return ItemBag |
||
192 | */ |
||
193 | protected function getActualItemFixture(Feed $feedEntity, $name, $feedType) |
||
212 | |||
213 | /** |
||
214 | * @param Feed $feed |
||
215 | * @param string $name |
||
216 | * @param string $feedType |
||
217 | * |
||
218 | * @return FeedItemBag |
||
219 | */ |
||
220 | protected function getExpectedItemFixture(Feed $feed, $name, $feedType) |
||
240 | |||
241 | /** |
||
242 | * Returns a feed entity for a specific feed type. |
||
243 | * |
||
244 | * @param string $type |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | protected function getFeed($type) |
||
258 | |||
259 | /** |
||
260 | * @return EventDispatcherInterface |
||
261 | */ |
||
262 | protected function createEventDispatcher() |
||
280 | |||
281 | /** |
||
282 | * @param Feed $feed |
||
283 | * @param string $fixtureName |
||
284 | * @param EventDispatcherInterface $dispatcher |
||
285 | * @param array $options |
||
286 | * |
||
287 | * @return ReaderInterface |
||
288 | */ |
||
289 | protected function createReader(Feed $feed, $fixtureName, EventDispatcherInterface $dispatcher, array $options = []) |
||
302 | |||
303 | /** |
||
304 | * @return ImportRegistry |
||
305 | */ |
||
306 | protected function getImportRegistry() |
||
310 | |||
311 | /** |
||
312 | * @param ItemBag $item |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | protected function getOriginalId(ItemBag $item) |
||
320 | |||
321 | /** |
||
322 | * @param ItemBag $item |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | protected function getOriginalUrl(ItemBag $item) |
||
330 | |||
331 | /** |
||
332 | * @param Feed $feed |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function getFeedTypeOptions(Feed $feed) |
||
347 | |||
348 | /** |
||
349 | * @param Feed $feed |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function getReaderTypeOptions(Feed $feed) |
||
363 | } |
||
364 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: