DefaultBoxSorter::compare()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 3
eloc 9
c 2
b 0
f 2
nc 3
nop 2
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
/**
4
 * Box packing (3D bin packing, knapsack problem).
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace DVDoug\BoxPacker;
11
12
class DefaultBoxSorter implements BoxSorter
13 28
{
14
    public function compare(Box $boxA, Box $boxB): int
15 28
    {
16 28
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
17
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
18 28
19
        $volumeDecider = $boxAVolume <=> $boxBVolume; // try smallest box first
20 28
21 23
        if ($volumeDecider !== 0) {
22
            return $volumeDecider;
23
        }
24 7
25 7
        $emptyWeightDecider = $boxA->getEmptyWeight() <=> $boxB->getEmptyWeight(); // with smallest empty weight
26 4
        if ($emptyWeightDecider !== 0) {
27
            return $emptyWeightDecider;
28
        }
29
30 3
        // maximum weight capacity as fallback decider
31
        return ($boxA->getMaxWeight() - $boxA->getEmptyWeight()) <=> ($boxB->getMaxWeight() - $boxB->getEmptyWeight());
32
    }
33
}
34