Passed
Push — behat ( a74de5...4872f1 )
by Doug
02:39
created

PackedItemList::asItemArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use ArrayIterator;
12
use Countable;
13
use IteratorAggregate;
14
use Traversable;
15
16
/**
17
 * List of packed items, ordered by volume.
18
 *
19
 * @author Doug Wright
20
 */
21
class PackedItemList implements Countable, IteratorAggregate
22
{
23
    /**
24
     * List containing items.
25
     *
26
     * @var PackedItem[]
27
     */
28
    private $list = [];
29
30
    /**
31
     * Has this list already been sorted?
32
     *
33
     * @var bool
34
     */
35
    private $isSorted = false;
36
37
    /**
38
     * @param PackedItem $item
39
     */
40
    public function insert(PackedItem $item)
41
    {
42
        $this->list[] = $item;
43
    }
44
45
    /**
46
     * @return Traversable
47
     */
48 View Code Duplication
    public function getIterator(): Traversable
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
    {
50
        if (!$this->isSorted) {
51
            usort($this->list, [$this, 'compare']);
52
            $this->isSorted = true;
53
        }
54
55
        return new ArrayIterator($this->list);
56
    }
57
58
    /**
59
     * Number of items in list.
60
     *
61
     * @return int
62
     */
63
    public function count(): int
64
    {
65
        return count($this->list);
66
    }
67
68
    /**
69
     * Get copy of this list as a standard PHP array.
70
     *
71
     * @internal
72
     *
73
     * @return Item[]
74
     */
75
    public function asItemArray(): array
76
    {
77
        return array_map(function (PackedItem $packedItem) {
78
            return $packedItem->getItem();
79
        }, $this->list);
80
    }
81
82
    /**
83
     * @param PackedItem $itemA
84
     * @param PackedItem $itemB
85
     *
86
     * @return int
87
     */
88
    private function compare(PackedItem $itemA, PackedItem $itemB): int
89
    {
90
        $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth();
91
        $itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth();
92
93
        return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getItem()->getWeight() <=> $itemA->getItem()->getWeight());
94
    }
95
}
96