Passed
Pull Request — master (#549)
by
unknown
15:13 queued 13:32
created

DefaultBoxSorter::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

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 6
cts 10
cp 0.6
crap 3.576
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 20
    public function compare(Box $boxA, Box $boxB): int
14
    {
15 20
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
16 20
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
17
18 20
        $volumeDecider = $boxAVolume <=> $boxBVolume; // try smallest box first
19
20 20
        if ($volumeDecider !== 0) {
21 20
            return $volumeDecider;
22
        }
23
24
        $emptyWeightDecider = $boxA->getEmptyWeight() <=> $boxB->getEmptyWeight(); // with smallest empty weight
25
        if ($emptyWeightDecider !== 0) {
26
            return $emptyWeightDecider;
27
        }
28
29
        // maximum weight capacity as fallback decider
30
        return ($boxA->getMaxWeight() - $boxA->getEmptyWeight()) <=> ($boxB->getMaxWeight() - $boxB->getEmptyWeight());
31
    }
32
}
33