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
 * 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 26
    public function compare(Box $boxA, Box $boxB): int
14
    {
15 26
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
16 26
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
17
18 26
        $volumeDecider = $boxAVolume <=> $boxBVolume; // try smallest box first
19
20 26
        if ($volumeDecider !== 0) {
21 21
            return $volumeDecider;
22
        }
23
24 5
        $emptyWeightDecider = $boxA->getEmptyWeight() <=> $boxB->getEmptyWeight(); // with smallest empty weight
25 5
        if ($emptyWeightDecider !== 0) {
26 4
            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