Passed
Push — dependabot/npm_and_yarn/docs/v... ( cbefaf )
by
unknown
08:40
created

LayerStabiliser::compare()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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
use function usort;
12
13
/**
14
 * Applies load stability to generated result.
15
 * @internal
16
 */
17
class LayerStabiliser
18
{
19
    /**
20
     * @param PackedLayer[] $packedLayers
21
     *
22
     * @return PackedLayer[]
23
     */
24 92
    public function stabilise(array $packedLayers): array
25
    {
26
        // first re-order according to footprint
27 92
        $stabilisedLayers = [];
28 92
        usort($packedLayers, $this->compare(...));
29
30
        // then for each item in the layer, re-calculate each item's z position
31 92
        $currentZ = 0;
32 92
        foreach ($packedLayers as $oldZLayer) {
33 92
            $oldZStart = $oldZLayer->getStartZ();
34 92
            $newZLayer = new PackedLayer();
35 92
            foreach ($oldZLayer->getItems() as $oldZItem) {
36 92
                $newZ = $oldZItem->getZ() - $oldZStart + $currentZ;
37 92
                $newZItem = new PackedItem($oldZItem->getItem(), $oldZItem->getX(), $oldZItem->getY(), $newZ, $oldZItem->getWidth(), $oldZItem->getLength(), $oldZItem->getDepth());
38 92
                $newZLayer->insert($newZItem);
39
            }
40
41 92
            $stabilisedLayers[] = $newZLayer;
42 92
            $currentZ += $newZLayer->getDepth();
43
        }
44
45 92
        return $stabilisedLayers;
46
    }
47
48 45
    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...
49
    {
50 45
        return ($layerB->getFootprint() <=> $layerA->getFootprint()) ?: ($layerB->getDepth() <=> $layerA->getDepth());
51
    }
52
}
53