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 |
||
6 | class ElementalWidgetExtension extends DataExtension |
||
|
|||
7 | { |
||
8 | /** |
||
9 | * Basic permissions, defaults to page perms where possible |
||
10 | */ |
||
11 | View Code Duplication | public function canView($member = null) |
|
21 | |||
22 | /** |
||
23 | * Basic permissions, defaults to page perms where possible |
||
24 | */ |
||
25 | View Code Duplication | public function canEdit($member = null) |
|
35 | |||
36 | /** |
||
37 | * Basic permissions, defaults to page perms where possible |
||
38 | * Uses archive not delete so that current stage is respected |
||
39 | * i.e if a widget is not published, then it can be deleted by someone who |
||
40 | * doesn't have publishing permissions |
||
41 | */ |
||
42 | View Code Duplication | public function canDelete($member = null) |
|
52 | |||
53 | /** |
||
54 | * Basic permissions, defaults to page perms where possible |
||
55 | */ |
||
56 | public function canCreate($member = null) |
||
62 | |||
63 | /** |
||
64 | * Handles unpublishing as VersionedDataObjects doesn't |
||
65 | * Modelled on SiteTree::doUnpublish |
||
66 | * Has to be applied here, rather than BaseElement so that goes against Widget |
||
67 | */ |
||
68 | public function doUnpublish() { |
||
89 | |||
90 | } |
||
91 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.