Complex classes like BuyableStockCalculatedQuantity 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 BuyableStockCalculatedQuantity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class BuyableStockCalculatedQuantity extends DataObject |
||
|
|||
13 | { |
||
14 | private static $db = array( |
||
15 | "BaseQuantity" => "Int", |
||
16 | "BuyableID" => "Int", |
||
17 | "BuyableClassName" => "Varchar" |
||
18 | ); |
||
19 | |||
20 | private static $has_many = array( |
||
21 | "BuyableStockOrderEntry" => "BuyableStockOrderEntry", |
||
22 | "BuyableStockManualUpdate" => "BuyableStockManualUpdate" |
||
23 | ); |
||
24 | |||
25 | private static $defaults = array( |
||
26 | "BaseQuantity" => 0 |
||
27 | ); |
||
28 | |||
29 | private static $casting = array( |
||
30 | "Name" => "Varchar", |
||
31 | "Buyable" => "DataObject", |
||
32 | "UnlimitedStock" => "Boolean" |
||
33 | ); |
||
34 | |||
35 | //MODEL ADMIN STUFF |
||
36 | private static $searchable_fields = array( |
||
37 | "BaseQuantity" |
||
38 | ); |
||
39 | |||
40 | private static $field_labels = array( |
||
41 | "BaseQuantity" => "Calculated Quantity On Hand", |
||
42 | "BuyableID" => "Buyable ID", |
||
43 | "LastEdited" => "Last Calculated" |
||
44 | ); |
||
45 | |||
46 | private static $summary_fields = array( |
||
47 | "Name", |
||
48 | "BaseQuantity", |
||
49 | "LastEdited" |
||
50 | ); |
||
51 | |||
52 | private static $indexes = array( |
||
53 | "BuyableClassName" => true, |
||
54 | "BuyableID" => true, |
||
55 | 'BaseQuantity' => true |
||
56 | ); |
||
57 | |||
58 | private static $default_sort = [ |
||
59 | 'BuyableClassName' => 'ASC', |
||
60 | 'BaseQuantity' => 'DESC', |
||
61 | 'ID' => 'ASC' |
||
62 | ]; |
||
63 | |||
64 | private static $singular_name = "Stock Calculated Quantity"; |
||
65 | |||
66 | private static $plural_name = "Stock Calculated Quantities"; |
||
67 | |||
68 | private static $calculation_done = array(); |
||
69 | |||
70 | public function canCreate($member = null) |
||
74 | |||
75 | public function canEdit($member = null) |
||
79 | |||
80 | public function canDelete($member = null) |
||
84 | |||
85 | public function canView($member = null) |
||
89 | |||
90 | public function Link($action = "update") |
||
94 | |||
95 | public function HistoryLink() |
||
99 | |||
100 | public function Buyable() |
||
111 | |||
112 | public function UnlimitedStock() |
||
122 | |||
123 | public function Name() |
||
134 | |||
135 | protected function canDoAnything($member = null) |
||
144 | |||
145 | public static function get_quantity_by_buyable($buyable) |
||
157 | |||
158 | public static function get_by_buyable($buyable) |
||
187 | |||
188 | public function calculatedBaseQuantity() |
||
202 | |||
203 | protected function calculatedBaseQuantities($buyables = null) |
||
214 | |||
215 | /** |
||
216 | * TODO: change to submitted from CustomerCanEdit criteria |
||
217 | */ |
||
218 | |||
219 | |||
220 | protected function workoutActualQuantity() |
||
314 | } |
||
315 |
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.