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:
Complex classes like ProductQuestion often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductQuestion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class ProductQuestion extends DataObject |
||
|
|
|||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Standard SS variable. |
||
| 14 | */ |
||
| 15 | private static $db = array( |
||
| 16 | 'InternalCode' => 'Varchar(30)', |
||
| 17 | 'Question' => 'Varchar(255)', |
||
| 18 | 'AnswerRequired' => 'Boolean', |
||
| 19 | 'Label' => 'Varchar(100)', |
||
| 20 | 'DefaultAnswer' => 'Varchar(150)', |
||
| 21 | 'DefaultFormField' => 'Varchar(255)', |
||
| 22 | 'Options' => 'Text', |
||
| 23 | "HasImages" => "Boolean", |
||
| 24 | "ApplyToAllProducts" => "Boolean" |
||
| 25 | ); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Standard SS variable. |
||
| 29 | */ |
||
| 30 | private static $casting = array( |
||
| 31 | 'Title' => 'Varchar', |
||
| 32 | 'FullName' => 'Varchar' |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Standard SS variable. |
||
| 37 | * Links questions to products |
||
| 38 | */ |
||
| 39 | private static $many_many = array( |
||
| 40 | 'Products' => 'Product' |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Standard SS variable. |
||
| 45 | * Links to folder for images |
||
| 46 | */ |
||
| 47 | private static $has_one = array( |
||
| 48 | 'Folder' => 'Folder' |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Standard SS variable. |
||
| 53 | * Links questions to products |
||
| 54 | */ |
||
| 55 | private static $summary_fields = array( |
||
| 56 | 'InternalCode' => 'InternalCode', |
||
| 57 | 'Question' => 'Question' |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Standard SS variable. |
||
| 62 | * Links questions to products |
||
| 63 | */ |
||
| 64 | private static $searchable_fields = array( |
||
| 65 | 'InternalCode' => 'PartialMatch', |
||
| 66 | 'Question' => 'Varchar(255)', |
||
| 67 | 'Label' => 'PartialMatch', |
||
| 68 | 'DefaultAnswer' => 'PartialMatch', |
||
| 69 | 'DefaultFormField' => 'PartialMatch', |
||
| 70 | 'Options' => 'PartialMatch', |
||
| 71 | "HasImages" => true |
||
| 72 | ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Standard SS variable. |
||
| 76 | */ |
||
| 77 | private static $default_sort = [ |
||
| 78 | 'Question' => 'ASC', |
||
| 79 | 'ID' => 'ASC' |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Standard SS variable. |
||
| 84 | */ |
||
| 85 | private static $defaults = array( |
||
| 86 | "DefaultFormField" => "TextField", |
||
| 87 | "DefaultAnswer" => "tba" |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * form fields used without a list |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private static $available_form_fields_free = array( |
||
| 95 | "TextField" => "Text Field", |
||
| 96 | "TextareaField" => "Text Field with several lines", |
||
| 97 | "DateField" => "Date Field", |
||
| 98 | "EmailField" => "Email Field" |
||
| 99 | ); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * form fields used with a list |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private static $available_form_fields_list = array( |
||
| 106 | "DropdownField" => "Dropdown Field", |
||
| 107 | "OptionSetField" => "Option list Field" |
||
| 108 | ); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Maximum number of products on the site before the CheckboxSetField is |
||
| 112 | * replaced with a GridField. |
||
| 113 | * This field is used for selecting the products the question applies to. |
||
| 114 | * |
||
| 115 | * @var Int |
||
| 116 | */ |
||
| 117 | private static $max_products_for_checkbox_set_field = 300; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Standard SS variable. |
||
| 121 | */ |
||
| 122 | private static $singular_name = "Product Question"; |
||
| 123 | public function i18n_singular_name() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Standard SS variable. |
||
| 130 | */ |
||
| 131 | private static $plural_name = "Product Questions"; |
||
| 132 | public function i18n_plural_name() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * turns an option into potential file names |
||
| 144 | * e.g. red |
||
| 145 | * returns |
||
| 146 | * red.jpg, red.png, red.gif |
||
| 147 | * @param String $option |
||
| 148 | * @return Array |
||
| 149 | */ |
||
| 150 | public static function create_file_array_from_option($option) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Standard SS method |
||
| 169 | * @return FieldSet |
||
| 170 | */ |
||
| 171 | public function getCMSFields() |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * casted variable |
||
| 316 | * @return String |
||
| 317 | */ |
||
| 318 | public function FullName() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * casted variable |
||
| 329 | * @return String |
||
| 330 | */ |
||
| 331 | public function Title() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * |
||
| 342 | * @return FormField |
||
| 343 | */ |
||
| 344 | public function getFieldForProduct($product, $value = null) |
||
| 376 | |||
| 377 | public function getFieldForProductName(Product $product) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * |
||
| 384 | * making sure all the data is clean |
||
| 385 | */ |
||
| 386 | public function onBeforeWrite() |
||
| 409 | |||
| 410 | public function onAfterWrite() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * link to edit the record |
||
| 420 | * @param String | Null $action - e.g. edit |
||
| 421 | * @return String |
||
| 422 | */ |
||
| 423 | public function CMSEditLink($action = null) |
||
| 431 | } |
||
| 432 |
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.