Conditions | 55 |
Paths | > 20000 |
Total Lines | 185 |
Code Lines | 132 |
Lines | 63 |
Ratio | 34.05 % |
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 |
||
178 | /** |
||
179 | * Getting items params with params |
||
180 | * |
||
181 | * @param array $params |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getItemsParams($params = []) { |
||
185 | $params['filters'] = []; |
||
186 | $selectOptions = Ecommerce\OptionsParser::parse($params); |
||
187 | $items = Ecommerce\Item::getList($selectOptions); |
||
188 | if (!$items) { |
||
189 | return []; |
||
190 | } |
||
191 | $items = Ecommerce\Item\Param::getList([ |
||
192 | 'where' => ['item_id', array_keys($items), 'IN'], |
||
193 | 'join' => [[Ecommerce\Item\Option::table(), Ecommerce\Item\Option::index() . ' = ' . \Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' and ' . \Ecommerce\Item\Option::colPrefix() . 'searchable = 1', 'inner']], |
||
194 | 'distinct' => \Ecommerce\Item\Option::index() |
||
195 | ]); |
||
196 | return $items; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Getting items with params |
||
201 | * |
||
202 | * @param array $params |
||
203 | * @return array |
||
204 | */ |
||
205 | public function getItems($params = []) { |
||
206 | $selectOptions = Ecommerce\OptionsParser::parse($params); |
||
207 | $items = Ecommerce\Item::getList($selectOptions); |
||
208 | return $items; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Return count of items with params |
||
213 | * |
||
214 | * @param array $params |
||
215 | * @return int |
||
216 | */ |
||
217 | public function getItemsCount($params = []) { |
||
218 | $selectOptions = Ecommerce\OptionsParser::parse($params); |
||
219 | $selectOptions['distinct'] = \Ecommerce\Item::index(); |
||
220 | $counts = Ecommerce\Item::getCount($selectOptions); |
||
221 | if (is_array($counts)) { |
||
222 | $sum = 0; |
||
223 | foreach ($counts as $count) { |
||
224 | $sum +=$count['count']; |
||
225 | } |
||
226 | return $sum; |
||
227 | } |
||
228 | return $counts; |
||
229 | } |
||
230 | |||
231 | View Code Duplication | public function viewsCategoryList($inherit = true) { |
|
232 | $return = []; |
||
233 | if ($inherit) { |
||
234 | $return['inherit'] = 'Как у родителя'; |
||
235 | } |
||
236 | $return['itemList'] = 'Список товаров'; |
||
237 | $conf = App::$primary->view->template->config; |
||
238 | if (!empty($conf['files']['modules']['Ecommerce'])) { |
||
239 | foreach ($conf['files']['modules']['Ecommerce'] as $file) { |
||
240 | if ($file['type'] == 'Category') { |
||
241 | $return[$file['file']] = $file['name']; |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | return $return; |
||
246 | } |
||
247 | |||
248 | View Code Duplication | public function templatesCategoryList() { |
|
249 | $return = [ |
||
250 | 'inherit' => 'Как у родителя', |
||
251 | 'current' => 'Текущая тема' |
||
252 | ]; |
||
253 | |||
254 | $conf = App::$primary->view->template->config; |
||
255 | |||
256 | if (!empty($conf['files']['aditionTemplateFiels'])) { |
||
257 | foreach ($conf['files']['aditionTemplateFiels'] as $file) { |
||
258 | $return[$file['file']] = '- ' . $file['name']; |
||
259 | } |
||
260 | } |
||
261 | return $return; |
||
262 | } |
||
263 | |||
264 | public function cartStatusDetector($event) { |
||
265 | $cart = $event['eventObject']; |
||
266 | if (!empty($cart->_changedParams['cart_cart_status_id'])) { |
||
267 | $cart->date_status = date('Y-m-d H:i:s'); |
||
268 | $event = new Ecommerce\Cart\Event(['cart_id' => $cart->id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 5, 'info' => $cart->cart_status_id]); |
||
269 | $event->save(); |
||
270 | |||
271 | $prev_status_id = $cart->_changedParams['cart_cart_status_id']; |
||
272 | $now_status_id = $cart->cart_status_id; |
||
273 | |||
274 | $status = Ecommerce\Cart\Status::getList(['where' => ['id', implode(',', [$prev_status_id, $now_status_id]), 'IN']]); |
||
275 | |||
276 | $prefix = isset(App::$cur->ecommerce->config['orderPrefix']) ? $config = App::$cur->ecommerce->config['orderPrefix'] : ''; |
||
277 | \App::$cur->users->AddUserActivity($cart->user_id, 3, "Статус вашего заказа номер {$prefix}{$cart->id} изменился с {$status[$prev_status_id]->name} на {$status[$now_status_id]->name}"); |
||
278 | |||
279 | if ($cart->cart_status_id == 5) { |
||
280 | Inji::$inst->event('ecommerceCartClosed', $cart); |
||
281 | } |
||
282 | } |
||
283 | return $cart; |
||
284 | } |
||
285 | |||
286 | public function cardTrigger($event) { |
||
287 | $cart = $event['eventObject']; |
||
288 | if ($cart->card) { |
||
289 | $sum = 0; |
||
290 | foreach ($cart->cartItems as $cartItem) { |
||
291 | $sum += $cartItem->final_price * $cartItem->count; |
||
292 | } |
||
293 | $cardItemHistory = new Ecommerce\Card\Item\History(); |
||
294 | $cardItemHistory->amount = $sum; |
||
295 | $cardItemHistory->card_item_id = $cart->card_item_id; |
||
296 | $cardItemHistory->save(); |
||
297 | $cart->card->sum += $sum; |
||
298 | $cart->card->save(); |
||
299 | } |
||
300 | return $cart; |
||
301 | } |
||
302 | |||
303 | public function bonusTrigger($event) { |
||
304 | $cart = $event['eventObject']; |
||
305 | foreach ($cart->cartItems as $cartItem) { |
||
306 | foreach ($cartItem->price->offer->bonuses as $bonus) { |
||
307 | if ($bonus->limited && $bonus->left <= 0) { |
||
308 | continue; |
||
309 | } elseif ($bonus->limited && $bonus->left > 0) { |
||
310 | $bonus->left -= 1; |
||
311 | $bonus->save(); |
||
312 | } |
||
313 | switch ($bonus->type) { |
||
314 | case'currency': |
||
315 | $currency = \Money\Currency::get($bonus->value); |
||
316 | $wallets = App::$cur->money->getUserWallets($cart->user->id); |
||
317 | $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку'); |
||
318 | break; |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | return $cart; |
||
323 | } |
||
324 | |||
325 | View Code Duplication | function sitemap() { |
|
1 ignored issue
–
show
|
|||
326 | $map = []; |
||
327 | $zeroItems = \Ecommerce\Item::getList(['where' => ['category_id', 0]]); |
||
328 | foreach ($zeroItems as $item) { |
||
329 | $map[] = [ |
||
330 | 'name' => $item->name, |
||
331 | 'url' => [ |
||
332 | 'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref()) |
||
333 | ], |
||
334 | ]; |
||
335 | } |
||
336 | |||
337 | $categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]); |
||
338 | $scan = function($category, $scan) { |
||
339 | $map = []; |
||
340 | |||
341 | foreach ($category->items as $item) { |
||
342 | $map[] = [ |
||
343 | 'name' => $item->name, |
||
344 | 'url' => [ |
||
345 | 'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($item->getHref()) |
||
346 | ], |
||
347 | ]; |
||
348 | } |
||
349 | foreach ($category->catalogs as $child) { |
||
350 | $map = array_merge($map, $scan($child, $scan)); |
||
351 | } |
||
352 | return $map; |
||
353 | }; |
||
354 | foreach ($categorys as $category) { |
||
355 | $map = array_merge($map, $scan($category, $scan)); |
||
356 | } |
||
357 | return $map; |
||
358 | } |
||
359 | |||
360 | } |
||
361 |
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: