Completed
Push — master ( fd86aa...d435ff )
by Paweł
18s
created

src/Sylius/Component/Core/Model/OrderItemUnit.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Sylius\Component\Inventory\Model\InventoryUnitInterface;
15
use Sylius\Component\Order\Model\OrderItemUnit as BaseOrderItemUnit;
16
use Sylius\Component\Shipping\Model\ShipmentInterface as BaseShipmentInterface;
17
18
/**
19
 * @author Mateusz Zalewski <[email protected]>
20
 */
21
class OrderItemUnit extends BaseOrderItemUnit implements OrderItemUnitInterface
22
{
23
    /**
24
     * @var string InventoryUnitInterface::STATE_*
25
     */
26
    protected $inventoryState = InventoryUnitInterface::STATE_CHECKOUT;
27
28
    /**
29
     * @var BaseShipmentInterface
30
     */
31
    protected $shipment;
32
33
    /**
34
     * @var string BaseShipmentInterface::STATE_*
35
     */
36
    protected $shippingState = BaseShipmentInterface::STATE_CHECKOUT;
37
38
    /**
39
     * @var \DateTime
40
     */
41
    protected $createdAt;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    protected $updatedAt;
47
48
    /**
49
     * @param OrderItemInterface $orderItem
50
     */
51
    public function __construct(OrderItemInterface $orderItem)
52
    {
53
        parent::__construct($orderItem);
54
55
        $this->createdAt = new \DateTime();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getStockable()
62
    {
63
        return $this->orderItem->getVariant();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderItemInterface as the method getVariant() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\OrderItem.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getInventoryName()
70
    {
71
        return $this->orderItem->getVariant()->getInventoryName();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getInventoryState()
78
    {
79
        return $this->inventoryState;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function setInventoryState($state)
86
    {
87
        $this->inventoryState = $state;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function isSold()
94
    {
95
        return InventoryUnitInterface::STATE_SOLD === $this->inventoryState;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function isBackordered()
102
    {
103
        return InventoryUnitInterface::STATE_BACKORDERED === $this->inventoryState;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getShipment()
110
    {
111
        return $this->shipment;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function setShipment(BaseShipmentInterface $shipment = null)
118
    {
119
        $this->shipment = $shipment;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getShippable()
126
    {
127
        return $this->orderItem->getVariant();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\Model\OrderItemInterface as the method getVariant() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\OrderItem.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getShippingState()
134
    {
135
        return $this->shippingState;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function setShippingState($state)
142
    {
143
        $this->shippingState = $state;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getCreatedAt()
150
    {
151
        return $this->createdAt;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function setCreatedAt(\DateTime $createdAt)
158
    {
159
        $this->createdAt = $createdAt;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getUpdatedAt()
166
    {
167
        return $this->updatedAt;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function setUpdatedAt(\DateTime $updatedAt)
174
    {
175
        $this->updatedAt = $updatedAt;
176
    }
177
}
178