Completed
Push — 2.x-dev ( 8d94cf...55a3c1 )
by Doug
16:17 queued 06:55
created

ItemList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compare() 0 14 5
A asArray() 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 items to be packed, ordered by volume
11
 * @author Doug Wright
12
 * @package BoxPacker
13
 */
14
class ItemList 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 mixed $itemA
23
     * @param mixed $itemB
24
     *
25
     * @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...
26
     */
27 38
    public function compare($itemA, $itemB)
28
    {
29 38
        if ($itemA->getVolume() > $itemB->getVolume()) {
30 18
            return 1;
31 38
        } elseif ($itemA->getVolume() < $itemB->getVolume()) {
32 15
            return -1;
33 36
        } elseif ($itemA->getWeight() !== $itemB->getWeight()) {
34 4
            return $itemA->getWeight() - $itemB->getWeight();
35 34
        } else if ($itemA->getDescription() < $itemB->getDescription()) {
36 13
            return 1;
37
        } else {
38 32
            return -1;
39
        }
40
    }
41
42
    /**
43
     * Get copy of this list as a standard PHP array
44
     * @return array
45
     */
46 28
    public function asArray()
47
    {
48 28
        $return = [];
49 28
        foreach (clone $this as $item) {
50 28
            $return[] = $item;
51
        }
52 28
        return $return;
53
    }
54
}
55