Complex classes like OrderStatusLog 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 OrderStatusLog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class OrderStatusLog extends DataObject implements EditableEcommerceObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * standard SS variable. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private static $db = array( |
||
|
|
|||
| 19 | 'Title' => 'Varchar(100)', |
||
| 20 | 'Note' => 'HTMLText', |
||
| 21 | 'InternalUseOnly' => 'Boolean', |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * standard SS variable. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $has_one = array( |
||
| 30 | 'Author' => 'Member', |
||
| 31 | 'Order' => 'Order', |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * standard SS variable. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $casting = array( |
||
| 40 | 'CustomerNote' => 'HTMLText', |
||
| 41 | 'Type' => 'Varchar', |
||
| 42 | 'InternalUseOnlyNice' => 'Varchar', |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * standard SS variable. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $summary_fields = array( |
||
| 51 | 'Created' => 'Date', |
||
| 52 | 'Order.Title' => 'Order', |
||
| 53 | 'Type' => 'Type', |
||
| 54 | 'Title' => 'Title', |
||
| 55 | 'InternalUseOnlyNice' => 'Internal use only', |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * standard SS variable. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private static $defaults = array( |
||
| 64 | 'InternalUseOnly' => true, |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * casted method. |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | public function InternalUseOnlyNice() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Standard SS method. |
||
| 87 | * |
||
| 88 | * @param Member $member |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function canCreate($member = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Standard SS method. |
||
| 114 | * |
||
| 115 | * @param Member $member |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function canView($member = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Standard SS method. |
||
| 151 | * |
||
| 152 | * @param Member $member |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function canEdit($member = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Standard SS method |
||
| 184 | * logs can never be deleted... |
||
| 185 | * |
||
| 186 | * @param Member $member |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function canDelete($member = null) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * standard SS variable. |
||
| 205 | * |
||
| 206 | * @var array |
||
| 207 | */ |
||
| 208 | private static $searchable_fields = array( |
||
| 209 | 'OrderID' => array( |
||
| 210 | 'field' => 'NumericField', |
||
| 211 | 'title' => 'Order Number', |
||
| 212 | ), |
||
| 213 | 'ClassName' => array( |
||
| 214 | 'title' => 'Type', |
||
| 215 | 'filter' => 'ExactMatchFilter', |
||
| 216 | ), |
||
| 217 | 'Title' => 'PartialMatchFilter', |
||
| 218 | 'Note' => 'PartialMatchFilter', |
||
| 219 | ); |
||
| 220 | |||
| 221 | /** |
||
| 222 | * standard SS variable. |
||
| 223 | * |
||
| 224 | * @var string |
||
| 225 | */ |
||
| 226 | private static $singular_name = 'Order Log Entry'; |
||
| 227 | public function i18n_singular_name() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * standard SS variable. |
||
| 234 | * |
||
| 235 | * @var string |
||
| 236 | */ |
||
| 237 | private static $plural_name = 'Order Log Entries'; |
||
| 238 | public function i18n_plural_name() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Standard SS variable. |
||
| 245 | * |
||
| 246 | * @var string |
||
| 247 | */ |
||
| 248 | private static $description = 'A record of anything that happened with an order.'; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * standard SS variable. |
||
| 252 | * |
||
| 253 | * @var string |
||
| 254 | */ |
||
| 255 | private static $default_sort = [ |
||
| 256 | 'ID' => 'DESC' |
||
| 257 | ]; |
||
| 258 | |||
| 259 | private static $indexes = [ |
||
| 260 | 'Title' => true, |
||
| 261 | 'InternalUseOnly' => true |
||
| 262 | ]; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * standard SS method. |
||
| 266 | */ |
||
| 267 | public function populateDefaults() |
||
| 274 | |||
| 275 | /** |
||
| 276 | *@return FieldList |
||
| 277 | **/ |
||
| 278 | public function getCMSFields() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * when being created, can the user choose the type of log? |
||
| 353 | * |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | protected function limitedToOneClassName() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * link to edit the record. |
||
| 367 | * |
||
| 368 | * @param string | Null $action - e.g. edit |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | public function CMSEditLink($action = null) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return string |
||
| 379 | **/ |
||
| 380 | public function Type() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Determine which properties on the DataObject are |
||
| 391 | * searchable, and map them to their default {@link FormField} |
||
| 392 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 393 | * |
||
| 394 | * Some additional logic is included for switching field labels, based on |
||
| 395 | * how generic or specific the field type is. |
||
| 396 | * |
||
| 397 | * Used by {@link SearchContext}. |
||
| 398 | * |
||
| 399 | * @param array $_params |
||
| 400 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 401 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 402 | * |
||
| 403 | * @return FieldList |
||
| 404 | */ |
||
| 405 | public function scaffoldSearchFields($_params = null) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * standard SS method. |
||
| 420 | */ |
||
| 421 | public function onBeforeWrite() |
||
| 442 | |||
| 443 | /** |
||
| 444 | *@return string |
||
| 445 | **/ |
||
| 446 | public function CustomerNote() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * returns the standard EcommerceDBConfig for use within OrderSteps. |
||
| 457 | * |
||
| 458 | * @return EcommerceDBConfig |
||
| 459 | */ |
||
| 460 | protected function EcomConfig() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Debug helper method. |
||
| 467 | * Can be called from /shoppingcart/debug/. |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function debug() |
||
| 475 | } |
||
| 476 |