LayerStabiliser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 3 2
A stabilise() 0 22 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
use function usort;
13
14
/**
15
 * Applies load stability to generated result.
16
 * @internal
17
 */
18
class LayerStabiliser
19
{
20
    /**
21
     * @param PackedLayer[] $packedLayers
22
     *
23
     * @return PackedLayer[]
24 97
     */
25
    public function stabilise(array $packedLayers): array
26
    {
27 97
        // first re-order according to footprint
28 97
        $stabilisedLayers = [];
29
        usort($packedLayers, $this->compare(...));
30
31 97
        // then for each item in the layer, re-calculate each item's z position
32 97
        $currentZ = 0;
33 97
        foreach ($packedLayers as $oldZLayer) {
34 97
            $oldZStart = $oldZLayer->getStartZ();
35 97
            $newZLayer = new PackedLayer();
36 97
            foreach ($oldZLayer->getItems() as $oldZItem) {
37 97
                $newZ = $oldZItem->z - $oldZStart + $currentZ;
38 97
                $newZItem = new PackedItem($oldZItem->item, $oldZItem->x, $oldZItem->y, $newZ, $oldZItem->width, $oldZItem->length, $oldZItem->depth);
39
                $newZLayer->insert($newZItem);
40
            }
41 97
42 97
            $stabilisedLayers[] = $newZLayer;
43
            $currentZ += $newZLayer->getDepth();
44
        }
45 97
46
        return $stabilisedLayers;
47
    }
48 50
49
    private function compare(PackedLayer $layerA, PackedLayer $layerB): int
0 ignored issues
show
Unused Code introduced by
The method compare() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
50 50
    {
51
        return ($layerB->getFootprint() <=> $layerA->getFootprint()) ?: ($layerB->getDepth() <=> $layerA->getDepth());
52
    }
53
}
54