Completed
Push — taxon-ancestor ( 0755ef )
by Kamil
119:50 queued 87:37
created

Taxon::getAncestors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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
declare(strict_types=1);
13
14
namespace Sylius\Component\Taxonomy\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sylius\Component\Resource\Model\TranslatableTrait;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 * @author Gonzalo Vilaseca <[email protected]>
23
 */
24
class Taxon implements TaxonInterface
25
{
26
    use TranslatableTrait {
27
        __construct as private initializeTranslationsCollection;
28
    }
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $id;
34
35
    /**
36
     * @var string|null
37
     */
38
    protected $code;
39
40
    /**
41
     * @var TaxonInterface|null
42
     */
43
    protected $root;
44
45
    /**
46
     * @var TaxonInterface|null
47
     */
48
    protected $parent;
49
50
    /**
51
     * @var Collection|TaxonInterface[]
52
     */
53
    protected $children;
54
55
    /**
56
     * @var int|null
57
     */
58
    protected $left;
59
60
    /**
61
     * @var int|null
62
     */
63
    protected $right;
64
65
    /**
66
     * @var int|null
67
     */
68
    protected $level;
69
70
    /**
71
     * @var int|null
72
     */
73
    protected $position;
74
75
    public function __construct()
76
    {
77
        $this->initializeTranslationsCollection();
78
79
        $this->children = new ArrayCollection();
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function __toString(): string
86
    {
87
        return (string) $this->getName();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getId()
94
    {
95
        return $this->id;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getCode(): ?string
102
    {
103
        return $this->code;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function setCode(?string $code): void
110
    {
111
        $this->code = $code;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function isRoot(): bool
118
    {
119
        return null === $this->parent;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getRoot(): ?TaxonInterface
126
    {
127
        return $this->root;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getParent(): ?TaxonInterface
134
    {
135
        return $this->parent;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function setParent(?TaxonInterface $parent): void
142
    {
143
        $this->parent = $parent;
144
        if (null !== $parent) {
145
            $parent->addChild($this);
146
        }
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getAncestors(): Collection
153
    {
154
        $ancestors = [];
155
156
        for ($ancestor = $this->getParent(); null !== $ancestor; $ancestor = $ancestor->getParent()) {
157
            $ancestors[] = $ancestor;
158
        }
159
160
        return new ArrayCollection($ancestors);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getChildren(): Collection
167
    {
168
        return $this->children;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function hasChild(TaxonInterface $taxon): bool
175
    {
176
        return $this->children->contains($taxon);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function hasChildren(): bool
183
    {
184
        return !$this->children->isEmpty();
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function addChild(TaxonInterface $taxon): void
191
    {
192
        if (!$this->hasChild($taxon)) {
193
            $this->children->add($taxon);
194
        }
195
196
        if ($this !== $taxon->getParent()) {
197
            $taxon->setParent($this);
198
        }
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function removeChild(TaxonInterface $taxon): void
205
    {
206
        if ($this->hasChild($taxon)) {
207
            $taxon->setParent(null);
208
209
            $this->children->removeElement($taxon);
210
        }
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function getName(): ?string
217
    {
218
        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...
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function setName(?string $name): void
225
    {
226
        $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...
227
    }
228
229
    /**
230
     * {@inheritdoc}
231
     */
232
    public function getSlug(): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
233
    {
234
        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...
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function setSlug(?string $slug): void
241
    {
242
        $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...
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function getDescription(): ?string
249
    {
250
        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...
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256
    public function setDescription(?string $description): void
257
    {
258
        $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...
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function getLeft(): ?int
265
    {
266
        return $this->left;
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function setLeft(?int $left): void
273
    {
274
        $this->left = $left;
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280
    public function getRight(): ?int
281
    {
282
        return $this->right;
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288
    public function setRight(?int $right): void
289
    {
290
        $this->right = $right;
291
    }
292
293
    /**
294
     * {@inheritdoc}
295
     */
296
    public function getLevel(): ?int
297
    {
298
        return $this->level;
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304
    public function setLevel(?int $level): void
305
    {
306
        $this->level = $level;
307
    }
308
309
    /**
310
     * {@inheritdoc}
311
     */
312
    public function getPosition(): ?int
313
    {
314
        return $this->position;
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    public function setPosition(?int $position): void
321
    {
322
        $this->position = $position;
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328
    protected function createTranslation(): TaxonTranslation
329
    {
330
        return new TaxonTranslation();
331
    }
332
}
333