Completed
Push — remove_logging ( 2ce4b7 )
by Doug
01:52
created

PackedBoxList::insertFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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
     * Variance in weight between boxes
25
     * @var float
26
     */
27
    protected $weightVariance;
28
29
    /**
30
     * Compare elements in order to place them correctly in the heap while sifting up.
31
     * @see \SplMinHeap::compare()
32
     */
33 6 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...
34
    {
35 6
        $choice = $boxA->getItems()->count() - $boxB->getItems()->count();
36 6
        if ($choice === 0) {
37 3
            $choice = $boxB->getBox()->getInnerVolume() - $boxA->getBox()->getInnerVolume();
38 3
        }
39 6
        if ($choice === 0) {
40 3
            $choice = $boxA->getWeight() - $boxB->getWeight();
41 3
        }
42 6
        return $choice;
43
    }
44
45
    /**
46
     * Reversed version of compare
47
     * @return int
0 ignored issues
show
Documentation introduced by
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...
48
     */
49 2 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...
50
    {
51 2
        $choice = $boxB->getItems()->count() - $boxA->getItems()->count();
52 2
        if ($choice === 0) {
53 1
            $choice = $boxA->getBox()->getInnerVolume() - $boxB->getBox()->getInnerVolume();
54 1
        }
55 2
        if ($choice === 0) {
56 1
            $choice = $boxB->getWeight() - $boxA->getWeight();
57 1
        }
58 2
        return $choice;
59
    }
60
61
    /**
62
     * Calculate the average (mean) weight of the boxes
63
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be double|integer?

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...
64
     */
65 4
    public function getMeanWeight()
66
    {
67
68 4
        if (!is_null($this->meanWeight)) {
69
            return $this->meanWeight;
70
        }
71
72 4
        foreach (clone $this as $box) {
73 4
            $this->meanWeight += $box->getWeight();
74 4
        }
75
76 4
        return $this->meanWeight /= $this->count();
77
78
    }
79
80
    /**
81
     * Calculate the variance in weight between these boxes
82
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be double|integer?

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...
83
     */
84 1
    public function getWeightVariance()
85
    {
86
87 1
        if (!is_null($this->weightVariance)) {
88
            return $this->weightVariance;
89
        }
90
91 1
        $mean = $this->getMeanWeight();
92
93 1
        foreach (clone $this as $box) {
94 1
            $this->weightVariance += pow($box->getWeight() - $mean, 2);
95 1
        }
96
97 1
        return $this->weightVariance /= $this->count();
98
99
    }
100
101
    /**
102
     * Get volume utilisation of the set of packed boxes
103
     * @return float
104
     */
105
    public function getVolumeUtilisation()
106
    {
107
        $itemVolume = 0;
108
        $boxVolume = 0;
109
110
        /** @var PackedBox $box */
111
        foreach (clone $this as $box) {
112
            $boxVolume += $box->getBox()->getInnerVolume();
113
114
            /** @var Item $item */
115
            foreach (clone $box->getItems() as $item) {
116
                $itemVolume += $item->getVolume();
117
            }
118
        }
119
120
        return round($itemVolume / $boxVolume * 100, 1);
121
    }
122
123
    /**
124
     * Do a bulk insert
125
     * @param array $boxes
126
     */
127 4
    public function insertFromArray(array $boxes)
128
    {
129 4
        foreach ($boxes as $box) {
130 3
            $this->insert($box);
131 4
        }
132 4
    }
133
}
134