Conditions | 25 |
Paths | 123 |
Total Lines | 122 |
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 |
||
33 | public function preparePage() |
||
34 | { |
||
35 | $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager); |
||
36 | $this->P->cb_pagetype = 'content'; |
||
37 | |||
38 | if ( |
||
39 | ( |
||
40 | $this->config->getShop('show_pricesonlytologgedin') |
||
41 | && !$this->helperCustomer->getUserData() |
||
42 | ) |
||
43 | || filter_input(INPUT_SERVER, 'HTTP_REFERER') === null |
||
44 | ) { |
||
45 | $this->P->oPayload->cl_html = $this->serviceManager->get('textcats')->T('denied_default'); |
||
46 | } else { |
||
47 | $iAmount = filter_input(INPUT_POST, 'amount', FILTER_SANITIZE_NUMBER_INT); |
||
48 | $postitemno = filter_input(INPUT_POST, 'itemno', FILTER_SANITIZE_SPECIAL_CHARS); |
||
49 | |||
50 | if (empty($postitemno) || !is_numeric($iAmount)) { |
||
51 | $this->replyToCartUpdate('noitemnooramount'); |
||
52 | } else { |
||
53 | $iAmount = floor($iAmount); |
||
54 | |||
55 | // Check if this item exists |
||
56 | $aData = $this->serviceManager->get('oItem')->sortItems('', $postitemno); |
||
57 | if (!isset($aData)) { |
||
58 | $this->replyToCartUpdate('itemnotfound'); |
||
59 | } else { |
||
60 | // are there additional items to this item, if so, check if they are valid, too. |
||
61 | $postadditionalitems = filter_input(INPUT_POST, 'additionalitems', FILTER_SANITIZE_SPECIAL_CHARS); |
||
62 | if (!empty($postadditionalitems)) { |
||
63 | |||
64 | if (strpos($postadditionalitems, '~') !== false) { |
||
65 | $postadditionalitems = explode('~', $postadditionalitems); |
||
66 | } else { |
||
67 | $postadditionalitems = [$postadditionalitems]; |
||
68 | } |
||
69 | |||
70 | $additionaldata = $this->serviceManager->get('oItem')->sortItems('', $postadditionalitems); |
||
71 | |||
72 | if (count($postadditionalitems) != count($additionaldata['item'])) { |
||
73 | $this->replyToCartUpdate('itemnotfound'); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // build the key for this item for the shoppingcart |
||
78 | $sItemno = $aData['item'][$postitemno]['itm_no']; |
||
79 | $sCartKey = $sItemno; |
||
80 | |||
81 | if (!empty($this->config->getShop('custom_order_fields'))) { |
||
82 | foreach ($this->config->getShop('custom_order_fields') as $sValue) { |
||
83 | if (isset($aData['item'][$sItemno]['itm_data'][$sValue])) { |
||
84 | $aOptions = []; |
||
85 | $TMP = explode('|', $aData['item'][$sItemno]['itm_data'][$sValue]); |
||
86 | foreach ($TMP as $sTMPValue) { |
||
87 | if (!empty($sTMPValue)) { |
||
88 | $aOptions[] = $sTMPValue; |
||
89 | } |
||
90 | } |
||
91 | unset($sTMP); |
||
92 | |||
93 | $currentpost = filter_input(INPUT_POST, $sValue); |
||
94 | if ($currentpost !== null && in_array($currentpost, $aOptions)) { |
||
95 | $sCartKey .= '|'.$sValue.':'.$currentpost; |
||
96 | } else { |
||
97 | $this->replyToCartUpdate('requiredfieldmissing'); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | // if this Items is not in cart and amount is 0, no need to do anything, return to referer |
||
103 | if ($iAmount == 0 && !isset($_SESSION['cart'][$sCartKey])) { |
||
104 | $this->replyToCartUpdate('noactiontaken'); |
||
105 | } |
||
106 | $aItem = [ |
||
107 | 'amount' => $iAmount, |
||
108 | 'price' => $this->serviceManager->get('oItem')->calcPrice($aData['item'][$sItemno]), |
||
109 | 'rg' => $aData['item'][$sItemno]['itm_rg'], |
||
110 | 'vat' => $aData['item'][$sItemno]['itm_vatid'], |
||
111 | 'name' => $aData['item'][$sItemno]['itm_name'], |
||
112 | 'img' => $aData['item'][$sItemno]['itm_img'], |
||
113 | ]; |
||
114 | |||
115 | if (filter_input(INPUT_POST, 'action') === 'add') { |
||
116 | $this->addItemToCart($sCartKey, $aItem); |
||
117 | |||
118 | if (!empty($postadditionalitems)) { |
||
119 | foreach ($postadditionalitems as $additionalitem) { |
||
120 | $this->addItemToCart( |
||
121 | $additionalitem, |
||
122 | [ |
||
123 | 'amount' => $iAmount, |
||
124 | 'price' => $this->serviceManager->get('oItem')->calcPrice($additionaldata['item'][$additionalitem]), |
||
|
|||
125 | 'rg' => $additionaldata['item'][$additionalitem]['itm_rg'], |
||
126 | 'vat' => $additionaldata['item'][$additionalitem]['itm_vatid'], |
||
127 | 'name' => $additionaldata['item'][$additionalitem]['itm_name'], |
||
128 | 'img' => $additionaldata['item'][$additionalitem]['itm_img'], |
||
129 | ] |
||
130 | ); |
||
131 | } |
||
132 | } |
||
133 | } else { |
||
134 | if (isset($_SESSION['cart'][$sCartKey])) { // if this item is already in cart, update amount |
||
135 | if ($iAmount == 0) { // new amount == 0 -> remove from cart |
||
136 | unset($_SESSION['cart'][$sCartKey]); |
||
137 | if (count($_SESSION['cart']) == 0) { // once the last cart item is unset, we no longer need cartpricesums |
||
138 | unset($_SESSION['cartpricesums']); |
||
139 | } |
||
140 | $this->replyToCartUpdate('removed', ['cartkey' => $sCartKey]); |
||
141 | } else { // update amount |
||
142 | $_SESSION['cart'][$sCartKey]['amount'] = $iAmount; |
||
143 | $this->replyToCartUpdate('updated', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
||
144 | } |
||
145 | } else { // if this item is not in the cart yet, add it |
||
146 | $_SESSION['cart'][$sCartKey] = $aItem; |
||
147 | } |
||
148 | } |
||
149 | $this->replyToCartUpdate('added', ['cartkey' => $sCartKey, 'amount' => $iAmount]); |
||
150 | } |
||
151 | } |
||
152 | $this->helper->terminateScript(); |
||
153 | } |
||
154 | } |
||
155 | |||
196 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: