Completed
Push — master ( 4bc269...bc2ce1 )
by Paweł
35:58 queued 24:11
created

Customer::getDefaultAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
namespace Sylius\Component\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Customer\Model\Customer as BaseCustomer;
17
use Sylius\Component\User\Model\UserInterface as BaseUserInterface;
18
19
/**
20
 * @author Michał Marcinkowski <[email protected]>
21
 */
22
class Customer extends BaseCustomer implements CustomerInterface, ProductReviewerInterface
23
{
24
    /**
25
     * @var Collection|OrderInterface[]
26
     */
27
    protected $orders;
28
29
    /**
30
     * @var AddressInterface
31
     */
32
    protected $defaultAddress;
33
34
    /**
35
     * @var Collection|AddressInterface[]
36
     */
37
    protected $addresses;
38
39
    /**
40
     * @var ShopUserInterface
41
     */
42
    protected $user;
43
44
    public function __construct()
45
    {
46
        parent::__construct();
47
48
        $this->orders = new ArrayCollection();
49
        $this->addresses = new ArrayCollection();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getOrders()
56
    {
57
        return $this->orders;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getDefaultAddress()
64
    {
65
        return $this->defaultAddress;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function setDefaultAddress(AddressInterface $defaultAddress = null)
72
    {
73
        $this->defaultAddress = $defaultAddress;
74
75
        if (null !== $defaultAddress) {
76
            $this->addAddress($defaultAddress);
77
        }
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function addAddress(AddressInterface $address)
84
    {
85
        if (!$this->hasAddress($address)) {
86
            $this->addresses[] = $address;
87
            $address->setCustomer($this);
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function removeAddress(AddressInterface $address)
95
    {
96
        $this->addresses->removeElement($address);
97
        $address->setCustomer(null);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function hasAddress(AddressInterface $address)
104
    {
105
        return $this->addresses->contains($address);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getAddresses()
112
    {
113
        return $this->addresses;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getUser()
120
    {
121
        return $this->user;
122
    }
123
    
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function setUser(BaseUserInterface $user = null)
128
    {
129
        if ($this->user !== $user) {
130
            $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...
131
            $this->assignCustomer($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by parameter $user on line 127 can also be of type object<Sylius\Component\User\Model\UserInterface>; however, Sylius\Component\Core\Mo...tomer::assignCustomer() does only seem to accept null|object<Sylius\Compo...odel\ShopUserInterface>, 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...
132
        }
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function hasUser()
139
    {
140
        return null !== $this->user;
141
    }
142
143
    /**
144
     * @param ShopUserInterface|null $user
145
     */
146
    protected function assignCustomer(ShopUserInterface $user = null)
147
    {
148
        if (null !== $user) {
149
            $user->setCustomer($this);
150
        }
151
    }
152
}
153