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 |
||
11 | class ResourceTest extends TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @var Resource |
||
15 | */ |
||
16 | protected $resource; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $html; |
||
22 | |||
23 | View Code Duplication | protected function setUp() |
|
|
|||
24 | { |
||
25 | $this->html = file_get_contents(__DIR__ . '/Fixtures/ResourceTestHTMLResource.html'); |
||
26 | $this->resource = new Resource( |
||
27 | new DiscoveredUri(new Uri('/domains/special', 'http://example.org')), |
||
28 | new Response(200, [], $this->html) |
||
29 | ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @covers VDB\Spider\Resource |
||
34 | */ |
||
35 | public function testGetCrawler() |
||
39 | |||
40 | /** |
||
41 | * @covers VDB\Spider\Resource |
||
42 | */ |
||
43 | public function testGetUri() |
||
48 | |||
49 | /** |
||
50 | * @covers VDB\Spider\Resource |
||
51 | */ |
||
52 | public function testGetResponse() |
||
57 | |||
58 | /** |
||
59 | * @covers VDB\Spider\Resource |
||
60 | */ |
||
61 | public function testSerialization() |
||
73 | } |
||
74 |
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.