Passed
Pull Request — master (#469)
by
unknown
12:53
created

DefaultItemSorter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 8
c 2
b 0
f 2
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A compare() 0 12 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 DefaultItemSorter implements ItemSorter
12
{
13 102
    public function compare(Item $itemA, Item $itemB): int
14
    {
15 102
        $volumeDecider = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth() <=> $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
16 102
        if ($volumeDecider !== 0) {
17 58
            return $volumeDecider;
18
        }
19 90
        $weightDecider = $itemB->getWeight() <=> $itemA->getWeight();
20 90
        if ($weightDecider !== 0) {
21
            return $weightDecider;
22
        }
23
24 90
        return $itemA->getDescription() <=> $itemB->getDescription();
25
    }
26
}
27