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 |
||
| 10 | class FormFieldExplanation extends DataObject |
||
|
|
|||
| 11 | { |
||
| 12 | public static $db = array( |
||
| 13 | "Name" => "Varchar(255)", |
||
| 14 | "Title" => "Varchar(255)", |
||
| 15 | "Explanation" => "Varchar(255)", |
||
| 16 | "AlternativeFieldLabel" => "Varchar(100)", |
||
| 17 | "CustomErrorMessage" => "Varchar(100)", |
||
| 18 | "CustomErrorMessageAdditional" => "Varchar(200)" |
||
| 19 | ); |
||
| 20 | |||
| 21 | public static $has_one = array( |
||
| 22 | "Parent" => "SiteTree" |
||
| 23 | ); |
||
| 24 | |||
| 25 | public static $indexes = array( |
||
| 26 | "Name" => true |
||
| 27 | ); |
||
| 28 | |||
| 29 | public static $searchable_fields = array( |
||
| 30 | "Title" => "PartialMatch" |
||
| 31 | ); |
||
| 32 | |||
| 33 | public static $field_labels = array( |
||
| 34 | "Name" => "Field Name", |
||
| 35 | "Title" => "Label", |
||
| 36 | "Explanation" => "Explanation", |
||
| 37 | "AlternativeFieldLabel" => "Alternative Field Label (if any - replaces standard field label)", |
||
| 38 | "CustomErrorMessage" => "Custom Error Message, shown when field is not entered", |
||
| 39 | "CustomErrorMessageAdditional" => "Additional (sub-heading) Custom Error Message, shown when field is not entered" |
||
| 40 | |||
| 41 | ); |
||
| 42 | public static $summary_fields = array( |
||
| 43 | "Title" => "Title" |
||
| 44 | ); |
||
| 45 | |||
| 46 | View Code Duplication | public function getCMSFields() |
|
| 54 | |||
| 55 | View Code Duplication | public function getFrontEndFields() |
|
| 63 | |||
| 64 | public static $singular_name = "Form Field Explanation"; |
||
| 65 | |||
| 66 | public static $plural_name = "Form Field Explanations"; |
||
| 67 | } |
||
| 68 |
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.