Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
24 | class Rate implements RateInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | * |
||
29 | * @Assert\NotBlank() |
||
30 | */ |
||
31 | private $rate; |
||
32 | |||
33 | /** |
||
34 | * @var \DateTime |
||
35 | * |
||
36 | * @Assert\NotBlank() |
||
37 | * @Assert\DateTime() |
||
38 | */ |
||
39 | private $date; |
||
40 | |||
41 | /** |
||
42 | * @var float |
||
43 | * |
||
44 | * @Assert\NotBlank() |
||
45 | * @Assert\Type(type="float") |
||
46 | */ |
||
47 | private $value; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | * |
||
52 | * @Assert\NotBlank() |
||
53 | * @ExchangeRateAssert\BaseCurrency() |
||
54 | */ |
||
55 | private $baseCurrencyCode; |
||
56 | |||
57 | /** |
||
58 | * Rate constructor. |
||
59 | * |
||
60 | * @param null $rate |
||
61 | * @param \DateTime|null $date |
||
62 | * @param null $value |
||
63 | */ |
||
64 | 11 | public function __construct($rate = null, \DateTime $date = null, $value = null, $baseCurrencyCode = null) |
|
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | 11 | public function getRate() |
|
79 | |||
80 | /** |
||
81 | * @param string $rate |
||
82 | * @return Rate |
||
83 | */ |
||
84 | 8 | public function setRate($rate) |
|
89 | |||
90 | /** |
||
91 | * @return \DateTime |
||
92 | */ |
||
93 | 9 | public function getDate() |
|
97 | |||
98 | /** |
||
99 | * @param \DateTime $date |
||
100 | * @return Rate |
||
101 | */ |
||
102 | 8 | public function setDate(\DateTime $date) |
|
107 | |||
108 | /** |
||
109 | * @return float |
||
110 | */ |
||
111 | 9 | public function getValue() |
|
115 | |||
116 | /** |
||
117 | * @param float $value |
||
118 | * @return Rate |
||
119 | */ |
||
120 | 11 | public function setValue($value) |
|
121 | { |
||
122 | 11 | $this->value = (is_numeric($value)) ? (float) $value : $value; |
|
123 | 11 | return $this; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | 1 | public function getBaseCurrencyCode() |
|
130 | { |
||
131 | 1 | return $this->baseCurrencyCode; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $baseCurrencyCode |
||
136 | * @return Rate |
||
137 | */ |
||
138 | 4 | public function setBaseCurrencyCode($baseCurrencyCode) |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 9 | View Code Duplication | public function getSourceName() |
|
|||
148 | { |
||
149 | 9 | if (false !== strpos($this->getRate(), '.')) { |
|
150 | 8 | return explode('.', $this->getRate())[0]; |
|
151 | } |
||
152 | |||
153 | 1 | return null; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 9 | View Code Duplication | public function getCurrencyCode() |
160 | { |
||
161 | 9 | if (false !== strpos($this->getRate(), '.')) { |
|
162 | 8 | return explode('.', $this->getRate())[2]; |
|
163 | } |
||
164 | |||
165 | 2 | return null; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 9 | View Code Duplication | public function getRateType() |
172 | { |
||
173 | 9 | if (false !== strpos($this->getRate(), '.')) { |
|
174 | 8 | return explode('.', $this->getRate())[1]; |
|
175 | } |
||
176 | |||
177 | 1 | return null; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 1 | public function getCreatedAt() |
|
184 | { |
||
185 | 1 | return null; // unknown |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 1 | public function getModifiedAt() |
|
192 | { |
||
193 | 1 | return null; // unknown |
|
194 | } |
||
195 | |||
196 | |||
197 | /** |
||
198 | * Build \RunOpenCode\ExchangeRate\Model\Rate from DTO object. |
||
199 | * |
||
200 | * @param ExchangeRate|null $exchangeRate Initial rate object that can be used for data population |
||
201 | * @return ExchangeRate |
||
202 | */ |
||
203 | 7 | public function toRate(ExchangeRate $exchangeRate = null) |
|
218 | |||
219 | /** |
||
220 | * Create DTO object from RateInterface. |
||
221 | * |
||
222 | * @param RateInterface $exchangeRate |
||
223 | * @return Rate |
||
224 | */ |
||
225 | 4 | public static function fromRateInterface(RateInterface $exchangeRate) |
|
237 | } |
||
238 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.