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 |
||
3 | namespace Dynamic\ProductCatalog\Docs; |
||
4 | |||
5 | use SilverStripe\Security\Permission; |
||
6 | use SilverStripe\Security\PermissionProvider; |
||
7 | |||
8 | View Code Duplication | class SpecSheet extends ProductDoc implements PermissionProvider |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private static $singular_name = 'Spec Sheet'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | 1 | private static $plural_name = 'Spec Sheets'; |
|
19 | |||
20 | 1 | /** |
|
21 | * @return FieldList |
||
22 | 1 | */ |
|
23 | public function getCMSFields() |
||
31 | |||
32 | /** |
||
33 | 1 | * @return array |
|
34 | 1 | */ |
|
35 | 1 | public function providePermissions() |
|
43 | |||
44 | 1 | /** |
|
45 | * @param null $member |
||
46 | 1 | * |
|
47 | * @return bool|int |
||
48 | */ |
||
49 | public function canCreate($member = null, $context = []) |
||
53 | |||
54 | 1 | /** |
|
55 | * @param null $member |
||
56 | 1 | * |
|
57 | * @return bool|int |
||
58 | */ |
||
59 | public function canEdit($member = null, $context = []) |
||
63 | |||
64 | 1 | /** |
|
65 | * @param null $member |
||
66 | 1 | * |
|
67 | * @return bool|int |
||
68 | */ |
||
69 | public function canDelete($member = null, $context = []) |
||
73 | |||
74 | 1 | /** |
|
75 | * @param null $member |
||
76 | 1 | * |
|
77 | * @return bool |
||
78 | */ |
||
79 | 1 | public function canView($member = null, $context = []) |
|
84 |
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.