| Conditions | 21 |
| Paths | 37 |
| Total Lines | 85 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 34 | public function preparePage() |
||
| 35 | { |
||
| 36 | $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
||
| 37 | $this->P->cb_pagetype = 'content'; |
||
| 38 | |||
| 39 | if ( |
||
| 40 | ( |
||
| 41 | HelperConfig::$shop["show_pricesonlytologgedin"] |
||
| 42 | && !\HaaseIT\HCSF\Customer\Helper::getUserData() |
||
| 43 | ) |
||
| 44 | || !isset($_SERVER["HTTP_REFERER"]) |
||
| 45 | ) { |
||
| 46 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T("denied_default"); |
||
| 47 | } else { |
||
| 48 | $iAmount = ''; |
||
| 49 | if (isset($_REQUEST["amount"])) { |
||
| 50 | $iAmount = $_REQUEST["amount"]; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (!isset($_REQUEST["itemno"]) || $_REQUEST["itemno"] == '' || !is_numeric($iAmount)) { |
||
| 54 | $this->replyToCartUpdate('noitemnooramount'); |
||
| 55 | } else { |
||
| 56 | $iAmount = floor($iAmount); |
||
| 57 | |||
| 58 | // Check if this item exists |
||
| 59 | $aData = $this->serviceManager->get('oItem')->sortItems('', $_REQUEST["itemno"]); |
||
| 60 | if (!isset($aData)) { |
||
| 61 | $this->replyToCartUpdate('itemnotfound'); |
||
| 62 | } else { |
||
| 63 | // build the key for this item for the shoppingcart |
||
| 64 | $sItemno = $aData["item"][$_REQUEST["itemno"]]['itm_no']; |
||
| 65 | $sCartKey = $sItemno; |
||
| 66 | |||
| 67 | if (isset(HelperConfig::$shop["custom_order_fields"])) { |
||
| 68 | foreach (HelperConfig::$shop["custom_order_fields"] as $sValue) { |
||
| 69 | if (isset($aData["item"][$sItemno]["itm_data"][$sValue])) { |
||
| 70 | $aOptions = []; |
||
| 71 | $TMP = explode('|', $aData["item"][$sItemno]["itm_data"][$sValue]); |
||
| 72 | foreach ($TMP as $sTMPValue) { |
||
| 73 | if (trim($sTMPValue) != '') { |
||
| 74 | $aOptions[] = $sTMPValue; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | unset($sTMP); |
||
| 78 | |||
| 79 | if (isset($_REQUEST[$sValue]) && in_array($_REQUEST[$sValue], $aOptions)) { |
||
| 80 | $sCartKey .= '|' . $sValue . ':' . $_REQUEST[$sValue]; |
||
| 81 | } else { |
||
| 82 | $this->replyToCartUpdate('requiredfieldmissing'); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | // if this Items is not in cart and amount is 0, no need to do anything, return to referer |
||
| 88 | if (!isset($_SESSION["cart"][$sCartKey]) && $iAmount == 0) { |
||
| 89 | $this->replyToCartUpdate('noactiontaken'); |
||
| 90 | } |
||
| 91 | $aItem = [ |
||
| 92 | 'amount' => $iAmount, |
||
| 93 | 'price' => $this->serviceManager->get('oItem')->calcPrice($aData["item"][$sItemno]), |
||
| 94 | 'rg' => $aData["item"][$sItemno]['itm_rg'], |
||
| 95 | 'vat' => $aData["item"][$sItemno]['itm_vatid'], |
||
| 96 | 'name' => $aData["item"][$sItemno]['itm_name'], |
||
| 97 | 'img' => $aData["item"][$sItemno]['itm_img'], |
||
| 98 | ]; |
||
| 99 | if (isset($_SESSION["cart"][$sCartKey])) { // if this item is already in cart, update amount |
||
| 100 | if ($iAmount == 0) { // new amount == 0 -> remove from cart |
||
| 101 | unset($_SESSION["cart"][$sCartKey]); |
||
| 102 | if (count($_SESSION["cart"]) == 0) { // once the last cart item is unset, we no longer need cartpricesums |
||
| 103 | unset($_SESSION["cartpricesums"]); |
||
| 104 | } |
||
| 105 | $this->replyToCartUpdate('removed', ['cartkey' => $sCartKey]); |
||
| 106 | } else { // update amount |
||
| 107 | $_SESSION["cart"][$sCartKey]["amount"] = $iAmount; |
||
| 108 | $this->replyToCartUpdate('updated', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
||
| 109 | } |
||
| 110 | } else { // if this item is not in the cart yet, add it |
||
| 111 | $_SESSION["cart"][$sCartKey] = $aItem; |
||
| 112 | } |
||
| 113 | $this->replyToCartUpdate('added', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | die(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 149 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: