Passed
Push — 2.x-dev ( e5166d...a75e6c )
by Doug
02:18
created

PackedItemList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 66
ccs 11
cts 21
cp 0.5238
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A asItemList() 0 8 2
A asArray() 0 8 2
A compare() 0 8 3
A asItemArray() 0 8 2
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 packed items, ordered by volume.
12
 *
13
 * @author Doug Wright
14
 */
15
class PackedItemList extends \SplMaxHeap
16
{
17
    /**
18
     * Compare elements in order to place them correctly in the heap while sifting up.
19
     *
20
     * @see \SplMaxHeap::compare()
21
     *
22
     * @param PackedItem $itemA
23
     * @param PackedItem $itemB
24
     *
25
     * @return int
26
     */
27 18
    public function compare($itemA, $itemB)
28
    {
29 18
        if ($itemA->getItem()->getVolume() > $itemB->getItem()->getVolume()) {
30 9
            return 1;
31 16
        } elseif ($itemA->getItem()->getVolume() < $itemB->getItem()->getVolume()) {
32 3
            return -1;
33
        } else {
34 16
            return $itemA->getItem()->getWeight() - $itemB->getItem()->getWeight();
35
        }
36
    }
37
38
    /**
39
     * Get copy of this list as a standard PHP array.
40
     *
41
     * @return PackedItem[]
42
     */
43
    public function asArray()
44
    {
45
        $return = [];
46
        foreach (clone $this as $item) {
47
            $return[] = $item;
48
        }
49
50
        return $return;
51
    }
52
53
    /**
54
     * Get copy of this list as a standard PHP array.
55
     *
56
     * @return Item[]
57
     */
58
    public function asItemArray()
59
    {
60
        $return = [];
61
        foreach (clone $this as $item) {
62
            $return[] = $item->getItem();
63
        }
64
65
        return $return;
66
    }
67
68
    /**
69
     * Get copy of this list as a standard PHP array.
70
     *
71
     * @return ItemList
72
     */
73 20
    public function asItemList()
74
    {
75 20
        $return = new ItemList();
76 20
        foreach (clone $this as $packedItem) {
77 20
            $return->insert($packedItem->getItem());
78
        }
79
80 20
        return $return;
81
    }
82
}
83