Passed
Push — 3.x ( 539084...e58d28 )
by Doug
02:52
created

InfalliblePacker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A pack() 0 11 3
A getUnpackedItems() 0 3 1
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 7
    public function __construct()
27
    {
28 7
        $this->unpackedItems = new ItemList();
29 7
        parent::__construct();
30 7
    }
31
32
    /**
33
     * Return the items that couldn't be packed.
34
     */
35 2
    public function getUnpackedItems(): ItemList
36
    {
37 2
        return $this->unpackedItems;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 7
    public function pack(): PackedBoxList
44
    {
45 7
        $itemList = clone $this->items;
46
47 7
        while (true) {
48
            try {
49 7
                return parent::pack();
50 2
            } catch (ItemTooLargeException $e) {
51 2
                $this->unpackedItems->insert($e->getItem());
52 2
                $itemList->remove($e->getItem());
53 2
                $this->setItems($itemList);
54
            }
55
        }
56
    }
57
}
58