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 EvolutionReportControllerTest extends AbstractHttpControllerTestCase |
||
16 | { |
||
17 | 5 | public function setUp() |
|
28 | |||
29 | 1 | View Code Duplication | public function testGetWholeSectoralEvolutionActionCanBeAccessed() |
|
|||
30 | { |
||
31 | 1 | $this->dispatch('/informe/evolucion-sectores', '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(EvolutionReportController::class); |
|
36 | 1 | $this->assertControllerClass('EvolutionReportController'); |
|
37 | 1 | $this->assertMatchedRouteName('informe-evolucion-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('column_1', $data); |
|
46 | 1 | $this->assertArrayHasKey('colores', $data); |
|
47 | 1 | $this->assertArrayHasKey('column_2', $data); |
|
48 | 1 | } |
|
49 | |||
50 | 1 | View Code Duplication | public function testGetSectoralEvolutionActionCanBeAccessed() |
51 | { |
||
52 | 1 | $this->dispatch('/informe/evolucion-sector/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(EvolutionReportController::class); |
|
57 | 1 | $this->assertControllerClass('EvolutionReportController'); |
|
58 | 1 | $this->assertMatchedRouteName('informe-evolucion-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('column_1', $data); |
|
67 | 1 | $this->assertArrayHasKey('colores', $data); |
|
68 | 1 | $this->assertArrayHasKey('column_2', $data); |
|
69 | 1 | } |
|
70 | |||
71 | 1 | View Code Duplication | public function testGetSectoralEvolutionSubactivityActionCanBeAccessed() |
72 | { |
||
73 | 1 | $this->dispatch('/informe/evolucion-sector-subactividad/2', '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(EvolutionReportController::class); |
|
78 | 1 | $this->assertControllerClass('EvolutionReportController'); |
|
79 | 1 | $this->assertMatchedRouteName('informe-evolucion-sector-subactividad'); |
|
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('column_1', $data); |
|
88 | 1 | $this->assertArrayHasKey('groups', $data); |
|
89 | 1 | $this->assertArrayHasKey('column_3', $data); |
|
90 | 1 | } |
|
91 | |||
92 | 1 | public function testGetSectoralEvolutionSubactivityCategoryActionCanBeAccessed() |
|
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.