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 AdminRouteHelperTest extends TestCase |
||
12 | { |
||
13 | protected static $ADMIN_KEY = 'admin'; |
||
14 | |||
15 | protected static $ALTERNATIVE_ADMIN_KEY = 'vip'; |
||
16 | |||
17 | protected static $NON_ADMIN_URL = '/en/some_path/%s/nodes'; |
||
18 | |||
19 | protected static $ADMIN_URL = '/en/%s/nodes'; |
||
20 | |||
21 | protected static $PREVIEW_ADMIN_URL = '/en/%s/preview/blog/page/1'; |
||
22 | |||
23 | /** |
||
24 | * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute |
||
25 | */ |
||
26 | View Code Duplication | public function testIsAdminRouteReturnsTrueWhenAdminUrl() |
|
36 | |||
37 | /** |
||
38 | * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute |
||
39 | */ |
||
40 | View Code Duplication | public function testIsAdminRouteReturnsFalseWhenFrontendUrl() |
|
50 | |||
51 | /** |
||
52 | * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute |
||
53 | */ |
||
54 | public function testIsAdminRouteReturnsFalseWhenPreviewUrl() |
||
67 | |||
68 | private function getAdminRouteHelper($adminKey) |
||
72 | |||
73 | private function getRequestStack() |
||
80 | |||
81 | private function getRequest() |
||
85 | |||
86 | private function getPreviewRequest() |
||
90 | } |
||
91 |
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.