Passed
Push — 3.x ( 071761...f78397 )
by Doug
03:06
created

DefaultItemSorter::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
class DefaultItemSorter implements ItemSorter
12
{
13 83
    public function compare(Item $itemA, Item $itemB): int
14
    {
15 83
        $volumeDecider = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth() <=> $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
16 83
        if ($volumeDecider !== 0) {
17 47
            return $volumeDecider;
18
        }
19 74
        $weightDecider = $itemB->getWeight() <=> $itemA->getWeight();
20 74
        if ($weightDecider !== 0) {
21 4
            return $weightDecider;
22
        }
23
24 70
        return $itemA->getDescription() <=> $itemB->getDescription();
25
    }
26
}
27