DefaultBoxSorter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 10
c 2
b 0
f 2
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
/**
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