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 DistributionReportControllerTest extends AbstractHttpControllerTestCase |
||
16 | { |
||
17 | 5 | public function setUp() |
|
28 | |||
29 | 1 | View Code Duplication | public function testGetWholeSectoralDistributionActionActionCanBeAccessed() |
|
|||
30 | { |
||
31 | 1 | $this->dispatch('/informe/distribucion-sectores/2014', 'GET'); |
|
32 | 1 | $this->assertResponseStatusCode(200); |
|
33 | 1 | $this->assertResponseHeaderContains('Content-Type', 'application/json; charset=utf-8'); |
|
34 | 1 | $this->assertModuleName('api'); |
|
35 | 1 | $this->assertControllerName(DistributionReportController::class); |
|
36 | 1 | $this->assertControllerClass('DistributionReportController'); |
|
37 | 1 | $this->assertMatchedRouteName('informe-todos-sectores'); |
|
38 | |||
39 | 1 | $data = $this->getResponse()->getContent(); |
|
40 | |||
41 | 1 | $this->assertJson($data); |
|
42 | |||
43 | 1 | $data = Json::decode($data, Json::TYPE_ARRAY); |
|
44 | |||
45 | 1 | $this->assertArrayHasKey('sector_1', $data); |
|
46 | 1 | $this->assertArrayHasKey('colores', $data); |
|
47 | 1 | $this->assertArrayHasKey('descripciones', $data); |
|
48 | 1 | } |
|
49 | |||
50 | 1 | View Code Duplication | public function testGetSectoralDistributionActionCanBeAccessed() |
51 | { |
||
52 | 1 | $this->dispatch('/informe/distribucion-sector/2014/1', 'GET'); |
|
53 | 1 | $this->assertResponseStatusCode(200); |
|
54 | 1 | $this->assertResponseHeaderContains('Content-Type', 'application/json; charset=utf-8'); |
|
55 | 1 | $this->assertModuleName('api'); |
|
56 | 1 | $this->assertControllerName(DistributionReportController::class); |
|
57 | 1 | $this->assertControllerClass('DistributionReportController'); |
|
58 | 1 | $this->assertMatchedRouteName('informe-por-sector'); |
|
59 | |||
60 | 1 | $data = $this->getResponse()->getContent(); |
|
61 | |||
62 | 1 | $this->assertJson($data); |
|
63 | |||
64 | 1 | $data = Json::decode($data, Json::TYPE_ARRAY); |
|
65 | |||
66 | 1 | $this->assertArrayHasKey('graph_data', $data); |
|
67 | 1 | $this->assertArrayHasKey('sector', $data); |
|
68 | 1 | $this->assertArrayHasKey('totalActividades', $data); |
|
69 | 1 | } |
|
70 | |||
71 | 1 | View Code Duplication | public function testGetGasesDistributionActionCanBeAccessed() |
72 | { |
||
73 | 1 | $this->dispatch('/informe/distribucion-gases/2014', 'GET'); |
|
74 | 1 | $this->assertResponseStatusCode(200); |
|
75 | 1 | $this->assertResponseHeaderContains('Content-Type', 'application/json; charset=utf-8'); |
|
76 | 1 | $this->assertModuleName('api'); |
|
77 | 1 | $this->assertControllerName(DistributionReportController::class); |
|
78 | 1 | $this->assertControllerClass('DistributionReportController'); |
|
79 | 1 | $this->assertMatchedRouteName('informe-gas'); |
|
80 | |||
81 | 1 | $data = $this->getResponse()->getContent(); |
|
82 | |||
83 | 1 | $this->assertJson($data); |
|
84 | |||
85 | 1 | $data = Json::decode($data, Json::TYPE_ARRAY); |
|
86 | |||
87 | 1 | $this->assertArrayHasKey('colores', $data); |
|
88 | 1 | $this->assertArrayHasKey('gases', $data); |
|
89 | 1 | $this->assertArrayHasKey('valores', $data); |
|
90 | 1 | } |
|
91 | |||
92 | 1 | public function testGetSectoralGasesDistributionActionCanBeAccessed() |
|
108 | |||
109 | 1 | public function testInvalidRouteDoesNotCrash() |
|
114 | |||
115 | // public function testInvalidHttpVerbDoesNotCrash() |
||
116 | // { |
||
117 | // $this->dispatch('/informe/distribucion-sectores/2014', 'DELETE'); |
||
118 | // $this->assertResponseStatusCode(405); |
||
119 | // } |
||
120 | } |
||
121 |
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.