Conditions | 6 |
Paths | 18 |
Total Lines | 67 |
Code Lines | 44 |
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 |
||
142 | public function doAddItemToCart($data) |
||
143 | { |
||
144 | $classname = $data["ClassName"]; |
||
145 | $id = $data["ID"]; |
||
146 | $cart = ShoppingCartFactory::create(); |
||
147 | $error = false; |
||
148 | $redirect_to_cart = Config::inst() |
||
149 | ->get(ShoppingCart::class, "redirect_on_add"); |
||
150 | |||
151 | if ($object = $classname::get()->byID($id)) { |
||
152 | // Attempt to get tax rate from object |
||
153 | // On a Product this is handleed via casting |
||
154 | // But could be a direct association as well. |
||
155 | $tax_id = $object->TaxID; |
||
156 | |||
157 | $deliverable = (isset($object->Deliverable)) ? $object->Deliverable : true; |
||
158 | |||
159 | $item_to_add = LineItem::create([ |
||
160 | "Title" => $object->Title, |
||
161 | "Content" => $object->Content, |
||
162 | "Price" => $object->Price, |
||
163 | "Quantity" => $data['Quantity'], |
||
164 | "StockID" => $object->StockID, |
||
165 | "Weight" => $object->Weight, |
||
166 | "ProductClass" => $object->ClassName, |
||
167 | "Stocked" => $object->Stocked, |
||
168 | "Deliverable" => $deliverable, |
||
169 | "TaxID" => $tax_id, |
||
170 | ]); |
||
171 | |||
172 | // Try and add item to cart, return any exceptions raised |
||
173 | // as a message |
||
174 | try { |
||
175 | $cart->addItem($item_to_add); |
||
176 | |||
177 | $message = _t( |
||
178 | 'ShoppingCart.AddedItemToCart', |
||
179 | 'Added "{item}" to your shopping cart', |
||
180 | ["item" => $object->Title] |
||
181 | ); |
||
182 | |||
183 | $this->sessionMessage( |
||
184 | $message, |
||
185 | ValidationResult::TYPE_GOOD |
||
186 | ); |
||
187 | } catch(Exception $e) { |
||
188 | $error = true; |
||
189 | $this->sessionMessage( |
||
190 | $e->getMessage() |
||
191 | ); |
||
192 | } |
||
193 | } else { |
||
194 | $error = true; |
||
195 | $this->sessionMessage( |
||
196 | _t("ShoppingCart.ErrorAddingToCart", "Error adding item to cart") |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | if ($redirect_to_cart && !$error) { |
||
201 | return $this |
||
202 | ->getController() |
||
203 | ->redirect($cart->Link()); |
||
204 | } |
||
205 | |||
206 | return $this |
||
207 | ->getController() |
||
208 | ->redirectBack(); |
||
209 | } |
||
210 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths