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 |
||
14 | class LogEntryTest extends SapphireTest |
||
15 | { |
||
16 | /** |
||
17 | * Test if the Permissions are an array and contain the view and delete permissions |
||
18 | */ |
||
19 | public function testProvidePermissions() |
||
26 | |||
27 | /** |
||
28 | * There's no reason to manually create, so don't allow manual creation |
||
29 | */ |
||
30 | public function testAllowCreate() |
||
38 | |||
39 | /** |
||
40 | * Test that LogEntry classes can not be edited |
||
41 | */ |
||
42 | public function testAllowEditing() |
||
46 | |||
47 | /** |
||
48 | * We can view if we're logged in as admin. Otherwise, no. |
||
49 | */ |
||
50 | View Code Duplication | public function testAllowView() |
|
60 | |||
61 | /** |
||
62 | * We can Delete if we're logged in as admin. Otherwise, no. |
||
63 | */ |
||
64 | View Code Duplication | public function testAllowDelete() |
|
74 | |||
75 | /** |
||
76 | * Ensure that the contents are JSON encoded and pretty printed, and that the CSS class is correct |
||
77 | */ |
||
78 | public function testLogEntriesAreFormattedAsJson() |
||
97 | } |
||
98 |
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.