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

src/ItemList.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 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 Item $itemA
23
     * @param Item $itemB
24
     *
25
     * @return int
26
     */
27 34
    public function compare($itemA, $itemB)
28
    {
29 34
        $itemAVolume = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
30 34
        $itemBVolume = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
31
32 34 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 14
            return 1;
34 34
        } elseif ($itemAVolume < $itemBVolume) {
35 14
            return -1;
36
        } else {
37 31
            return $itemA->getWeight() - $itemB->getWeight();
38
        }
39
    }
40
41
    /**
42
     * Get copy of this list as a standard PHP array
43
     * @return array
44
     */
45 25
    public function asArray()
46
    {
47 25
        $return = [];
48 25
        foreach (clone $this as $item) {
49 25
            $return[] = $item;
50
        }
51 25
        return $return;
52
    }
53
}
54