PartialShipItems::findById()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\DTO;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
17
final class PartialShipItems
18
{
19
    /** @var Collection */
20
    private $partialShipItems;
21
22
    public function __construct()
23
    {
24
        $this->partialShipItems = new ArrayCollection();
25
    }
26
27
    public function setPartialShipItem(PartialShipItem $partialShipItem): void
28
    {
29
        $partialItem = $this->findPartialShipItemWhereItem($partialShipItem);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $partialItem is correct as $this->findPartialShipIt...eItem($partialShipItem) targeting BitBag\SyliusMolliePlugi...tialShipItemWhereItem() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
31
        if (null !== $partialItem) {
0 ignored issues
show
introduced by
The condition null !== $partialItem is always false.
Loading history...
32
            $partialItem->setQuantity($partialItem->getQuantity() + 1);
33
34
            return;
35
        }
36
37
        $this->partialShipItems->add($partialShipItem);
38
    }
39
40
    public function getPartialShipItems(): Collection
41
    {
42
        return $this->partialShipItems;
43
    }
44
45
    public function getArrayFromObject(): array
46
    {
47
        $data = [];
48
        /** @var PartialShipItem $item */
49
        foreach ($this->getPartialShipItems() as $item) {
50
            $data[] = [
51
                'id' => $item->getLineId(),
52
                'quantity' => $item->getQuantity(),
53
            ];
54
        }
55
56
        return $data;
57
    }
58
59
    public function findPartialShipItemWhereItem(PartialShipItem $partialShipItem): ?PartialShipItem
60
    {
61
        /** @var PartialShipItem $item */
62
        foreach ($this->getPartialShipItems() as $item) {
63
            if ($item->getId() === $partialShipItem->getId()) {
64
                return $item;
65
            }
66
        }
67
68
        return null;
69
    }
70
71
    public function findById(int $id): ?PartialShipItem
72
    {
73
        /** @var PartialShipItem $item */
74
        foreach ($this->getPartialShipItems() as $item) {
75
            if ($item->getId() === $id) {
76
                return $item;
77
            }
78
        }
79
80
        return null;
81
    }
82
}
83