Skip to content

Commit 5f4cc5f

Browse files
#26622 - Reduce cyclomatic complexity of initTotals function
1 parent 90c3540 commit 5f4cc5f

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

app/code/Magento/SalesRule/Model/Validator.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,7 @@ public function initTotals($items, Address $address)
383383

384384
foreach ($items as $item) {
385385
//Skipping child items to avoid double calculations
386-
if ($item->getParentItemId() || $item->getParentItem()) {
387-
continue;
388-
}
389-
if (!$rule->getActions()->validate($item)) {
390-
continue;
391-
}
392-
if (!$this->canApplyDiscount($item)) {
386+
if (!$this->isValidItemForRule($item, $rule)) {
393387
continue;
394388
}
395389
$qty = $this->validatorUtility->getItemQty($item, $rule);
@@ -409,6 +403,32 @@ public function initTotals($items, Address $address)
409403
return $this;
410404
}
411405

406+
/**
407+
* Determine if quote item is valid for a given sales rule
408+
* @param AbstractItem $item
409+
* @param Rule $rule
410+
* @return bool
411+
*/
412+
protected function isValidItemForRule(
413+
AbstractItem $item,
414+
Rule $rule
415+
) {
416+
/** @var AbstractItem $item */
417+
if ($item->getParentItemId()) {
418+
return false;
419+
}
420+
if ($item->getParentItem()) {
421+
return false;
422+
}
423+
if (!$rule->getActions()->validate($item)) {
424+
return false;
425+
}
426+
if (!$this->canApplyDiscount($item)) {
427+
return false;
428+
}
429+
return true;
430+
}
431+
412432
/**
413433
* Return item price
414434
*

0 commit comments

Comments
 (0)