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 logHandler |
||
24 | */ |
||
25 | protected $logHandler; |
||
26 | |||
27 | /** |
||
28 | * @var StatsHandler |
||
29 | */ |
||
30 | protected $statsHandler; |
||
31 | |||
32 | /** |
||
33 | * @var PHPUnit_Framework_MockObject_MockObject |
||
34 | */ |
||
35 | protected $requestHandler; |
||
36 | |||
37 | /** @var FilterableUri */ |
||
38 | protected $linkA; |
||
39 | /** @var FilterableUri */ |
||
40 | protected $linkB; |
||
41 | /** @var FilterableUri */ |
||
42 | protected $linkC; |
||
43 | /** @var FilterableUri */ |
||
44 | protected $linkD; |
||
45 | /** @var FilterableUri */ |
||
46 | protected $linkE; |
||
47 | /** @var FilterableUri */ |
||
48 | protected $linkF; |
||
49 | /** @var FilterableUri */ |
||
50 | protected $linkG; |
||
51 | |||
52 | /** @var Response */ |
||
53 | protected $responseA; |
||
54 | /** @var Response */ |
||
55 | protected $responseB; |
||
56 | /** @var Response */ |
||
57 | protected $responseC; |
||
58 | /** @var Response */ |
||
59 | protected $responseD; |
||
60 | /** @var Response */ |
||
61 | protected $responseE; |
||
62 | /** @var Response */ |
||
63 | protected $responseF; |
||
64 | /** @var Response */ |
||
65 | protected $responseG; |
||
66 | |||
67 | /** @var string */ |
||
68 | protected $hrefA; |
||
69 | protected $hrefB; |
||
70 | protected $hrefC; |
||
71 | protected $hrefD; |
||
72 | protected $hrefE; |
||
73 | protected $hrefF; |
||
74 | protected $hrefG; |
||
75 | |||
76 | /** |
||
77 | * Sets up the fixture, for example, opens a network connection. |
||
78 | * This method is called before a test is executed. |
||
79 | * |
||
80 | * Setting up the following structure: |
||
81 | * |
||
82 | * 0: A |
||
83 | * /|\ |
||
84 | * 1: B C E |
||
85 | * /| | | |
||
86 | * 2: D F G | |
||
87 | * | _ | |
||
88 | * |
||
89 | * Note: E links to F. |
||
90 | */ |
||
91 | protected function setUp() |
||
159 | |||
160 | /** |
||
161 | * @return Resource |
||
162 | * @throws \ErrorException |
||
163 | */ |
||
164 | public function doTestRequest() |
||
187 | |||
188 | /** |
||
189 | * @covers VDB\Spider\Spider::crawl |
||
190 | * |
||
191 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
192 | */ |
||
193 | View Code Duplication | public function testCrawlDFSDefaultBehaviour() |
|
211 | |||
212 | /** |
||
213 | * @covers VDB\Spider\Spider::crawl |
||
214 | * |
||
215 | */ |
||
216 | View Code Duplication | public function testCrawlBFSDefaultBehaviour() |
|
235 | |||
236 | /** |
||
237 | * @covers VDB\Spider\Spider::crawl |
||
238 | * |
||
239 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
240 | * |
||
241 | * Given the following structure: |
||
242 | * |
||
243 | * 0: A |
||
244 | * /|\ |
||
245 | * 1: B C E |
||
246 | * /| | | |
||
247 | * 2: D F G | |
||
248 | * | _ | |
||
249 | * |
||
250 | * We expect the following result: A, E, C, B |
||
251 | * |
||
252 | */ |
||
253 | public function testCrawlDFSMaxDepthOne() |
||
254 | { |
||
255 | $this->spider->getDiscovererSet()->maxDepth = 1; |
||
256 | |||
257 | $this->spider->crawl(); |
||
258 | |||
259 | $expected = array( |
||
260 | $this->linkA, |
||
261 | $this->linkE, |
||
262 | $this->linkC, |
||
263 | $this->linkB, |
||
264 | ); |
||
265 | |||
266 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
267 | } |
||
268 | |||
269 | public function testCrawlBFSMaxDepthOne() |
||
285 | |||
286 | /** |
||
287 | * @covers VDB\Spider\Spider::crawl |
||
288 | */ |
||
289 | View Code Duplication | public function testCrawlDFSMaxQueueSize() |
|
1 ignored issue
–
show
|
|||
290 | { |
||
291 | $this->spider->getDiscovererSet()->maxDepth = 1000; |
||
292 | $this->spider->downloadLimit = 3; |
||
293 | |||
294 | $this->spider->crawl(); |
||
295 | |||
296 | $expected = array( |
||
297 | $this->linkA, |
||
298 | $this->linkE, |
||
299 | $this->linkF, |
||
300 | ); |
||
301 | |||
302 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
303 | } |
||
304 | |||
305 | View Code Duplication | public function testCrawlBFSMaxQueueSize() |
|
321 | |||
322 | /** |
||
323 | * @covers VDB\Spider\Spider::crawl |
||
324 | */ |
||
325 | public function testCrawlFailedRequest() |
||
341 | } |
||
342 |
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.