Completed
Push — pull-request/7612 ( 75b393 )
by Kamil
21:47
created

Taxon   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
wmc 36
c 0
b 0
f 0
lcom 3
cbo 4
dl 0
loc 313
rs 8.8

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __toString() 0 4 1
A getId() 0 4 1
A getCode() 0 4 1
A setCode() 0 4 1
A isRoot() 0 4 1
A getRoot() 0 4 1
A getParent() 0 4 1
A setParent() 0 7 2
A getParents() 0 14 3
A getChildren() 0 4 1
A hasChild() 0 4 1
A hasChildren() 0 4 1
A addChild() 0 10 3
A removeChild() 0 8 2
A getName() 0 4 1
A setName() 0 4 1
A getSlug() 0 4 1
A setSlug() 0 4 1
A getDescription() 0 4 1
A setDescription() 0 4 1
A getLeft() 0 4 1
A setLeft() 0 4 1
A getRight() 0 4 1
A setRight() 0 4 1
A getLevel() 0 4 1
A setLevel() 0 4 1
A getPosition() 0 4 1
A setPosition() 0 4 1
A createTranslation() 0 4 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
namespace Sylius\Component\Taxonomy\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Resource\Model\TranslatableTrait;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 * @author Gonzalo Vilaseca <[email protected]>
21
 */
22
class Taxon implements TaxonInterface
23
{
24
    use TranslatableTrait {
25
        __construct as private initializeTranslationsCollection;
26
    }
27
28
    /**
29
     * @var mixed
30
     */
31
    protected $id;
32
33
    /**
34
     * @var string
35
     */
36
    protected $code;
37
38
    /**
39
     * @var TaxonInterface
40
     */
41
    protected $root;
42
43
    /**
44
     * @var TaxonInterface
45
     */
46
    protected $parent;
47
48
    /**
49
     * @var Collection|TaxonInterface[]
50
     */
51
    protected $children;
52
53
    /**
54
     * @var int
55
     */
56
    protected $left;
57
58
    /**
59
     * @var int
60
     */
61
    protected $right;
62
63
    /**
64
     * @var int
65
     */
66
    protected $level;
67
68
    /**
69
     * @var int
70
     */
71
    protected $position;
72
73
    public function __construct()
74
    {
75
        $this->initializeTranslationsCollection();
76
77
        $this->children = new ArrayCollection();
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function __toString()
84
    {
85
        return (string) $this->getTranslation()->__toString();
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 __toString() does only exist in the following implementations of said interface: Sylius\Component\Payment...aymentMethodTranslation, 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...
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getId()
92
    {
93
        return $this->id;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getCode()
100
    {
101
        return $this->code;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function setCode($code)
108
    {
109
        $this->code = $code;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function isRoot()
116
    {
117
        return null === $this->parent;
118
    }
119
120
    /**
121
     * @return TaxonInterface
122
     */
123
    public function getRoot()
124
    {
125
        return $this->root;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getParent()
132
    {
133
        return $this->parent;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function setParent(TaxonInterface $parent = null)
140
    {
141
        $this->parent = $parent;
142
        if (null !== $parent) {
143
            $parent->addChild($this);
144
        }
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getParents()
151
    {
152
        if (null === $parent = $this->getParent()) {
153
            return [];
154
        }
155
156
        $parents = [$parent];
157
158
        while (null !== $parent->getParent()) {
159
            $parents[] = $parent = $parent->getParent();
160
        }
161
162
        return $parents;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getChildren()
169
    {
170
        return $this->children;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function hasChild(TaxonInterface $taxon)
177
    {
178
        return $this->children->contains($taxon);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function hasChildren()
185
    {
186
        return !$this->children->isEmpty();
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function addChild(TaxonInterface $taxon)
193
    {
194
        if (!$this->hasChild($taxon)) {
195
            $this->children->add($taxon);
196
        }
197
198
        if ($this !== $taxon->getParent()) {
199
            $taxon->setParent($this);
200
        }
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function removeChild(TaxonInterface $taxon)
207
    {
208
        if ($this->hasChild($taxon)) {
209
            $taxon->setParent(null);
210
211
            $this->children->removeElement($taxon);
212
        }
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function getName()
219
    {
220
        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...
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function setName($name)
227
    {
228
        $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...
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234
    public function getSlug()
235
    {
236
        return $this->getTranslation()->getSlug();
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 getSlug() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Product\Model\ProductTranslation, 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...
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function setSlug($slug = null)
243
    {
244
        $this->getTranslation()->setSlug($slug);
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 setSlug() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Product\Model\ProductTranslation, 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...
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250
    public function getDescription()
251
    {
252
        return $this->getTranslation()->getDescription();
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 getDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product\Model\ProductTranslation, 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...
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public function setDescription($description)
259
    {
260
        $this->getTranslation()->setDescription($description);
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 setDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation, Sylius\Component\Payment...aymentMethodTranslation, Sylius\Component\Product\Model\ProductTranslation, 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...
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266
    public function getLeft()
267
    {
268
        return $this->left;
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function setLeft($left)
275
    {
276
        $this->left = $left;
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282
    public function getRight()
283
    {
284
        return $this->right;
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290
    public function setRight($right)
291
    {
292
        $this->right = $right;
293
    }
294
295
    /**
296
     * {@inheritdoc}
297
     */
298
    public function getLevel()
299
    {
300
        return $this->level;
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306
    public function setLevel($level)
307
    {
308
        $this->level = $level;
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314
    public function getPosition()
315
    {
316
        return $this->position;
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322
    public function setPosition($position)
323
    {
324
        $this->position = $position;
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330
    protected function createTranslation()
331
    {
332
        return new TaxonTranslation();
333
    }
334
}
335