Passed
Push — master ( ede80c...25a1dd )
by Doug
02:13
created

src/InfalliblePacker.php (1 issue)

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
     * @return ItemList
36
     */
37 2
    public function getUnpackedItems(): ItemList
38
    {
39 2
        return $this->unpackedItems;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 7
    public function pack(): PackedBoxList
46
    {
47 7
        $itemList = clone $this->items;
48
49 7
        while(true) {
50
            try {
51 7
                return parent::pack();
52 2
            } catch (ItemTooLargeException $e) {
53 2
                $this->unpackedItems->insert($e->getItem());
54 2
                $itemList->remove($e->getItem());
55 2
                $this->setItems($itemList);
56
            }
57
        }
1 ignored issue
show
Bug Best Practice introduced by Doug Wright
In this branch, the function will implicitly return null which is incompatible with the type-hinted return DVDoug\BoxPacker\PackedBoxList. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
58
    }
59
}
60