DefaultItemSorter::compare()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

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 7
cp 1
crap 3
rs 10
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 DefaultItemSorter implements ItemSorter
13 100
{
14
    public function compare(Item $itemA, Item $itemB): int
15 100
    {
16 100
        $volumeDecider = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth() <=> $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
17 60
        if ($volumeDecider !== 0) {
18
            return $volumeDecider;
19 86
        }
20 86
        $weightDecider = $itemB->getWeight() <=> $itemA->getWeight();
21 4
        if ($weightDecider !== 0) {
22
            return $weightDecider;
23
        }
24 82
25
        return $itemA->getDescription() <=> $itemB->getDescription();
26
    }
27
}
28