Completed
Push — master ( 4734ca...53d220 )
by Doug
14:09
created

src/PackedItemList.php (1 issue)

Upgrade to new PHP Analysis Engine

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 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
        $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth();
30 31
        $itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth();
31
32 31 View Code Duplication
        if ($itemAVolume > $itemBVolume) {
0 ignored issues
show
This code seems to be duplicated across 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...
33 12
            return 1;
34 27
        } elseif ($itemAVolume < $itemBVolume) {
35 4
            return -1;
36
        } else {
37 27
            return $itemA->getItem()->getWeight() - $itemB->getItem()->getWeight();
38
        }
39
    }
40
41
    /**
42
     * Get copy of this list as a standard PHP array
43
     * @return PackedItem[]
44
     */
45 1
    public function asArray()
46
    {
47 1
        $return = [];
48 1
        foreach (clone $this as $item) {
49 1
            $return[] = $item;
50
        }
51 1
        return $return;
52
    }
53
54
    /**
55
     * Get copy of this list as a standard PHP array
56
     * @return Item[]
57
     */
58 3
    public function asItemArray()
59
    {
60 3
        $return = [];
61 3
        foreach (clone $this as $item) {
62 3
            $return[] = $item->getItem();
63
        }
64 3
        return $return;
65
    }
66
}
67