Passed
Push — infallible_packer ( e29505...d1091e )
by Doug
01:54
created

InfalliblePacker::pack()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.4285
c 0
b 0
f 0
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
/**
12
 * A version of the packer that swallows internal exceptions.
13
 *
14
 * @author Doug Wright
15
 */
16
class InfalliblePacker extends Packer
17
{
18
    /**
19
     * @var ItemList
20
     */
21
    protected $unpackedItems;
22
23
    /**
24
     * InfalliblePacker constructor.
25
     */
26 6
    public function __construct()
27
    {
28 6
        $this->unpackedItems = new ItemList();
29 6
        parent::__construct();
30 6
    }
31
32
    /**
33
     * Return the items that couldn't be packed
34
     *
35
     * @return ItemList
36
     */
37 1
    public function getUnpackedItems(): ItemList
38
    {
39 1
        return $this->unpackedItems;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 6
    public function pack(): PackedBoxList
46
    {
47 6
        $itemList = clone $this->items;
48
49 6
        $packedBoxList = new PackedBoxList();
50
        do {
51
            try {
52 6
                return parent::pack();
53
            }
54 1
            catch (ItemTooLargeException $e) {
55 1
                $this->unpackedItems->insert($e->getItem());
56 1
                $itemList->remove($e->getItem());
57 1
                $this->setItems($itemList);
0 ignored issues
show
Bug introduced by
$itemList of type DVDoug\BoxPacker\ItemList is incompatible with the type iterable expected by parameter $items of DVDoug\BoxPacker\Packer::setItems(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                $this->setItems(/** @scrutinizer ignore-type */ $itemList);
Loading history...
58
            }
59 1
        } while (true);
60
        return $packedBoxList;
61
    }
62
}
63