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 StatsHandler |
||
24 | */ |
||
25 | protected $statsHandler; |
||
26 | |||
27 | /** |
||
28 | * @var PHPUnit_Framework_MockObject_MockObject |
||
29 | */ |
||
30 | protected $requestHandler; |
||
31 | |||
32 | /** @var FilterableUri */ |
||
33 | protected $linkA; |
||
34 | /** @var FilterableUri */ |
||
35 | protected $linkB; |
||
36 | /** @var FilterableUri */ |
||
37 | protected $linkC; |
||
38 | /** @var FilterableUri */ |
||
39 | protected $linkD; |
||
40 | /** @var FilterableUri */ |
||
41 | protected $linkE; |
||
42 | /** @var FilterableUri */ |
||
43 | protected $linkF; |
||
44 | /** @var FilterableUri */ |
||
45 | protected $linkG; |
||
46 | |||
47 | /** @var Response */ |
||
48 | protected $responseA; |
||
49 | /** @var Response */ |
||
50 | protected $responseB; |
||
51 | /** @var Response */ |
||
52 | protected $responseC; |
||
53 | /** @var Response */ |
||
54 | protected $responseD; |
||
55 | /** @var Response */ |
||
56 | protected $responseE; |
||
57 | /** @var Response */ |
||
58 | protected $responseF; |
||
59 | /** @var Response */ |
||
60 | protected $responseG; |
||
61 | |||
62 | /** @var string */ |
||
63 | protected $hrefA; |
||
64 | protected $hrefB; |
||
65 | protected $hrefC; |
||
66 | protected $hrefD; |
||
67 | protected $hrefE; |
||
68 | protected $hrefF; |
||
69 | protected $hrefG; |
||
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 | protected function setUp() |
||
135 | |||
136 | /** |
||
137 | * @return Resource |
||
138 | * @throws \ErrorException |
||
139 | */ |
||
140 | public function doTestRequest() |
||
163 | |||
164 | /** |
||
165 | * @covers VDB\Spider\Spider::crawl |
||
166 | * |
||
167 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
168 | */ |
||
169 | View Code Duplication | public function testCrawlDFSDefaultBehaviour() |
|
1 ignored issue
–
show
|
|||
170 | { |
||
171 | $this->spider->getQueueManager()->maxDepth = 10; |
||
172 | |||
173 | $this->spider->crawl(); |
||
174 | |||
175 | $expected = array( |
||
176 | $this->linkA, |
||
177 | $this->linkE, |
||
178 | $this->linkF, |
||
179 | $this->linkC, |
||
180 | $this->linkG, |
||
181 | $this->linkB, |
||
182 | $this->linkD |
||
183 | ); |
||
184 | |||
185 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @covers VDB\Spider\Spider::crawl |
||
190 | * |
||
191 | */ |
||
192 | View Code Duplication | public function testCrawlBFSDefaultBehaviour() |
|
1 ignored issue
–
show
|
|||
193 | { |
||
194 | $this->spider->getQueueManager()->setTraversalAlgorithm(InMemoryQueueManager::ALGORITHM_BREADTH_FIRST); |
||
195 | $this->spider->getQueueManager()->maxDepth = 1000; |
||
196 | |||
197 | $this->spider->crawl(); |
||
198 | |||
199 | $expected = array( |
||
200 | $this->linkA, |
||
201 | $this->linkB, |
||
202 | $this->linkC, |
||
203 | $this->linkE, |
||
204 | $this->linkD, |
||
205 | $this->linkF, |
||
206 | $this->linkG |
||
207 | ); |
||
208 | |||
209 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @covers VDB\Spider\Spider::crawl |
||
214 | * |
||
215 | * Behaviour as explained here: https://en.wikipedia.org/wiki/Depth-first_search#Example |
||
216 | */ |
||
217 | public function testCrawlDFSMaxDepthOne() |
||
232 | |||
233 | public function testCrawlBFSMaxDepthOne() |
||
249 | |||
250 | /** |
||
251 | * @covers VDB\Spider\Spider::crawl |
||
252 | */ |
||
253 | View Code Duplication | public function testCrawlDFSMaxQueueSize() |
|
1 ignored issue
–
show
|
|||
254 | { |
||
255 | $this->spider->getQueueManager()->maxDepth = 1000; |
||
256 | $this->spider->downloadLimit = 3; |
||
257 | |||
258 | $this->spider->crawl(); |
||
259 | |||
260 | $expected = array( |
||
261 | $this->linkA, |
||
262 | $this->linkE, |
||
263 | $this->linkF, |
||
264 | ); |
||
265 | |||
266 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
267 | } |
||
268 | |||
269 | View Code Duplication | public function testCrawlBFSMaxQueueSize() |
|
1 ignored issue
–
show
|
|||
270 | { |
||
271 | $this->spider->getQueueManager()->setTraversalAlgorithm(InMemoryQueueManager::ALGORITHM_BREADTH_FIRST); |
||
272 | $this->spider->getQueueManager()->maxDepth = 1000; |
||
273 | $this->spider->downloadLimit = 3; |
||
274 | |||
275 | $this->spider->crawl(); |
||
276 | |||
277 | $expected = array( |
||
278 | $this->linkA, |
||
279 | $this->linkB, |
||
280 | $this->linkC, |
||
281 | ); |
||
282 | |||
283 | $this->assertEquals($expected, $this->statsHandler->getPersisted()); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @covers VDB\Spider\Spider::crawl |
||
288 | */ |
||
289 | public function testCrawlFailedRequest() |
||
305 | } |
||
306 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: