Completed
Pull Request — master (#73)
by Mikołaj
16:15
created

FrequentlyAskedQuestion::getCode()   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\CmsPlugin\Entity;
14
15
use Sylius\Component\Resource\Model\ToggleableTrait;
16
use Sylius\Component\Resource\Model\TranslatableTrait;
17
use Sylius\Component\Resource\Model\TranslationInterface;
18
19
/**
20
 * @author Mikołaj Król <[email protected]>
21
 */
22
class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface
23
{
24
    use SectionAssociationTrait;
25
    use ToggleableTrait,
26
        TranslatableTrait {
27
        __construct as private initializeTranslationsCollection;
28
    }
29
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var null|string
37
     */
38
    protected $code;
39
40
    /**
41
     * @var null|int
42
     */
43
    protected $position;
44
45
    public function __construct()
46
    {
47
        $this->initializeSectionsCollection();
48
        $this->initializeTranslationsCollection();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getId(): int
55
    {
56
        return $this->id;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getCode(): ?string
63
    {
64
        return $this->code;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function setCode(?string $code): void
71
    {
72
        $this->code = $code;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getPosition(): ?int
79
    {
80
        return $this->position;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function setPosition(?int $position): void
87
    {
88
        $this->position = $position;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getQuestion(): ?string
95
    {
96
        return $this->getFrequentlyAskedQuestionTranslation()->getQuestion();
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 getQuestion() does only exist in the following implementations of said interface: BitBag\CmsPlugin\Entity\...skedQuestionTranslation.

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...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setQuestion(?string $question): void
103
    {
104
        $this->getFrequentlyAskedQuestionTranslation()->setQuestion($question);
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 setQuestion() does only exist in the following implementations of said interface: BitBag\CmsPlugin\Entity\...skedQuestionTranslation.

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...
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getAnswer(): ?string
111
    {
112
        return $this->getFrequentlyAskedQuestionTranslation()->getAnswer();
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 getAnswer() does only exist in the following implementations of said interface: BitBag\CmsPlugin\Entity\...skedQuestionTranslation.

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...
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function setAnswer(?string $answer): void
119
    {
120
        $this->getFrequentlyAskedQuestionTranslation()->setAnswer($answer);
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 setAnswer() does only exist in the following implementations of said interface: BitBag\CmsPlugin\Entity\...skedQuestionTranslation.

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...
121
    }
122
123
    /**
124
     * @return TranslationInterface|FrequentlyAskedQuestionTranslationInterface
125
     */
126
    protected function getFrequentlyAskedQuestionTranslation(): TranslationInterface
127
    {
128
        return $this->getTranslation();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    protected function createTranslation(): TranslationInterface
135
    {
136
        return new FrequentlyAskedQuestionTranslation();
137
    }
138
}
139