Completed
Pull Request — master (#95)
by
unknown
35:39
created

Section::createTranslation()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file was created by the developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Entity;
14
15
use Sylius\Component\Resource\Model\TranslatableTrait;
16
use Sylius\Component\Resource\Model\TranslationInterface;
17
18
/**
19
 * @author Patryk Drapik <[email protected]>
20
 */
21
class Section implements SectionInterface
22
{
23
    use TranslatableTrait {
24
        __construct as private initializeTranslationsCollection;
25
    }
26
27
    /**
28
     * @var null|int
29
     */
30
    protected $id;
31
32
    /**
33
     * @var null|string
34
     */
35
    protected $code;
36
37
    public function __construct()
38
    {
39
        $this->initializeTranslationsCollection();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getId(): ?int
46
    {
47
        return $this->id;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getCode(): ?string
54
    {
55
        return $this->code;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function setCode(?string $code): void
62
    {
63
        $this->code = $code;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getName(): ?string
70
    {
71
        return $this->getSectionTranslation()->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: BitBag\SyliusCmsPlugin\Entity\BlockTranslation, BitBag\SyliusCmsPlugin\Entity\PageTranslation, BitBag\SyliusCmsPlugin\Entity\SectionTranslation, 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...
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function setName(?string $name): void
78
    {
79
        $this->getSectionTranslation()->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: BitBag\SyliusCmsPlugin\Entity\BlockTranslation, BitBag\SyliusCmsPlugin\Entity\PageTranslation, BitBag\SyliusCmsPlugin\Entity\SectionTranslation, 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...
80
    }
81
82
    /**
83
     * @return TranslationInterface|SectionTranslationInterface
84
     */
85
    protected function getSectionTranslation(): TranslationInterface
86
    {
87
        return $this->getTranslation();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function createTranslation(): TranslationInterface
94
    {
95
        return new SectionTranslation();
96
    }
97
}