Completed
Push — master ( 17ecec...f96e7f )
by Doug
51:32 queued 50:04
created

src/PackedBoxList.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem)
4
 * @package BoxPacker
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * List of possible packed box choices, ordered by utilisation (item count, volume)
12
 * @author Doug Wright
13
 * @package BoxPacker
14
 */
15
class PackedBoxList extends \SplMinHeap
16
{
17
18
    /**
19
     * Average (mean) weight of boxes
20
     * @var float
21
     */
22
    protected $meanWeight;
23
24
    /**
25
     * Compare elements in order to place them correctly in the heap while sifting up.
26
     * @see \SplMinHeap::compare()
27
     *
28
     * @param PackedBox $boxA
29
     * @param PackedBox $boxB
30
     *
31
     * @return int
32
     */
33 8 View Code Duplication
    public function compare($boxA, $boxB): int
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35 8
        $choice = $boxA->getItems()->count() - $boxB->getItems()->count();
36 8
        if ($choice === 0) {
37 4
            $choice = $boxB->getInnerVolume() - $boxA->getInnerVolume();
38
        }
39 8
        if ($choice === 0) {
40 4
            $choice = $boxA->getWeight() - $boxB->getWeight();
41
        }
42 8
        return $choice;
43
    }
44
45
    /**
46
     * Reversed version of compare
47
     *
48
     * @param PackedBox $boxA
49
     * @param PackedBox $boxB
50
     *
51
     * @return int
52
     */
53 1 View Code Duplication
    public function reverseCompare($boxA, $boxB): int
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 1
        $choice = $boxB->getItems()->count() - $boxA->getItems()->count();
56 1
        if ($choice === 0) {
57
            $choice = $boxA->getInnerVolume() - $boxB->getInnerVolume();
58
        }
59 1
        if ($choice === 0) {
60
            $choice = $boxB->getWeight() - $boxA->getWeight();
61
        }
62 1
        return $choice;
63
    }
64
65
    /**
66
     * Calculate the average (mean) weight of the boxes
67
     * @return float
68
     */
69 7
    public function getMeanWeight(): float
70
    {
71
72 7
        if (!is_null($this->meanWeight)) {
73 6
            return $this->meanWeight;
74
        }
75
76 7
        foreach (clone $this as $box) {
77 7
            $this->meanWeight += $box->getWeight();
78
        }
79
80 7
        return $this->meanWeight /= $this->count();
81
82
    }
83
84
    /**
85
     * Calculate the variance in weight between these boxes
86
     * @return float
87
     */
88 7
    public function getWeightVariance(): float
89
    {
90 7
        $mean = $this->getMeanWeight();
91
92 7
        $weightVariance = 0;
93 7
        foreach (clone $this as $box) {
94 7
            $weightVariance += ($box->getWeight() - $mean) ** 2;
95
        }
96
97 7
        return round($weightVariance / $this->count(), 1);
98
99
    }
100
101
    /**
102
     * Get volume utilisation of the set of packed boxes
103
     * @return float
104
     */
105 1
    public function getVolumeUtilisation(): float
106
    {
107 1
        $itemVolume = 0;
108 1
        $boxVolume = 0;
109
110
        /** @var PackedBox $box */
111 1
        foreach (clone $this as $box) {
112 1
            $boxVolume += $box->getInnerVolume();
113
114
            /** @var PackedItem $item */
115 1 View Code Duplication
            foreach (clone $box->getItems() as $item) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116 1
                $itemVolume += ($item->getItem()->getWidth() * $item->getItem()->getLength() * $item->getItem()->getDepth());
117
            }
118
        }
119
120 1
        return round($itemVolume / $boxVolume * 100, 1);
121
    }
122
123
    /**
124
     * Do a bulk insert
125
     * @param array $boxes
126
     */
127 6
    public function insertFromArray(array $boxes): void
128
    {
129 6
        foreach ($boxes as $box) {
130 4
            $this->insert($box);
131
        }
132
    }
133
}
134