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 | class Abbreviation extends DataObject implements Shortcodable |
||
|
|||
4 | { |
||
5 | |||
6 | private static $db = array( |
||
7 | 'Title' => 'Varchar(255)', |
||
8 | 'Description' => 'Varchar(255)', |
||
9 | 'URLSlug' => 'Varchar(255)', |
||
10 | 'Explanation' => 'HTMLText' |
||
11 | ); |
||
12 | |||
13 | private static $has_one = array( |
||
14 | 'Page' => 'Page' |
||
15 | ); |
||
16 | |||
17 | private static $singular_name = 'Abbreviation'; |
||
18 | |||
19 | private static $plural_name = 'Abbreviations'; |
||
20 | |||
21 | private static $summary_fields = array('Title', 'Description'); |
||
22 | |||
23 | private static $searchable_fields = array('Title', 'Description'); |
||
24 | |||
25 | public function getCMSFields() |
||
31 | |||
32 | /** |
||
33 | * Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php |
||
34 | */ |
||
35 | public function Slug($regen=false) |
||
40 | |||
41 | /** |
||
42 | * Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php |
||
43 | */ |
||
44 | public function onBeforeWrite() |
||
61 | |||
62 | public function Link() |
||
66 | |||
67 | public function AbsoluteLink() |
||
71 | |||
72 | public function forTemplate() |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Used for Breadcrumbs |
||
81 | * |
||
82 | * @return DBField |
||
83 | */ |
||
84 | public function getMenuTitle() |
||
88 | |||
89 | /** |
||
90 | * Returns the first letter of the module title, used for grouping. |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getTitleFirstLetter() |
||
97 | |||
98 | /** |
||
99 | * Parse the shortcode and render as a string, probably with a template |
||
100 | * @param array $arguments the list of attributes of the shortcode |
||
101 | * @param string $content the shortcode content |
||
102 | * @param ShortcodeParser $parser the ShortcodeParser instance |
||
103 | * @param string $shortcode the raw shortcode being parsed |
||
104 | * @return String |
||
105 | **/ |
||
106 | View Code Duplication | public static function parse_shortcode($arguments, $content, $parser, $shortcode) |
|
127 | |||
128 | /** |
||
129 | * returns a list of fields for editing the shortcode's attributes |
||
130 | * @return Fieldlist |
||
131 | **/ |
||
132 | public static function shortcode_attribute_fields() |
||
136 | } |
||
137 |
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.