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 |
||
13 | class LogEntryTest extends SapphireTest |
||
14 | { |
||
15 | /** |
||
16 | * Test if the Permissions are an array and contain the view and delete permissions |
||
17 | */ |
||
18 | public function testProvidePermissions() |
||
25 | |||
26 | /** |
||
27 | * There's no reason to manually create, so don't allow manual creation |
||
28 | */ |
||
29 | View Code Duplication | public function testAllowCreate() |
|
37 | |||
38 | /** |
||
39 | * Test that LogEntry classes can not be edited |
||
40 | */ |
||
41 | public function testAllowEditing() |
||
45 | |||
46 | /** |
||
47 | * We can view if we're logged in as admin. Otherwise, no. |
||
48 | */ |
||
49 | View Code Duplication | public function testAllowView() |
|
57 | |||
58 | /** |
||
59 | * We can Delete if we're logged in as admin. Otherwise, no. |
||
60 | */ |
||
61 | View Code Duplication | public function testAllowDelete() |
|
69 | |||
70 | } |
||
71 |
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.