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 |
||
16 | class SpiderTest extends TestCase |
||
17 | { |
||
18 | /** |
||
19 | * @var Spider |
||
20 | */ |
||
21 | protected $spider; |
||
22 | |||
23 | /** |
||
24 | * @var logHandler |
||
25 | */ |
||
26 | protected $logHandler; |
||
27 | |||
28 | /** |
||
29 | * @var StatsHandler |
||
30 | */ |
||
31 | protected $statsHandler; |
||
32 | |||
33 | /** |
||
34 | * @var PHPUnit_Framework_MockObject_MockObject |
||
35 | */ |
||
36 | protected $requestHandler; |
||
37 | |||
38 | /** @var DiscoveredUri */ |
||
39 | protected $linkA; |
||
40 | /** @var DiscoveredUri */ |
||
41 | protected $linkB; |
||
42 | /** @var DiscoveredUri */ |
||
43 | protected $linkC; |
||
44 | /** @var DiscoveredUri */ |
||
45 | protected $linkD; |
||
46 | /** @var DiscoveredUri */ |
||
47 | protected $linkE; |
||
48 | /** @var DiscoveredUri */ |
||
49 | protected $linkF; |
||
50 | /** @var DiscoveredUri */ |
||
51 | protected $linkG; |
||
52 | |||
53 | /** @var Response */ |
||
54 | protected $responseA; |
||
55 | /** @var Response */ |
||
56 | protected $responseB; |
||
57 | /** @var Response */ |
||
58 | protected $responseC; |
||
59 | /** @var Response */ |
||
60 | protected $responseD; |
||
61 | /** @var Response */ |
||
62 | protected $responseE; |
||
63 | /** @var Response */ |
||
64 | protected $responseF; |
||
65 | /** @var Response */ |
||
66 | protected $responseG; |
||
67 | |||
68 | /** @var string */ |
||
69 | protected $hrefA; |
||
70 | protected $hrefB; |
||
71 | protected $hrefC; |
||
72 | protected $hrefD; |
||
73 | protected $hrefE; |
||
74 | protected $hrefF; |
||
75 | protected $hrefG; |
||
76 | |||
77 | /** |
||
78 | * @var array An associative array, containing a map of $this->linkX to $this->responseX. |
||
79 | */ |
||
80 | protected $linkToResponseMap = []; |
||
81 | |||
82 | /** |
||
83 | * Sets up the fixture, for example, opens a network connection. |
||
84 | * This method is called before a test is executed. |
||
85 | * |
||
86 | * Setting up the following structure: |
||
87 | * |
||
88 | * 0: A |
||
89 | * /|\ |
||
90 | * 1: B C E |
||
91 | * /| | | |
||
92 | * 2: D F G | |
||
93 | * | _ | |
||
94 | * |
||
95 | * Note: E links to F. |
||
96 | */ |
||
97 | protected function setUp() |
||
175 | |||
176 | /** |
||
177 | * @return Resource |
||
178 | * @throws \ErrorException |
||
179 | */ |
||
180 | public function doTestRequest() |
||
190 | |||
191 | /** |
||
192 | * @covers VDB\Spider\Spider |
||
193 | * |
||
194 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
195 | */ |
||
196 | View Code Duplication | public function testCrawlDFSDefaultBehaviour() |
|
214 | |||
215 | /** |
||
216 | * @covers VDB\Spider\Spider |
||
217 | */ |
||
218 | View Code Duplication | public function testCrawlBFSDefaultBehaviour() |
|
237 | |||
238 | private function compareUriArray($expected, $actual) |
||
244 | |||
245 | /** |
||
246 | * @covers VDB\Spider\Spider |
||
247 | * |
||
248 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
249 | * |
||
250 | * Given the following structure: |
||
251 | * |
||
252 | * 0: A |
||
253 | * /|\ |
||
254 | * 1: B C E |
||
255 | * /| | | |
||
256 | * 2: D F G | |
||
257 | * | _ | |
||
258 | * |
||
259 | * We expect the following result: A, E, C, B |
||
260 | * |
||
261 | */ |
||
262 | public function testCrawlDFSMaxDepthOne() |
||
277 | |||
278 | /** |
||
279 | * @covers VDB\Spider\Spider |
||
280 | */ |
||
281 | View Code Duplication | public function testCrawlBFSMaxDepthOne() |
|
297 | |||
298 | /** |
||
299 | * @covers VDB\Spider\Spider |
||
300 | */ |
||
301 | View Code Duplication | public function testCrawlDFSMaxQueueSize() |
|
316 | |||
317 | /** |
||
318 | * @covers VDB\Spider\Spider |
||
319 | */ |
||
320 | View Code Duplication | public function testCrawlBFSMaxQueueSize() |
|
336 | |||
337 | /** |
||
338 | * @covers VDB\Spider\Spider |
||
339 | */ |
||
340 | public function testCrawlFailedRequest() |
||
356 | } |
||
357 |