Conditions | 23 |
Paths | 12288 |
Total Lines | 153 |
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 |
||
56 | public function ReturnCartData(array $messages = array(), array $additionalData = null, $status = 'success') |
||
57 | { |
||
58 | //add header |
||
59 | if ($this->includeHeaders) { |
||
60 | $this->addHeader('Content-Type', 'application/json'); |
||
61 | } |
||
62 | |||
63 | SSViewer::set_source_file_comments(false); |
||
64 | //merge messages |
||
65 | $messagesImploded = ''; |
||
66 | if (is_array($messages) && count($messages)) { |
||
67 | foreach ($messages as $messageArray) { |
||
68 | $messagesImploded .= '<span class="'.$messageArray['Type'].'">'.$messageArray['Message'].'</span>'; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | //bad status |
||
73 | if ($status != 'success') { |
||
74 | $this->setStatusCode(400, $messagesImploded); |
||
75 | } |
||
76 | |||
77 | //init Order - IMPORTANT |
||
78 | $currentOrder = ShoppingCart::current_order(); |
||
79 | |||
80 | //THIS LINE TAKES UP MOST OF THE TIME OF THE RESPONSE!!! |
||
81 | $currentOrder->calculateOrderAttributes($force = false); |
||
82 | |||
83 | $ajaxObject = $currentOrder->AJAXDefinitions(); |
||
84 | // populate Javascript |
||
85 | $js = array(); |
||
86 | |||
87 | //must be first |
||
88 | if (isset($_REQUEST['loadingindex'])) { |
||
89 | $js[] = array( |
||
90 | 't' => 'loadingindex', |
||
91 | 'v' => $_REQUEST['loadingindex'], |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | //order items |
||
96 | |||
97 | $inCartArray = array(); |
||
98 | $items = $currentOrder->Items(); |
||
99 | if ($items->count()) { |
||
100 | foreach ($items as $item) { |
||
101 | $js = $item->updateForAjax($js); |
||
102 | $buyable = $item->Buyable(true); |
||
103 | if ($buyable) { |
||
104 | //products in cart |
||
105 | $inCartArray[] = $buyable->AJAXDefinitions()->UniqueIdentifier(); |
||
106 | //HACK TO INCLUDE PRODUCT IN PRODUCT VARIATION |
||
107 | if (is_a($buyable, 'ProductVariation')) { |
||
108 | $inCartArray[] = $buyable->Product()->AJAXDefinitions()->UniqueIdentifier(); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | //in cart items |
||
115 | $js[] = array( |
||
116 | 't' => 'replaceclass', |
||
117 | 's' => $inCartArray, |
||
118 | 'p' => $currentOrder->AJAXDefinitions()->ProductListItemClassName(), |
||
119 | 'v' => $currentOrder->AJAXDefinitions()->ProductListItemInCartClassName(), |
||
120 | 'without' => $currentOrder->AJAXDefinitions()->ProductListItemNotInCartClassName(), |
||
121 | ); |
||
122 | |||
123 | //order modifiers |
||
124 | $modifiers = $currentOrder->Modifiers(); |
||
125 | if ($modifiers->count()) { |
||
126 | foreach ($modifiers as $modifier) { |
||
127 | $js = $modifier->updateForAjax($js); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | //order |
||
132 | $js = $currentOrder->updateForAjax($js); |
||
133 | |||
134 | //messages |
||
135 | if (is_array($messages)) { |
||
136 | $js[] = array( |
||
137 | 't' => 'id', |
||
138 | 's' => $ajaxObject->TableMessageID(), |
||
139 | 'p' => 'innerHTML', |
||
140 | 'v' => $messagesImploded, |
||
141 | 'isOrderMessage' => true, |
||
142 | ); |
||
143 | $js[] = array( |
||
144 | 't' => 'id', |
||
145 | 's' => $ajaxObject->TableMessageID(), |
||
146 | 'p' => 'hide', |
||
147 | 'v' => 0, |
||
148 | ); |
||
149 | } else { |
||
150 | $js[] = array( |
||
151 | 't' => 'id', |
||
152 | 's' => $ajaxObject->TableMessageID(), |
||
153 | 'p' => 'hide', |
||
154 | 'v' => 1, |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | //TO DO: set it up in such a way that it specifically requests one of these |
||
159 | $templates = EcommerceConfig::get('CartResponse', 'cart_responses_required'); |
||
160 | foreach ($templates as $idMethod => $template) { |
||
161 | $selector = $ajaxObject->$idMethod(); |
||
162 | $classOrID = 'id'; |
||
163 | if (stripos($selector, 'class') !== false) { |
||
164 | $classOrID = 'class'; |
||
165 | } |
||
166 | $js[] = array( |
||
167 | 't' => $classOrID, |
||
168 | 's' => $selector, |
||
169 | 'p' => 'innerHTML', |
||
170 | //note the space is a hack to return something! |
||
171 | 'v' => ' '.$currentOrder->renderWith($template), |
||
172 | ); |
||
173 | } |
||
174 | //now can check if it needs to be reloaded |
||
175 | if (self::$force_reload) { |
||
176 | $js = array( |
||
177 | 'reload' => 1, |
||
178 | ); |
||
179 | } else { |
||
180 | $js[] = array( |
||
181 | 'reload' => 0, |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | //merge and return |
||
186 | if (is_array($additionalData) && count($additionalData)) { |
||
187 | $js = array_merge($js, $additionalData); |
||
188 | } |
||
189 | //TODO: remove doubles? |
||
190 | //turn HTMLText (et al.) objects into text |
||
191 | foreach ($js as $key => $node) { |
||
192 | if (isset($node['v'])) { |
||
193 | if ($node['v'] instanceof DBField) { |
||
194 | $js[$key]['v'] = $node['v']->forTemplate(); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | $json = json_encode($js); |
||
199 | $json = str_replace('\t', ' ', $json); |
||
200 | $json = str_replace('\r', ' ', $json); |
||
201 | $json = str_replace('\n', ' ', $json); |
||
202 | $json = preg_replace('/\s\s+/', ' ', $json); |
||
203 | if (Director::isDev()) { |
||
204 | $json = str_replace('{', "\r\n{", $json); |
||
205 | } |
||
206 | |||
207 | return $json; |
||
208 | } |
||
209 | } |
||
210 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.