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

DefaultItemSorter::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 3
eloc 7
c 2
b 0
f 2
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
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