Conditions | 17 |
Paths | 288 |
Total Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
6 | public function __construct($controller, $name, $repeatOrderID = 0, $originatingOrder = 0) |
||
7 | { |
||
8 | $order = null; |
||
9 | //create vs edit |
||
10 | if ($repeatOrderID) { |
||
11 | $repeatOrder = DataObject::get_one('RepeatOrder', "ID = ".$repeatOrderID); |
||
12 | $items = $repeatOrder->OrderItems(); |
||
13 | } else { |
||
14 | $repeatOrder = null; |
||
15 | if ($originatingOrder) { |
||
16 | $order = Order::get_by_id_if_can_view($originatingOrder); |
||
17 | } |
||
18 | if (!$order) { |
||
19 | $order = ShoppingCart::current_order(); |
||
20 | } |
||
21 | if ($order) { |
||
22 | $items = $order->Items(); |
||
23 | } else { |
||
24 | $items = null; |
||
25 | } |
||
26 | } |
||
27 | |||
28 | //build fields |
||
29 | $fields = FieldList::create(); |
||
30 | |||
31 | //products! |
||
32 | if ($items) { |
||
33 | $fields->push(HeaderField::create('ProductsHeader', 'Products')); |
||
34 | $products = Product::get()->filter('Product', "\"AllowPurchase\" = 1"); |
||
35 | $productsMap = $products->map('ID', 'Title'); |
||
36 | $this->array_unshift_assoc($productsMap, 0, "--- Please select ---"); |
||
37 | foreach ($productsMap as $id => $title) { |
||
38 | if ($product = DataObject::get_one('Product', ["ID" => $id])) { |
||
39 | if (!$product->canPurchase()) { |
||
40 | unset($productsMap[$id]); |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | $j = 0; |
||
45 | foreach ($items as $key => $item) { |
||
46 | $j++; |
||
47 | $alternativeItemsMap = $productsMap; |
||
48 | $defaultProductID = $item->ProductID ? $item->ProductID : $item->BuyableID; |
||
49 | $itemID = $defaultProductID; |
||
50 | unset($alternativeItemsMap[$defaultProductID]); |
||
51 | $fields->push(DropdownField::create('Product[ID]['.$itemID.']', "Preferred Product #$j", $productsMap, $defaultProductID)); |
||
52 | $fields->push(NumericField::create('Product[Quantity]['.$itemID.']', " ... quantity", $item->Quantity)); |
||
53 | for ($i = 1; $i < 6; $i++) { |
||
54 | $alternativeField = "Alternative".$i."ID"; |
||
55 | $fields->push(DropdownField::create('Product['.$alternativeField.']['.$itemID.']', " ... alternative $i", $alternativeItemsMap, (isset($item->$alternativeField) ? $item->$alternativeField : 0))); |
||
56 | } |
||
57 | } |
||
58 | } else { |
||
59 | $fields->push(HeaderField::create('items', 'There are no products in this repeating order')); |
||
60 | } |
||
61 | |||
62 | //other details |
||
63 | $fields->push(HeaderField::create('DetailsHeader', 'Repeat Order Details')); |
||
64 | $fields->push( |
||
65 | ListboxField::create( |
||
66 | 'PaymentMethod', |
||
67 | 'Payment Method', |
||
68 | Config::inst()->get('RepeatOrder', 'payment_methods'), |
||
69 | null, |
||
70 | Config::inst()->get('RepeatOrder', 'payment_methods') |
||
71 | ) |
||
72 | ); |
||
73 | $startField = DateField::create('Start', 'Start Date'); |
||
74 | $startField->setConfig('showcalendar', true); |
||
75 | $fields->push($startField); |
||
76 | $endField = DateField::create('End', 'End Date'); |
||
77 | $endField->setConfig('showcalendar', true); |
||
78 | $fields->push($endField); |
||
79 | $fields->push( |
||
80 | ListboxField::create( |
||
81 | 'Period', |
||
82 | 'Period', |
||
83 | Config::inst()->get('RepeatOrder', 'period_fields'), |
||
84 | null, |
||
85 | count(Config::inst()->get('RepeatOrder', 'period_fields')) |
||
86 | ) |
||
87 | ); |
||
88 | $fields->push(TextareaField::create('Notes', 'Notes')); |
||
89 | |||
90 | //hidden field |
||
91 | if (isset($order->ID)) { |
||
92 | $fields->push(HiddenField::create('OrderID', 'OrderID', $order->ID)); |
||
93 | } |
||
94 | if ($repeatOrder) { |
||
95 | $fields->push(HiddenField::create('RepeatOrderID', 'RepeatOrderID', $repeatOrder->ID)); |
||
96 | } |
||
97 | |||
98 | //actions |
||
99 | $actions = FieldList::create(); |
||
100 | if ($repeatOrder) { |
||
101 | $actions->push(FormAction::create('doSave', 'Save')); |
||
102 | } else { |
||
103 | $actions->push(FormAction::create('doCreate', 'Create')); |
||
104 | } |
||
105 | |||
106 | //required fields |
||
107 | $requiredArray = array('Start', 'End', 'Period'); |
||
108 | $requiredFields = RequiredFields::create($requiredArray); |
||
109 | |||
110 | //make form |
||
111 | parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
||
112 | |||
113 | //load data |
||
114 | if ($repeatOrder) { |
||
115 | $this->loadDataFrom( |
||
116 | [ |
||
117 | 'Start' => $repeatOrder->Start, |
||
118 | 'End' => $repeatOrder->End, |
||
119 | 'Period' => $repeatOrder->Period, |
||
120 | 'Notes' => $repeatOrder->Notes, |
||
121 | 'PaymentMethod' => $repeatOrder->PaymentMethod, |
||
122 | ] |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 | |||
235 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.