Completed
Push — master ( 5c5914...1745af )
by Doug
05:42
created

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
namespace DVDoug\BoxPacker;
8
9
/**
10
 * List of possible packed box choices, ordered by utilisation (item count, volume)
11
 * @author Doug Wright
12
 * @package BoxPacker
13
 */
14
class PackedBoxList extends \SplMinHeap
15
{
16
17
    /**
18
     * Average (mean) weight of boxes
19
     * @var float
20
     */
21
    protected $meanWeight;
22
23
    /**
24
     * Compare elements in order to place them correctly in the heap while sifting up.
25
     * @see \SplMinHeap::compare()
26
     */
27 6 View Code Duplication
    public function compare($boxA, $boxB)
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...
28
    {
29 6
        $choice = $boxA->getItems()->count() - $boxB->getItems()->count();
30 6
        if ($choice === 0) {
31 3
            $choice = $boxB->getBox()->getInnerVolume() - $boxA->getBox()->getInnerVolume();
32 3
        }
33 6
        if ($choice === 0) {
34 3
            $choice = $boxA->getWeight() - $boxB->getWeight();
35 3
        }
36 6
        return $choice;
37
    }
38
39
    /**
40
     * Reversed version of compare
41
     * @return int
0 ignored issues
show
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     */
43 2 View Code Duplication
    public function reverseCompare($boxA, $boxB)
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...
44
    {
45 2
        $choice = $boxB->getItems()->count() - $boxA->getItems()->count();
46 2
        if ($choice === 0) {
47 1
            $choice = $boxA->getBox()->getInnerVolume() - $boxB->getBox()->getInnerVolume();
48 1
        }
49 2
        if ($choice === 0) {
50 1
            $choice = $boxB->getWeight() - $boxA->getWeight();
51 1
        }
52 2
        return $choice;
53
    }
54
55
    /**
56
     * Calculate the average (mean) weight of the boxes
57
     * @return float
58
     */
59 5
    public function getMeanWeight()
60
    {
61
62 5
        if (!is_null($this->meanWeight)) {
63 4
            return $this->meanWeight;
64
        }
65
66 5
        foreach (clone $this as $box) {
67 5
            $this->meanWeight += $box->getWeight();
68 5
        }
69
70 5
        return $this->meanWeight /= $this->count();
71
72
    }
73
74
    /**
75
     * Calculate the variance in weight between these boxes
76
     * @return float
77
     */
78 5
    public function getWeightVariance()
79
    {
80 5
        $mean = $this->getMeanWeight();
81
82 5
        $weightVariance = 0;
83 5
        foreach (clone $this as $box) {
84 5
            $weightVariance += pow($box->getWeight() - $mean, 2);
85 5
        }
86
87 5
        return $weightVariance / $this->count();
88
89
    }
90
91
    /**
92
     * Get volume utilisation of the set of packed boxes
93
     * @return float
94
     */
95 6
    public function getVolumeUtilisation()
96
    {
97 1
        $itemVolume = 0;
98 1
        $boxVolume = 0;
99
100
        /** @var PackedBox $box */
101 1
        foreach (clone $this as $box) {
102 1
            $boxVolume += $box->getBox()->getInnerVolume();
103
104
            /** @var Item $item */
105 1
            foreach (clone $box->getItems() as $item) {
106 1
                $itemVolume += $item->getVolume();
107 1
            }
108 6
        }
109
110 1
        return round($itemVolume / $boxVolume * 100, 1);
111
    }
112
113
    /**
114
     * Do a bulk insert
115
     * @param array $boxes
116
     */
117 4
    public function insertFromArray(array $boxes)
118
    {
119 4
        foreach ($boxes as $box) {
120 3
            $this->insert($box);
121 4
        }
122 4
    }
123
}
124