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

DefaultBoxSorter::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
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 DefaultBoxSorter implements BoxSorter
12
{
13 19
    public function compare(Box $boxA, Box $boxB): int
14
    {
15 19
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
16 19
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
17
18 19
        $volumeDecider = $boxAVolume <=> $boxBVolume; // try smallest box first
19
20 19
        if ($volumeDecider !== 0) {
21 16
            return $volumeDecider;
22
        }
23
24 3
        $emptyWeightDecider = $boxA->getEmptyWeight() <=> $boxB->getEmptyWeight(); // with smallest empty weight
25 3
        if ($emptyWeightDecider !== 0) {
26 2
            return $emptyWeightDecider;
27
        }
28
29
        // maximum weight capacity as fallback decider
30 1
        return ($boxA->getMaxWeight() - $boxA->getEmptyWeight()) <=> ($boxB->getMaxWeight() - $boxB->getEmptyWeight());
31
    }
32
}
33