Completed
Push — translation-class ( c4acc3 )
by Kamil
18:30
created

ProductOptionValue::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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 Sylius\Component\Resource\Model\TranslatableTrait;
15
use Sylius\Component\Resource\Model\TranslationInterface;
16
17
/**
18
 * @author Paweł Jędrzejewski <[email protected]>
19
 */
20
class ProductOptionValue implements ProductOptionValueInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addTranslation, getTranslation, getTranslations, hasTranslation, removeTranslation, setCurrentLocale, setFallbackLocale
Loading history...
21
{
22
    use TranslatableTrait {
23
        __construct as private initializeTranslationCollection;
24
    }
25
26
    /**
27
     * @var mixed
28
     */
29
    protected $id;
30
31
    /**
32
     * @var string
33
     */
34
    protected $code;
35
36
    /**
37
     * @var ProductOptionInterface
38
     */
39
    protected $option;
40
41
    public function __construct()
42
    {
43
        $this->initializeTranslationCollection();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function __toString()
50
    {
51
        return $this->getValue();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getId()
58
    {
59
        return $this->id;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getCode()
66
    {
67
        return $this->code;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function setCode($code)
74
    {
75
        $this->code = $code;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getOption()
82
    {
83
        return $this->option;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function setOption(ProductOptionInterface $option = null)
90
    {
91
        $this->option = $option;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getValue()
98
    {
99
        return $this->getTranslation()->getValue();
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 getValue() does only exist in the following implementations of said interface: Sylius\Component\Product...tOptionValueTranslation.

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...
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function setValue($value)
106
    {
107
        $this->getTranslation()->setValue($value);
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 setValue() does only exist in the following implementations of said interface: Sylius\Component\Product...tOptionValueTranslation.

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...
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getOptionCode()
114
    {
115
        if (null === $this->option) {
116
            throw new \BadMethodCallException('The option have not been created yet so you cannot access proxy methods.');
117
        }
118
119
        return $this->option->getCode();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getName()
126
    {
127
        if (null === $this->option) {
128
            throw new \BadMethodCallException('The option have not been created yet so you cannot access proxy methods.');
129
        }
130
131
        return $this->option->getName();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    protected function createTranslation()
138
    {
139
        return new ProductOptionValueTranslation();
140
    }
141
}
142