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 |
||
18 | class AssignmentTest extends TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $_roleName = 'admin'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $_permissionName = 'viewArticles'; |
||
29 | |||
30 | // Tests : |
||
31 | |||
32 | View Code Duplication | public function testAssignRole() |
|
44 | |||
45 | View Code Duplication | public function testAssignPermission() |
|
57 | |||
58 | /** |
||
59 | * @depends testAssignRole |
||
60 | * @depends testAssignPermission |
||
61 | * |
||
62 | * @param AssignmentModel $role |
||
63 | * @param AssignmentModel $permission |
||
64 | */ |
||
65 | public function testGetItems(AssignmentModel $role, AssignmentModel $permission) |
||
70 | |||
71 | /** |
||
72 | * @depends testAssignRole |
||
73 | * |
||
74 | * @param AssignmentModel $model |
||
75 | */ |
||
76 | public function testRevokeRole(AssignmentModel $model) |
||
80 | |||
81 | /** |
||
82 | * @depends testAssignPermission |
||
83 | * |
||
84 | * @param AssignmentModel $model |
||
85 | */ |
||
86 | public function testRevokePermission(AssignmentModel $model) |
||
90 | |||
91 | /** |
||
92 | * Create role for testing purposes |
||
93 | * |
||
94 | * @throws Exception |
||
95 | */ |
||
96 | View Code Duplication | private function createRole() |
|
106 | |||
107 | /** |
||
108 | * Create permission for testing purposes |
||
109 | * |
||
110 | * @throws Exception |
||
111 | */ |
||
112 | View Code Duplication | private function createPermission() |
|
122 | } |
||
123 |
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.