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 |
||
15 | class SpiderTest extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var Spider |
||
19 | */ |
||
20 | protected $spider; |
||
21 | |||
22 | /** |
||
23 | * @var PHPUnit_Framework_MockObject_MockObject |
||
24 | */ |
||
25 | protected $requestHandler; |
||
26 | |||
27 | /** @var DiscoveredUri */ |
||
28 | protected $linkA; |
||
29 | /** @var DiscoveredUri */ |
||
30 | protected $linkB; |
||
31 | /** @var DiscoveredUri */ |
||
32 | protected $linkC; |
||
33 | /** @var DiscoveredUri */ |
||
34 | protected $linkD; |
||
35 | /** @var DiscoveredUri */ |
||
36 | protected $linkE; |
||
37 | /** @var DiscoveredUri */ |
||
38 | protected $linkF; |
||
39 | /** @var DiscoveredUri */ |
||
40 | protected $linkG; |
||
41 | |||
42 | /** @var Response */ |
||
43 | protected $responseA; |
||
44 | /** @var Response */ |
||
45 | protected $responseB; |
||
46 | /** @var Response */ |
||
47 | protected $responseC; |
||
48 | /** @var Response */ |
||
49 | protected $responseD; |
||
50 | /** @var Response */ |
||
51 | protected $responseE; |
||
52 | /** @var Response */ |
||
53 | protected $responseF; |
||
54 | /** @var Response */ |
||
55 | protected $responseG; |
||
56 | |||
57 | /** @var string */ |
||
58 | protected $hrefA; |
||
59 | protected $hrefB; |
||
60 | protected $hrefC; |
||
61 | protected $hrefD; |
||
62 | protected $hrefE; |
||
63 | protected $hrefF; |
||
64 | protected $hrefG; |
||
65 | |||
66 | /** |
||
67 | * @var array An associative array, containing a map of $this->linkX to $this->responseX. |
||
68 | */ |
||
69 | protected $linkToResponseMap = []; |
||
70 | |||
71 | /** |
||
72 | * Sets up the fixture, for example, opens a network connection. |
||
73 | * This method is called before a test is executed. |
||
74 | * |
||
75 | * Setting up the following structure: |
||
76 | * |
||
77 | * 0: A |
||
78 | * /|\ |
||
79 | * 1: B C E |
||
80 | * /| | | |
||
81 | * 2: D F G | |
||
82 | * | _ | |
||
83 | * |
||
84 | * Note: E links to F. |
||
85 | */ |
||
86 | protected function setUp() |
||
154 | |||
155 | /** |
||
156 | * @return Resource |
||
157 | * @throws \ErrorException |
||
158 | */ |
||
159 | public function doTestRequest() |
||
169 | |||
170 | /** |
||
171 | * @covers VDB\Spider\Spider |
||
172 | * |
||
173 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
174 | */ |
||
175 | View Code Duplication | public function testCrawlDFSDefaultBehaviour() |
|
193 | |||
194 | /** |
||
195 | * @covers VDB\Spider\Spider |
||
196 | */ |
||
197 | View Code Duplication | public function testCrawlBFSDefaultBehaviour() |
|
216 | |||
217 | private function compareUriArray($expected, $actual) |
||
223 | |||
224 | /** |
||
225 | * @covers VDB\Spider\Spider |
||
226 | * |
||
227 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
228 | * |
||
229 | * Given the following structure: |
||
230 | * |
||
231 | * 0: A |
||
232 | * /|\ |
||
233 | * 1: B C E |
||
234 | * /| | | |
||
235 | * 2: D F G | |
||
236 | * | _ | |
||
237 | * |
||
238 | * We expect the following result: A, E, C, B |
||
239 | * |
||
240 | */ |
||
241 | public function testCrawlDFSMaxDepthOne() |
||
256 | |||
257 | /** |
||
258 | * @covers VDB\Spider\Spider |
||
259 | */ |
||
260 | View Code Duplication | public function testCrawlBFSMaxDepthOne() |
|
276 | |||
277 | /** |
||
278 | * @covers VDB\Spider\Spider |
||
279 | */ |
||
280 | View Code Duplication | public function testCrawlDFSMaxQueueSize() |
|
295 | |||
296 | /** |
||
297 | * @covers VDB\Spider\Spider |
||
298 | */ |
||
299 | View Code Duplication | public function testCrawlBFSMaxQueueSize() |
|
315 | |||
316 | /** |
||
317 | * @covers VDB\Spider\Spider |
||
318 | */ |
||
319 | public function testCrawlFailedRequest() |
||
332 | |||
333 | /** |
||
334 | * @covers VDB\Spider\Spider |
||
335 | * @covers VDB\Spider\Downloader\Downloader::getDispatcher |
||
336 | */ |
||
337 | public function testDownloaderEventDispatcher() |
||
345 | |||
346 | /** |
||
347 | * @covers VDB\Spider\Spider |
||
348 | * @covers VDB\Spider\QueueManager\InMemoryQueueManager::getDispatcher |
||
349 | */ |
||
350 | public function testQueueManagerEventDispatcher() |
||
358 | } |
||
359 |
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.