Passed
Push — 3.x ( ec426a...33350c )
by Doug
01:45
created

InfalliblePacker::pack()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 18
ccs 0
cts 12
cp 0
crap 56
rs 8.8333
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
    public function __construct()
27
    {
28
        $this->unpackedItems = new ItemList();
29
        parent::__construct();
30
    }
31
32
    /**
33
     * Return the items that couldn't be packed.
34
     */
35
    public function getUnpackedItems(): ItemList
36
    {
37
        return $this->unpackedItems;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function pack(): PackedBoxList
44
    {
45
        foreach ($this->items as $item) {
46
            foreach ($this->boxes as $box) {
47
                if ($item->getWeight() <= ($box->getMaxWeight() - $box->getEmptyWeight()) && (new OrientatedItemFactory($box))->hasPossibleOrientationsInEmptyBox($item)) {
48
                    continue 2;
49
                }
50
            }
51
            $this->unpackedItems->insert($item);
52
            $this->items->remove($item);
53
        }
54
55
        while (true) {
56
            try {
57
                return parent::pack();
58
            } catch (NoBoxesAvailableException $e) {
59
                $this->unpackedItems->insert($e->getItem());
60
                $this->items->remove($e->getItem());
61
            }
62
        }
0 ignored issues
show
Bug Best Practice introduced by
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...
63
    }
64
}
65