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