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 |
||
14 | 1 | class ActiveProfile implements IProfile |
|
15 | { |
||
16 | 1 | use SmartObject; |
|
17 | |||
18 | /** @var \SixtyEightPublishers\Application\Environment\IProfile */ |
||
19 | private $profile; |
||
20 | |||
21 | /** @var \SixtyEightPublishers\Application\Environment\IProfileStorage */ |
||
22 | private $profileStorage; |
||
23 | |||
24 | /** @var null|string */ |
||
25 | private $country; |
||
26 | |||
27 | /** @var null|string */ |
||
28 | private $language; |
||
29 | |||
30 | /** @var null|string */ |
||
31 | private $currency; |
||
32 | |||
33 | /** |
||
34 | * @param \SixtyEightPublishers\Application\Environment\IProfile $profile |
||
35 | * @param \SixtyEightPublishers\Application\Environment\IProfileStorage $profileStorage |
||
36 | */ |
||
37 | public function __construct(IProfile $profile, IProfileStorage $profileStorage) |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getCountry() |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getLanguage() |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getCurrency() |
||
69 | |||
70 | /** |
||
71 | * @param string $country |
||
72 | * @param bool $persist |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | View Code Duplication | public function changeCountry($country, $persist = TRUE) |
|
90 | |||
91 | /** |
||
92 | * @param string $language |
||
93 | * @param bool $persist |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | View Code Duplication | public function changeLanguage($language, $persist = TRUE) |
|
111 | |||
112 | /** |
||
113 | * @param string $currency |
||
114 | * @param bool $persist |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | View Code Duplication | public function changeCurrency($currency, $persist = TRUE) |
|
132 | |||
133 | /***************** interface \SixtyEightPublishers\Application\Environment\IProfile *****************/ |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function getName() : string |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getCountries() : array |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getLanguages() : array |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function getCurrencies() : array |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function getDomains() : array |
||
174 | } |
||
175 |
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.