Checks if the types of returned expressions are compatible with the hinted types.
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
|
|||
58 | } |
||
59 | } |
||
60 |
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: