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

DefaultBoxSorter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A compare() 0 18 3
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