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 | declare(strict_types=1); |
||
8 | namespace DVDoug\BoxPacker; |
||
9 | |||
10 | use ArrayIterator, Countable, IteratorAggregate, Traversable; |
||
11 | |||
12 | /** |
||
13 | * List of packed items, ordered by volume |
||
14 | * @author Doug Wright |
||
15 | * @package BoxPacker |
||
16 | */ |
||
17 | class PackedItemList implements Countable, IteratorAggregate |
||
18 | { |
||
19 | /** |
||
20 | * List containing items |
||
21 | * @var PackedItem[] |
||
22 | */ |
||
23 | private $list = []; |
||
24 | |||
25 | /** |
||
26 | * Has this list already been sorted? |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $isSorted = false; |
||
30 | |||
31 | /** |
||
32 | * @param PackedItem $item |
||
33 | */ |
||
34 | 38 | public function insert(PackedItem $item) |
|
35 | { |
||
36 | 38 | $this->list[] = $item; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return Traversable |
||
41 | */ |
||
42 | 30 | View Code Duplication | public function getIterator(): Traversable |
0 ignored issues
–
show
|
|||
43 | { |
||
44 | 30 | if (!$this->isSorted) { |
|
45 | 30 | usort($this->list, [$this, 'compare']); |
|
46 | 30 | $this->isSorted = true; |
|
47 | } |
||
48 | 30 | return new ArrayIterator($this->list); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Number of items in list |
||
53 | * @return int |
||
54 | */ |
||
55 | 34 | public function count(): int |
|
56 | { |
||
57 | 34 | return count($this->list); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get copy of this list as a standard PHP array |
||
62 | * @internal |
||
63 | * @return Item[] |
||
64 | */ |
||
65 | public function asItemArray(): array |
||
66 | { |
||
67 | 4 | return array_map(function(PackedItem $packedItem) { |
|
68 | 4 | return $packedItem->getItem(); |
|
69 | 4 | }, $this->list); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param PackedItem $itemA |
||
74 | * @param PackedItem $itemB |
||
75 | * |
||
76 | * @return int |
||
77 | */ |
||
78 | 24 | private function compare(PackedItem $itemA, PackedItem $itemB): int |
|
79 | { |
||
80 | 24 | $itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth(); |
|
81 | 24 | $itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth(); |
|
82 | 24 | return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getItem()->getWeight() <=> $itemA->getItem()->getWeight()); |
|
83 | } |
||
84 | } |
||
85 |
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.