Conditions | 27 |
Paths | 216 |
Total Lines | 162 |
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 |
||
42 | public static function repeatOrderFormFields($repeatOrderID = 0, $originatingOrder = 0, $isCheckout = false, $showTable = false) |
||
43 | { |
||
44 | $order = null; |
||
45 | $repeatOrder = null; |
||
46 | //create vs edit |
||
47 | if ($repeatOrderID) { |
||
48 | $repeatOrder = DataObject::get_one('RepeatOrder', "ID = ".$repeatOrderID); |
||
49 | $items = $repeatOrder->OrderItems(); |
||
50 | } |
||
51 | |||
52 | if(is_null($repeatOrder) || $isCheckout){ |
||
53 | $repeatOrder = null; |
||
54 | if ($originatingOrder) { |
||
55 | $order = Order::get_by_id_if_can_view($originatingOrder); |
||
56 | } |
||
57 | if (!$order) { |
||
58 | $order = ShoppingCart::current_order(); |
||
59 | } |
||
60 | if ($order) { |
||
61 | $items = $order->Items(); |
||
62 | } else { |
||
63 | $items = null; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | //build fields |
||
68 | $fields = FieldList::create(); |
||
69 | |||
70 | //products! |
||
71 | if ($items) { |
||
72 | // $fields->push(HeaderField::create('ProductsHeader', 'Products')); |
||
73 | if($repeatOrder && $showTable) { |
||
74 | $fields->push( |
||
75 | LiteralField::create( |
||
76 | 'OverviewTable', |
||
77 | $repeatOrder->renderWith('RepeatOrder_Order') |
||
78 | ) |
||
79 | ); |
||
80 | } |
||
81 | else { |
||
82 | $products = Product::get()->filter(["AllowPurchase" => 1]); |
||
83 | $productsMap = $products->map('ID', 'Title')->toArray(); |
||
84 | $arr1 = [0 => "--- Please select ---"] + $productsMap; |
||
85 | foreach ($productsMap as $id => $title) { |
||
86 | if ($product = Product::get()->byID($id)) { |
||
87 | if (!$product->canPurchase()) { |
||
88 | unset($productsMap[$id]); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | $j = 0; |
||
93 | foreach ($items as $key => $item) { |
||
94 | $j++; |
||
95 | $alternativeItemsMap = $productsMap; |
||
96 | $defaultProductID = $item->ProductID ? $item->ProductID : $item->BuyableID; |
||
97 | $itemID = $defaultProductID; |
||
98 | unset($alternativeItemsMap[$defaultProductID]); |
||
99 | $fields->push( |
||
100 | HiddenField::create( |
||
101 | 'Product[ID]['.$itemID.']', |
||
102 | "Preferred Product #$j", |
||
103 | $defaultProductID |
||
104 | ) |
||
105 | ); |
||
106 | $fields->push( |
||
107 | HiddenField::create( |
||
108 | 'Product[Quantity]['.$itemID.']', |
||
109 | " ... quantity", |
||
110 | $item->Quantity |
||
111 | ) |
||
112 | ); |
||
113 | $altCount = Config::inst()->get('RepeatOrderForm', 'number_of_product_alternatives'); |
||
114 | if($altCount > 9) { |
||
115 | user_error('You can only have up to nine alternatives buyables'); |
||
116 | } elseif($altCount > 0) { |
||
117 | for ($i = 1; $i <= $altCount; $i++) { |
||
118 | $alternativeField = "Alternative".$i."ID"; |
||
119 | $fields->push( |
||
120 | DropdownField::create( |
||
121 | 'Product['.$alternativeField.']['.$itemID.']', |
||
122 | " ... alternative $i", |
||
123 | $alternativeItemsMap, |
||
124 | (isset($item->$alternativeField) ? $item->$alternativeField : 0) |
||
125 | ) |
||
126 | ); |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } else { |
||
132 | $fields->push(HeaderField::create('items', 'There are no products in this repeating order')); |
||
133 | } |
||
134 | |||
135 | //other details |
||
136 | $fields->push( |
||
137 | HeaderField::create( |
||
138 | 'DetailsHeader', |
||
139 | 'Repeat Order Details' |
||
140 | ) |
||
141 | ); |
||
142 | $fields->push( |
||
143 | DropdownField::create( |
||
144 | 'PaymentMethod', |
||
145 | 'Payment Method', |
||
146 | Config::inst()->get('RepeatOrder', 'payment_methods'), |
||
147 | $repeatOrder ? $repeatOrder->PaymentMethod : '' |
||
148 | ) |
||
149 | ); |
||
150 | |||
151 | $startField = DateField::create( |
||
152 | 'Start', |
||
153 | 'Start Date', |
||
154 | $repeatOrder ? $repeatOrder->Start : date('d-m-Y') |
||
155 | ); |
||
156 | $startField->setAttribute('autocomplete', 'off'); |
||
157 | $startField->setConfig('showcalendar', true); |
||
158 | $fields->push($startField); |
||
159 | |||
160 | $endField = DateField::create( |
||
161 | 'End', |
||
162 | 'End Date', |
||
163 | $repeatOrder ? $repeatOrder->End : '' |
||
164 | ); |
||
165 | $endField->setAttribute('autocomplete', 'off'); |
||
166 | $endField->setConfig('showcalendar', true); |
||
167 | $fields->push($endField); |
||
168 | |||
169 | $fields->push( |
||
170 | DropdownField::create( |
||
171 | 'Period', |
||
172 | 'Period', |
||
173 | Config::inst()->get('RepeatOrder', 'period_fields'), |
||
174 | $repeatOrder ? $repeatOrder->Period : '' |
||
175 | ) |
||
176 | ); |
||
177 | |||
178 | $fields->push( |
||
179 | TextareaField::create( |
||
180 | 'Notes', |
||
181 | 'Notes', |
||
182 | $repeatOrder ? $repeatOrder->Notes : '' |
||
183 | ) |
||
184 | ); |
||
185 | |||
186 | $repeatOrderFormLink = RepeatOrdersPage::get_repeat_order_link('ajaxcreateorder', $order ? $order->ID : 0); |
||
187 | $fields->push( |
||
188 | HiddenField::create('AjaxSubmissionLink', 'AjaxSubmissionLink', $repeatOrderFormLink) |
||
189 | ); |
||
190 | |||
191 | //hidden field |
||
192 | if (isset($order->ID)) { |
||
193 | $fields->push( |
||
194 | HiddenField::create('OrderID', 'OrderID', $order->ID) |
||
195 | ); |
||
196 | } |
||
197 | if ($repeatOrder) { |
||
198 | $fields->push( |
||
199 | HiddenField::create('RepeatOrderID', 'RepeatOrderID', $repeatOrder->ID) |
||
200 | ); |
||
201 | } |
||
202 | return $fields; |
||
203 | } |
||
204 | |||
351 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.