Passed
Push — 1.x-dev ( 9106e4...cc1b76 )
by Doug
02:13
created

PackedBoxList::compare()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * List of possible packed box choices, ordered by utilisation (item count, volume).
12
 *
13
 * @author Doug Wright
14
 */
15
class PackedBoxList extends \SplMinHeap
16
{
17
    /**
18
     * Average (mean) weight of boxes.
19
     *
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
     *
27
     * @see \SplMinHeap::compare()
28
     *
29
     * @param PackedBox $boxA
30
     * @param PackedBox $boxB
31
     *
32
     * @return int
33
     */
34 7 View Code Duplication
    public function compare($boxA, $boxB)
0 ignored issues
show
Duplication introduced by
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...
35
    {
36 7
        $choice = $boxA->getItems()->count() - $boxB->getItems()->count();
37 7
        if ($choice === 0) {
38 6
            $choice = $boxB->getBox()->getInnerVolume() - $boxA->getBox()->getInnerVolume();
39
        }
40 7
        if ($choice === 0) {
41 6
            $choice = $boxA->getWeight() - $boxB->getWeight();
42
        }
43
44 7
        return $choice;
45
    }
46
47
    /**
48
     * Reversed version of compare.
49
     *
50
     * @param PackedBox $boxA
51
     * @param PackedBox $boxB
52
     *
53
     * @return int
54
     */
55 View Code Duplication
    public function reverseCompare($boxA, $boxB)
0 ignored issues
show
Duplication introduced by
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...
56
    {
57
        $choice = $boxB->getItems()->count() - $boxA->getItems()->count();
58
        if ($choice === 0) {
59
            $choice = $boxA->getBox()->getInnerVolume() - $boxB->getBox()->getInnerVolume();
60
        }
61
        if ($choice === 0) {
62
            $choice = $boxB->getWeight() - $boxA->getWeight();
63
        }
64
65
        return $choice;
66
    }
67
68
    /**
69
     * Calculate the average (mean) weight of the boxes.
70
     *
71
     * @return float
72
     */
73 3
    public function getMeanWeight()
74
    {
75 3
        if (!is_null($this->meanWeight)) {
76 1
            return $this->meanWeight;
77
        }
78
79 3
        foreach (clone $this as $box) {
80 3
            $this->meanWeight += $box->getWeight();
81
        }
82
83 3
        return $this->meanWeight /= $this->count();
84
    }
85
86
    /**
87
     * Calculate the variance in weight between these boxes.
88
     *
89
     * @return float
90
     */
91 2
    public function getWeightVariance()
92
    {
93 2
        $mean = $this->getMeanWeight();
94
95 2
        $weightVariance = 0;
96 2
        foreach (clone $this as $box) {
97 2
            $weightVariance += pow($box->getWeight() - $mean, 2);
98
        }
99
100 2
        return round($weightVariance / $this->count(), 1);
101
    }
102
103
    /**
104
     * Get volume utilisation of the set of packed boxes.
105
     *
106
     * @return float
107
     */
108 1
    public function getVolumeUtilisation()
109
    {
110 1
        $itemVolume = 0;
111 1
        $boxVolume = 0;
112
113
        /** @var PackedBox $box */
114 1
        foreach (clone $this as $box) {
115 1
            $boxVolume += $box->getBox()->getInnerVolume();
116
117
            /** @var PackedItem $item */
118 1
            foreach (clone $box->getItems() as $item) {
119 1
                $itemVolume += $item->getVolume();
120
            }
121
        }
122
123 1
        return round($itemVolume / $boxVolume * 100, 1);
124
    }
125
126
    /**
127
     * Do a bulk insert.
128
     *
129
     * @param array $boxes
130
     */
131 2
    public function insertFromArray(array $boxes)
132
    {
133 2
        foreach ($boxes as $box) {
134 2
            $this->insert($box);
135
        }
136 2
    }
137
}
138