Complex classes like OrderItem 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 OrderItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 13 | class OrderItem extends OrderAttribute  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 14 | { | 
            ||
| 15 | /**  | 
            ||
| 16 | * what variables are accessible through http://mysite.com/api/ecommerce/v1/OrderItem/.  | 
            ||
| 17 | *  | 
            ||
| 18 | * @var array  | 
            ||
| 19 | */  | 
            ||
| 20 | private static $api_access = array(  | 
            ||
| 21 | 'view' => array(  | 
            ||
| 22 | 'InternalItemID',  | 
            ||
| 23 | 'CalculatedTotal',  | 
            ||
| 24 | 'TableTitle',  | 
            ||
| 25 | 'TableSubTitleNOHTML',  | 
            ||
| 26 | 'Name',  | 
            ||
| 27 | 'TableValue',  | 
            ||
| 28 | 'Quantity',  | 
            ||
| 29 | 'BuyableID',  | 
            ||
| 30 | 'BuyableClassName',  | 
            ||
| 31 | 'Version',  | 
            ||
| 32 | 'UnitPrice',  | 
            ||
| 33 | 'Total',  | 
            ||
| 34 | 'Order',  | 
            ||
| 35 | ),  | 
            ||
| 36 | );  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * stardard SS variable.  | 
            ||
| 40 | *  | 
            ||
| 41 | * @var array  | 
            ||
| 42 | */  | 
            ||
| 43 | private static $db = array(  | 
            ||
| 44 | 'Quantity' => 'Double',  | 
            ||
| 45 | 'BuyableID' => 'Int',  | 
            ||
| 46 | 'BuyableClassName' => 'Varchar(60)',  | 
            ||
| 47 | 'Version' => 'Int',  | 
            ||
| 48 | );  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * @var array  | 
            ||
| 52 | * stardard SS definition  | 
            ||
| 53 | */  | 
            ||
| 54 | private static $indexes = array(  | 
            ||
| 55 | 'Quantity' => true,  | 
            ||
| 56 | 'BuyableID' => true,  | 
            ||
| 57 | 'BuyableClassName' => true,  | 
            ||
| 58 | );  | 
            ||
| 59 | |||
| 60 | /**  | 
            ||
| 61 | * @var array  | 
            ||
| 62 | * stardard SS definition  | 
            ||
| 63 | */  | 
            ||
| 64 | private static $casting = array(  | 
            ||
| 65 | 'UnitPrice' => 'Currency',  | 
            ||
| 66 | 'UnitPriceAsMoney' => 'Money',  | 
            ||
| 67 | 'Total' => 'Currency',  | 
            ||
| 68 | 'TotalAsMoney' => 'Money',  | 
            ||
| 69 | 'InternalItemID' => 'Varchar',  | 
            ||
| 70 | 'Link' => 'Varchar',  | 
            ||
| 71 | 'AbsoluteLink' => 'Varchar',  | 
            ||
| 72 | 'BuyableLink' => 'Varchar',  | 
            ||
| 73 | 'BuyableExists' => 'Boolean',  | 
            ||
| 74 | 'BuyableFullName' => 'Varchar',  | 
            ||
| 75 | 'BuyableMoreDetails' => 'Varchar'  | 
            ||
| 76 | );  | 
            ||
| 77 | |||
| 78 | ######################  | 
            ||
| 79 | ## CMS CONFIG ##  | 
            ||
| 80 | ######################  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @var array  | 
            ||
| 84 | * stardard SS definition  | 
            ||
| 85 | */  | 
            ||
| 86 | private static $searchable_fields = array(  | 
            ||
| 87 | 'OrderID' => array(  | 
            ||
| 88 | 'field' => 'NumericField',  | 
            ||
| 89 | 'title' => 'Order Number',  | 
            ||
| 90 | ),  | 
            ||
| 91 | //"TableTitle" => "PartialMatchFilter",  | 
            ||
| 92 | //"UnitPrice",  | 
            ||
| 93 | 'Quantity',  | 
            ||
| 94 | //"Total"  | 
            ||
| 95 | );  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * @var array  | 
            ||
| 99 | * stardard SS definition  | 
            ||
| 100 | */  | 
            ||
| 101 | private static $field_labels = array(  | 
            ||
| 102 | //@todo - complete  | 
            ||
| 103 | );  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * @var array  | 
            ||
| 107 | * stardard SS definition  | 
            ||
| 108 | */  | 
            ||
| 109 | private static $summary_fields = array(  | 
            ||
| 110 | 'OrderID' => 'Order ID',  | 
            ||
| 111 | 'BuyableFullName' => 'Item',  | 
            ||
| 112 | 'BuyableMoreDetails' => 'Details ... ',  | 
            ||
| 113 | 'UnitPrice' => 'Unit Price',  | 
            ||
| 114 | 'Quantity' => 'Quantity',  | 
            ||
| 115 | 'Total' => 'Total Price',  | 
            ||
| 116 | );  | 
            ||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * singular name of the object. it is recommended to override this  | 
            ||
| 120 | * in any extensions of this class.  | 
            ||
| 121 | *  | 
            ||
| 122 | * @var string  | 
            ||
| 123 | */  | 
            ||
| 124 | private static $singular_name = 'Order Item';  | 
            ||
| 125 | public function i18n_singular_name()  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * plural name of the object. it is recommended to override this  | 
            ||
| 132 | * in any extensions of this class.  | 
            ||
| 133 | *  | 
            ||
| 134 | * @var string  | 
            ||
| 135 | */  | 
            ||
| 136 | private static $plural_name = 'Order Items';  | 
            ||
| 137 | public function i18n_plural_name()  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Standard SS variable.  | 
            ||
| 144 | *  | 
            ||
| 145 | * @var string  | 
            ||
| 146 | */  | 
            ||
| 147 | private static $description = 'Any item that is added to an order and sits before the sub-total. ';  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * HACK: Versioned is BROKEN this method helps in fixing it.  | 
            ||
| 151 | * Basically, in Versioned, you get a hard-coded error  | 
            ||
| 152 | * when you retrieve an older version of a DataObject.  | 
            ||
| 153 | * This method returns null if it does not exist.  | 
            ||
| 154 | *  | 
            ||
| 155 | * Idea is from Jeremy: https://github.com/burnbright/silverstripe-shop/blob/master/code/products/FixVersioned.php  | 
            ||
| 156 | *  | 
            ||
| 157 | * @param string $class  | 
            ||
| 158 | * @param int $id  | 
            ||
| 159 | * @param int $version  | 
            ||
| 160 | *  | 
            ||
| 161 | * @return DataObject | Null  | 
            ||
| 162 | */  | 
            ||
| 163 | public static function get_version($class, $id, $version)  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Standard SS method.  | 
            ||
| 175 | *  | 
            ||
| 176 | * @var string  | 
            ||
| 177 | */  | 
            ||
| 178 | public function getCMSFields()  | 
            ||
| 262 | |||
| 263 | /**  | 
            ||
| 264 | * standard SS method.  | 
            ||
| 265 | *  | 
            ||
| 266 | * @param Member $member  | 
            ||
| 267 | *  | 
            ||
| 268 | * @return bool  | 
            ||
| 269 | **/  | 
            ||
| 270 | public function canDelete($member = null)  | 
            ||
| 274 | |||
| 275 | /**  | 
            ||
| 276 | * Determine which properties on the DataObject are  | 
            ||
| 277 |      * searchable, and map them to their default {@link FormField} | 
            ||
| 278 |      * representations. Used for scaffolding a searchform for {@link ModelAdmin}. | 
            ||
| 279 | *  | 
            ||
| 280 | * Some additional logic is included for switching field labels, based on  | 
            ||
| 281 | * how generic or specific the field type is.  | 
            ||
| 282 | *  | 
            ||
| 283 |      * Used by {@link SearchContext}. | 
            ||
| 284 | *  | 
            ||
| 285 | * @param array $_params  | 
            ||
| 286 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values  | 
            ||
| 287 | * 'restrictFields': Numeric array of a field name whitelist  | 
            ||
| 288 | *  | 
            ||
| 289 | * @return FieldList  | 
            ||
| 290 | */  | 
            ||
| 291 | public function scaffoldSearchFields($_params = null)  | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * standard SS method.  | 
            ||
| 301 | *  | 
            ||
| 302 | * @param BuyableModel $buyable  | 
            ||
| 303 | * @param float $quantity  | 
            ||
| 304 | *  | 
            ||
| 305 | * @return FieldList  | 
            ||
| 306 | **/  | 
            ||
| 307 | public function addBuyableToOrderItem(BuyableModel $buyable, $quantity = 1)  | 
            ||
| 314 | |||
| 315 | /**  | 
            ||
| 316 | * used to return data for ajax.  | 
            ||
| 317 | *  | 
            ||
| 318 | * @param array  | 
            ||
| 319 | *  | 
            ||
| 320 | * @return array used to create JSON for AJAX  | 
            ||
| 321 | **/  | 
            ||
| 322 | public function updateForAjax(array $js)  | 
            ||
| 387 | |||
| 388 | /**  | 
            ||
| 389 | * saves details about the Order Item before the order is submittted.  | 
            ||
| 390 | *  | 
            ||
| 391 | * @param bool $recalculate - run it, even if it has run already  | 
            ||
| 392 | **/  | 
            ||
| 393 | public function runUpdate($recalculate = false)  | 
            ||
| 417 | |||
| 418 | |||
| 419 | /**  | 
            ||
| 420 | * Standard SS method.  | 
            ||
| 421 | * If the quantity is zero then we set it to 1.  | 
            ||
| 422 | * TODO: evaluate this rule.  | 
            ||
| 423 | */  | 
            ||
| 424 | public function onBeforeWrite()  | 
            ||
| 446 | |||
| 447 | /**  | 
            ||
| 448 | * Standard SS method  | 
            ||
| 449 | * the method below is very important...  | 
            ||
| 450 | * We initialise the order once it has an OrderItem.  | 
            ||
| 451 | */  | 
            ||
| 452 | public function onAfterWrite()  | 
            ||
| 463 | |||
| 464 | /**  | 
            ||
| 465 | * Check if two Order Items are the same.  | 
            ||
| 466 | * Useful when adding two items to cart.  | 
            ||
| 467 | *  | 
            ||
| 468 | * @param OrderItem $orderItem  | 
            ||
| 469 | *  | 
            ||
| 470 | * @return bool  | 
            ||
| 471 | **/  | 
            ||
| 472 | public function hasSameContent(OrderItem $orderItem)  | 
            ||
| 480 | |||
| 481 | ######################  | 
            ||
| 482 | ## TEMPLATE METHODS ##  | 
            ||
| 483 | ######################  | 
            ||
| 484 | |||
| 485 | protected static $calculated_buyable_price = array();  | 
            ||
| 486 | public static function reset_calculated_buyable_price()  | 
            ||
| 490 | |||
| 491 | public function UnitPrice($recalculate = false)  | 
            ||
| 518 | |||
| 519 | public function UnitPriceAsMoney($recalculate = false)  | 
            ||
| 527 | |||
| 528 | /**  | 
            ||
| 529 | * @param bool $recalculate - forces recalculation of price  | 
            ||
| 530 | *  | 
            ||
| 531 | * @return float  | 
            ||
| 532 | */  | 
            ||
| 533 | public function Total($recalculate = false)  | 
            ||
| 552 | |||
| 553 | /**  | 
            ||
| 554 | * @param bool $recalculate - forces recalculation of price  | 
            ||
| 555 | *  | 
            ||
| 556 | * @return Money  | 
            ||
| 557 | */  | 
            ||
| 558 | public function TotalAsMoney($recalculate = false)  | 
            ||
| 566 | |||
| 567 | /**  | 
            ||
| 568 | * Casted variable  | 
            ||
| 569 | * returns InternalItemID from Buyable.  | 
            ||
| 570 | */  | 
            ||
| 571 | public function InternalItemID()  | 
            ||
| 581 | |||
| 582 | /**  | 
            ||
| 583 | * @return Field (EcomQuantityField)  | 
            ||
| 584 | **/  | 
            ||
| 585 | public function QuantityField()  | 
            ||
| 589 | |||
| 590 | /**  | 
            ||
| 591 | * @return Currency (DB Object)  | 
            ||
| 592 | **/  | 
            ||
| 593 | public function TotalAsCurrencyObject()  | 
            ||
| 597 | |||
| 598 | ##########################  | 
            ||
| 599 | ## OTHER LOOKUP METHODS ##  | 
            ||
| 600 | ##########################  | 
            ||
| 601 | |||
| 602 | /**  | 
            ||
| 603 | * Helps in speeding up code.  | 
            ||
| 604 | * This can be a static variable as it is the same for all OrderItems for an Order.  | 
            ||
| 605 | *  | 
            ||
| 606 | * @var array  | 
            ||
| 607 | */  | 
            ||
| 608 | private static $_price_has_been_fixed = array();  | 
            ||
| 609 | |||
| 610 | /**  | 
            ||
| 611 | * @param int $orderID  | 
            ||
| 612 | */  | 
            ||
| 613 | public static function reset_price_has_been_fixed($orderID = 0)  | 
            ||
| 618 | |||
| 619 | /**  | 
            ||
| 620 | * @description - tells you if an order item price has been "fixed"  | 
            ||
| 621 | * meaning that is has been saved in the CalculatedTotal field so that  | 
            ||
| 622 | * it can not be altered.  | 
            ||
| 623 | *  | 
            ||
| 624 | * Default returns false; this is good for uncompleted orders  | 
            ||
| 625 | * but not so good for completed ones.  | 
            ||
| 626 | *  | 
            ||
| 627 | * @return bool  | 
            ||
| 628 | **/  | 
            ||
| 629 | protected function priceHasBeenFixed($recalculate = false)  | 
            ||
| 645 | |||
| 646 | /**  | 
            ||
| 647 | * Store for buyables.  | 
            ||
| 648 | * We store this here to speed up things a little  | 
            ||
| 649 | * Format is like this  | 
            ||
| 650 | * Array(  | 
            ||
| 651 | * 0 => Buyable (versioned)  | 
            ||
| 652 | * 1 => Buyable (current)  | 
            ||
| 653 | * );.  | 
            ||
| 654 | *  | 
            ||
| 655 | * @var array  | 
            ||
| 656 | */  | 
            ||
| 657 | protected $tempBuyableStore = array();  | 
            ||
| 658 | |||
| 659 | /**  | 
            ||
| 660 | * @param bool $current - is this a current one, or an older VERSION ?  | 
            ||
| 661 | *  | 
            ||
| 662 | * @return DataObject (Any type of Data Object that is buyable)  | 
            ||
| 663 | **/  | 
            ||
| 664 | public function Buyable($current = false)  | 
            ||
| 668 | |||
| 669 | /**  | 
            ||
| 670 | * @param bool $current - is this a current one, or an older VERSION ?  | 
            ||
| 671 | *  | 
            ||
| 672 | * @return DataObject (Any type of Data Object that is buyable)  | 
            ||
| 673 | **/  | 
            ||
| 674 | public function getBuyable($current = false)  | 
            ||
| 732 | |||
| 733 | /**  | 
            ||
| 734 | * @alias for getBuyableTitle  | 
            ||
| 735 | * @return string  | 
            ||
| 736 | **/  | 
            ||
| 737 | public function BuyableTitle()  | 
            ||
| 741 | |||
| 742 | /**  | 
            ||
| 743 | * @return string  | 
            ||
| 744 | **/  | 
            ||
| 745 | public function getBuyableTitle()  | 
            ||
| 756 | |||
| 757 | /**  | 
            ||
| 758 | * @alias for getBuyableLink  | 
            ||
| 759 | * @return string  | 
            ||
| 760 | */  | 
            ||
| 761 | public function BuyableLink()  | 
            ||
| 765 | |||
| 766 | /**  | 
            ||
| 767 | *  | 
            ||
| 768 | * @return string  | 
            ||
| 769 | */  | 
            ||
| 770 | public function getBuyableLink()  | 
            ||
| 783 | |||
| 784 | /**  | 
            ||
| 785 | * @alias for getBuyableExists  | 
            ||
| 786 | * @return bool  | 
            ||
| 787 | */  | 
            ||
| 788 | public function BuyableExists()  | 
            ||
| 792 | |||
| 793 | /**  | 
            ||
| 794 | *  | 
            ||
| 795 | * @return bool  | 
            ||
| 796 | */  | 
            ||
| 797 | public function getBuyableExists()  | 
            ||
| 807 | |||
| 808 | |||
| 809 | /**  | 
            ||
| 810 | * @alias for getBuyableFullName  | 
            ||
| 811 | * @return String  | 
            ||
| 812 | */  | 
            ||
| 813 | public function BuyableFullName()  | 
            ||
| 817 | |||
| 818 | /**  | 
            ||
| 819 | * @return String  | 
            ||
| 820 | */  | 
            ||
| 821 | public function getBuyableFullName()  | 
            ||
| 830 | |||
| 831 | /**  | 
            ||
| 832 | * @alias for getBuyableMoreDetails  | 
            ||
| 833 | * @return String  | 
            ||
| 834 | */  | 
            ||
| 835 | public function BuyableMoreDetails()  | 
            ||
| 839 | |||
| 840 | /**  | 
            ||
| 841 | * @return String  | 
            ||
| 842 | */  | 
            ||
| 843 | public function getBuyableMoreDetails()  | 
            ||
| 857 | |||
| 858 | ##########################  | 
            ||
| 859 | ## LINKS ##  | 
            ||
| 860 | ##########################  | 
            ||
| 861 | |||
| 862 | /**  | 
            ||
| 863 | * @return string (URLSegment)  | 
            ||
| 864 | **/  | 
            ||
| 865 | public function Link()  | 
            ||
| 869 | |||
| 870 | /**  | 
            ||
| 871 | * @return string (URLSegment)  | 
            ||
| 872 | **/  | 
            ||
| 873 | public function getLink()  | 
            ||
| 882 | |||
| 883 | /**  | 
            ||
| 884 | * alias  | 
            ||
| 885 | * @return string  | 
            ||
| 886 | */  | 
            ||
| 887 | public function AbsoluteLink()  | 
            ||
| 891 | |||
| 892 | /**  | 
            ||
| 893 | * @return string  | 
            ||
| 894 | */  | 
            ||
| 895 | public function getAbsoluteLink()  | 
            ||
| 899 | |||
| 900 | /**  | 
            ||
| 901 | * @return string (URLSegment)  | 
            ||
| 902 | **/  | 
            ||
| 903 | public function CheckoutLink()  | 
            ||
| 907 | |||
| 908 | ## Often Overloaded functions ##  | 
            ||
| 909 | |||
| 910 | /**  | 
            ||
| 911 | * @return string (URLSegment)  | 
            ||
| 912 | **/  | 
            ||
| 913 | public function AddLink()  | 
            ||
| 917 | |||
| 918 | /**  | 
            ||
| 919 | * @return string (URLSegment)  | 
            ||
| 920 | **/  | 
            ||
| 921 | public function IncrementLink()  | 
            ||
| 925 | |||
| 926 | /**  | 
            ||
| 927 | * @return string (URLSegment)  | 
            ||
| 928 | **/  | 
            ||
| 929 | public function DecrementLink()  | 
            ||
| 933 | |||
| 934 | /**  | 
            ||
| 935 | * @return string (URLSegment)  | 
            ||
| 936 | **/  | 
            ||
| 937 | public function RemoveLink()  | 
            ||
| 941 | |||
| 942 | /**  | 
            ||
| 943 | * @return string (URLSegment)  | 
            ||
| 944 | **/  | 
            ||
| 945 | public function RemoveAllLink()  | 
            ||
| 949 | |||
| 950 | /**  | 
            ||
| 951 | * @return string (URLSegment)  | 
            ||
| 952 | **/  | 
            ||
| 953 | public function RemoveAllAndEditLink()  | 
            ||
| 957 | |||
| 958 | /**  | 
            ||
| 959 | * @return string (URLSegment)  | 
            ||
| 960 | **/  | 
            ||
| 961 | public function SetSpecificQuantityItemLink($quantity)  | 
            ||
| 965 | |||
| 966 | /**  | 
            ||
| 967 | * @Todo: do we still need this?  | 
            ||
| 968 | *  | 
            ||
| 969 | * @return array for use as get variables in link  | 
            ||
| 970 | **/  | 
            ||
| 971 | protected function linkParameters()  | 
            ||
| 983 | |||
| 984 | public function debug()  | 
            ||
| 994 | }  | 
            ||
| 995 | 
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.