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 |
||
| 9 | class SilverstripeColumnsPageControllerExtension extends Extension |
||
|
|
|||
| 10 | { |
||
| 11 | |||
| 12 | private static $allowed_actions = [ |
||
| 13 | 'myspecificpagemenuitems' => true |
||
| 14 | ]; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @return bool |
||
| 18 | */ |
||
| 19 | View Code Duplication | function HasFullWidthContent() |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return bool |
||
| 34 | */ |
||
| 35 | function HasSideBar() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param boolean $asClassName |
||
| 57 | * |
||
| 58 | * @return string | int |
||
| 59 | */ |
||
| 60 | function NumberOfColumns($asClassName = true) |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * returns a data list of items that have been edited last - up to one day ago. |
||
| 88 | * This ensures that we do not show stuff we have just fixed up... |
||
| 89 | * @return DataList |
||
| 90 | */ |
||
| 91 | function RecentlyUpdated($limit = 5) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Children Menu Items |
||
| 112 | * @return null | DataList |
||
| 113 | */ |
||
| 114 | View Code Duplication | function InThisSection() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Sibling Menu Items |
||
| 128 | * @return null | DataList |
||
| 129 | */ |
||
| 130 | function AlsoSee() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * returns relevant menus items for |
||
| 150 | * @param SS_Request |
||
| 151 | * @return string (html) |
||
| 152 | */ |
||
| 153 | function myspecificpagemenuitems($request) |
||
| 166 | |||
| 167 | |||
| 168 | } |
||
| 169 |
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.