Completed
Push — taxon-parent-form ( 63b2c2 )
by Kamil
21:22
created

ProductVariant::getDescriptor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
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\Product\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Resource\Model\TimestampableTrait;
17
use Sylius\Component\Resource\Model\TranslatableTrait;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class ProductVariant implements ProductVariantInterface
23
{
24
    use TimestampableTrait;
25
    use TranslatableTrait {
26
        __construct as private initializeTranslationsCollection;
27
    }
28
29
    /**
30
     * @var mixed
31
     */
32
    protected $id;
33
34
    /**
35
     * @var string
36
     */
37
    protected $code;
38
39
    /**
40
     * @var ProductInterface
41
     */
42
    protected $product;
43
44
    /**
45
     * @var Collection|ProductOptionValueInterface[]
46
     */
47
    protected $optionValues;
48
49
    /**
50
     * @var \DateTime
51
     */
52
    protected $availableOn;
53
54
    /**
55
     * @var \DateTime
56
     */
57
    protected $availableUntil;
58
59
    /**
60
     * @var int
61
     */
62
    protected $position;
63
64
    public function __construct()
65
    {
66
        $this->initializeTranslationsCollection();
67
        $this->optionValues = new ArrayCollection();
68
69
        $this->createdAt = new \DateTime();
70
        $this->availableOn = new \DateTime();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getId()
77
    {
78
        return $this->id;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getCode()
85
    {
86
        return $this->code;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function setCode($code)
93
    {
94
        $this->code = $code;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getName()
101
    {
102
        return $this->getTranslation()->getName();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getName() does only exist in the following implementations of said interface: Sylius\Component\Attribu...el\AttributeTranslation, Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product...ociationTypeTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Product...oductVariantTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

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...
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function setName($name)
109
    {
110
        $this->getTranslation()->setName($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setName() does only exist in the following implementations of said interface: Sylius\Component\Attribu...el\AttributeTranslation, Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product...ociationTypeTranslation, Sylius\Component\Product...uctAttributeTranslation, Sylius\Component\Product...roductOptionTranslation, Sylius\Component\Product\Model\ProductTranslation, Sylius\Component\Product...oductVariantTranslation, Sylius\Component\Shippin...ippingMethodTranslation, Sylius\Component\Taxonomy\Model\TaxonTranslation.

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...
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getDescriptor()
117
    {
118
        $name = empty($this->getName()) ? $this->getProduct()->getName() : $this->getName();
119
120
        return trim(sprintf('%s (%s)', $name, $this->code));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getOptionValues()
127
    {
128
        return $this->optionValues;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function addOptionValue(ProductOptionValueInterface $optionValue)
135
    {
136
        if (!$this->hasOptionValue($optionValue)) {
137
            $this->optionValues->add($optionValue);
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function removeOptionValue(ProductOptionValueInterface $optionValue)
145
    {
146
        if ($this->hasOptionValue($optionValue)) {
147
            $this->optionValues->removeElement($optionValue);
148
        }
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function hasOptionValue(ProductOptionValueInterface $optionValue)
155
    {
156
        return $this->optionValues->contains($optionValue);
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getProduct()
163
    {
164
        return $this->product;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function setProduct(ProductInterface $product = null)
171
    {
172
        $this->product = $product;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function isAvailable()
179
    {
180
        return (new DateRange($this->availableOn, $this->availableUntil))->isInRange();
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function getAvailableOn()
187
    {
188
        return $this->availableOn;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function setAvailableOn(\DateTime $availableOn = null)
195
    {
196
        $this->availableOn = $availableOn;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function getAvailableUntil()
203
    {
204
        return $this->availableUntil;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function setAvailableUntil(\DateTime $availableUntil = null)
211
    {
212
        $this->availableUntil = $availableUntil;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function getPosition()
219
    {
220
        return $this->position;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function setPosition($position)
227
    {
228
        $this->position = $position;
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    protected function createTranslation()
235
    {
236
        return new ProductVariantTranslation();
237
    }
238
}
239