Completed
Push — pull-request/8600 ( 367a7c )
by Kamil
56:28 queued 34:06
created

Customer::addAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sylius\Component\Customer\Model\Customer as BaseCustomer;
19
use Sylius\Component\User\Model\UserInterface as BaseUserInterface;
20
21
/**
22
 * @author Michał Marcinkowski <[email protected]>
23
 */
24
class Customer extends BaseCustomer implements CustomerInterface
25
{
26
    /**
27
     * @var Collection|OrderInterface[]
28
     */
29
    protected $orders;
30
31
    /**
32
     * @var AddressInterface
33
     */
34
    protected $defaultAddress;
35
36
    /**
37
     * @var Collection|AddressInterface[]
38
     */
39
    protected $addresses;
40
41
    /**
42
     * @var ShopUserInterface
43
     */
44
    protected $user;
45
46
    public function __construct()
47
    {
48
        parent::__construct();
49
50
        $this->orders = new ArrayCollection();
51
        $this->addresses = new ArrayCollection();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function setEmail(?string $email): void
58
    {
59
        parent::setEmail($email);
60
61
        if ($this->hasUser() && null != $email) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $email of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
62
            $this->getUser()->setUsername($email);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getOrders(): Collection
70
    {
71
        return $this->orders;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getDefaultAddress(): ?AddressInterface
78
    {
79
        return $this->defaultAddress;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function setDefaultAddress(?AddressInterface $defaultAddress): void
86
    {
87
        $this->defaultAddress = $defaultAddress;
88
89
        if (null !== $defaultAddress) {
90
            $this->addAddress($defaultAddress);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function addAddress(AddressInterface $address): void
98
    {
99
        if (!$this->hasAddress($address)) {
100
            $this->addresses[] = $address;
101
            $address->setCustomer($this);
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function removeAddress(AddressInterface $address): void
109
    {
110
        $this->addresses->removeElement($address);
111
        $address->setCustomer(null);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function hasAddress(AddressInterface $address): bool
118
    {
119
        return $this->addresses->contains($address);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getAddresses(): Collection
126
    {
127
        return $this->addresses;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getUser(): ?BaseUserInterface
134
    {
135
        return $this->user;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function setUser(?BaseUserInterface $user): void
142
    {
143
        if ($this->user !== $user) {
144
            $this->user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user can also be of type object<Sylius\Component\User\Model\UserInterface>. However, the property $user is declared as type object<Sylius\Component\...odel\ShopUserInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
145
            $this->assignCustomer($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by parameter $user on line 141 can also be of type object<Sylius\Component\User\Model\UserInterface>; however, Sylius\Component\Core\Mo...tomer::assignCustomer() does only seem to accept object<Sylius\Component\...ShopUserInterface>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
146
        }
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function hasUser(): bool
153
    {
154
        return null !== $this->user;
155
    }
156
157
    /**
158
     * @param ShopUserInterface|null $user
159
     */
160
    protected function assignCustomer(?ShopUserInterface $user): void
161
    {
162
        if (null !== $user) {
163
            $user->setCustomer($this);
164
        }
165
    }
166
}
167