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 |
||
10 | class HostOverrideListenerTest extends TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @var HostOverrideListener |
||
14 | */ |
||
15 | protected $object; |
||
16 | |||
17 | /** |
||
18 | * @var |
||
19 | */ |
||
20 | protected $session; |
||
21 | |||
22 | /** |
||
23 | * Sets up the fixture, for example, opens a network connection. |
||
24 | * This method is called before a test is executed. |
||
25 | */ |
||
26 | protected function setUp() |
||
29 | |||
30 | /** |
||
31 | * Tears down the fixture, for example, closes a network connection. |
||
32 | * This method is called after a test is executed. |
||
33 | */ |
||
34 | protected function tearDown() |
||
37 | |||
38 | /** |
||
39 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
40 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
41 | */ |
||
42 | View Code Duplication | public function testHostOverrideMessageIsSetForAdmin() |
|
55 | |||
56 | /** |
||
57 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
58 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
59 | */ |
||
60 | View Code Duplication | public function testHostOverrideMessageIsNotSetForAdminRedirectResponse() |
|
72 | |||
73 | /** |
||
74 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
75 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
76 | */ |
||
77 | View Code Duplication | public function testHostOverrideMessageIsNotSetForSubRequest() |
|
89 | |||
90 | /** |
||
91 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
92 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
93 | */ |
||
94 | View Code Duplication | public function testHostOverrideMessageIsNotSetForXmlRequest() |
|
106 | |||
107 | /** |
||
108 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
109 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
110 | */ |
||
111 | View Code Duplication | public function testHostOverrideMessageIsNotSetForPreview() |
|
123 | |||
124 | /** |
||
125 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct |
||
126 | * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse |
||
127 | */ |
||
128 | View Code Duplication | public function testHostOverrideMessageIsNotSetForFrontend() |
|
140 | |||
141 | private function getHostOverrideListener($flashBag) |
||
174 | |||
175 | private function getFilterResponseEvent($request, $response, $requestType = HttpKernelInterface::MASTER_REQUEST) |
||
191 | |||
192 | private function getResponse() |
||
196 | |||
197 | private function getRedirectResponse() |
||
205 | |||
206 | private function getXmlHttpRequest() |
||
213 | |||
214 | private function getAdminRequest() |
||
218 | |||
219 | private function getAdminPreviewRequest() |
||
223 | |||
224 | private function getFrontendRequest() |
||
228 | } |
||
229 |
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.