Completed
Push — master ( 9610e0...320eee )
by Doug
08:43
created

PackedItemList   A

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 10 3
A asArray() 0 8 2
A asItemArray() 0 8 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 packed items, ordered by volume
11
 * @author Doug Wright
12
 * @package BoxPacker
13
 */
14
class PackedItemList extends \SplMaxHeap
15
{
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 31
    public function compare($itemA, $itemB)
28
    {
29 31
        if ($itemA->getItem()->getVolume() > $itemB->getItem()->getVolume()) {
30 12
            return 1;
31 27
        } elseif ($itemA->getItem()->getVolume() < $itemB->getItem()->getVolume()) {
32 4
            return -1;
33
        } else {
34 27
            return $itemA->getItem()->getWeight() - $itemB->getItem()->getWeight();
35
        }
36
    }
37
38
    /**
39
     * Get copy of this list as a standard PHP array
40
     * @return PackedItem[]
41
     */
42 1
    public function asArray()
43
    {
44 1
        $return = [];
45 1
        foreach (clone $this as $item) {
46 1
            $return[] = $item;
47
        }
48 1
        return $return;
49
    }
50
51
    /**
52
     * Get copy of this list as a standard PHP array
53
     * @return Item[]
54
     */
55 3
    public function asItemArray()
56
    {
57 3
        $return = [];
58 3
        foreach (clone $this as $item) {
59 3
            $return[] = $item->getItem();
60
        }
61 3
        return $return;
62
    }
63
}
64