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 |
||
62 | class Context implements \ArrayAccess |
||
63 | { |
||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $graceful = false; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | private $languages = []; |
||
73 | |||
74 | /** |
||
75 | * @var CurrencyFormatter |
||
76 | */ |
||
77 | private $currencyFormatter; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | private $locale; |
||
83 | |||
84 | /** |
||
85 | * @var LoggerInterface |
||
86 | */ |
||
87 | private $logger; |
||
88 | |||
89 | const GRACEFUL = 'graceful'; |
||
90 | const LANGUAGES = 'languages'; |
||
91 | const CURRENCY_FORMATTER = 'currencyFormatter'; |
||
92 | const LOCALE = 'locale'; |
||
93 | const LOGGER = 'logger'; |
||
94 | |||
95 | 258 | public function __construct() |
|
106 | |||
107 | /** |
||
108 | * @return boolean |
||
109 | */ |
||
110 | 84 | public function isGraceful() |
|
114 | |||
115 | /** |
||
116 | * @param boolean $graceful |
||
117 | * @return Context |
||
118 | */ |
||
119 | 149 | public function setGraceful($graceful) |
|
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | 125 | public function getLanguages() |
|
132 | |||
133 | /** |
||
134 | * @param array $languages |
||
135 | * @return Context |
||
136 | */ |
||
137 | 166 | public function setLanguages(array $languages) |
|
142 | |||
143 | /** |
||
144 | * @return CurrencyFormatter |
||
145 | */ |
||
146 | 5 | public function getCurrencyFormatter() |
|
150 | |||
151 | /** |
||
152 | * @param CurrencyFormatter $currencyFormatter |
||
153 | * @return Context |
||
154 | */ |
||
155 | 1 | public function setCurrencyFormatter($currencyFormatter) |
|
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | 7 | public function getLocale() |
|
168 | |||
169 | /** |
||
170 | * @param string $locale |
||
171 | * @return Context |
||
172 | */ |
||
173 | 46 | public function setLocale($locale) |
|
178 | |||
179 | /** |
||
180 | * @return LoggerInterface |
||
181 | */ |
||
182 | 6 | public function getLogger() |
|
186 | |||
187 | /** |
||
188 | * @param LoggerInterface $logger |
||
189 | * @return Context |
||
190 | */ |
||
191 | 2 | public function setLogger($logger) |
|
196 | |||
197 | 95 | public static function of() |
|
201 | |||
202 | /** |
||
203 | * @inheritDoc |
||
204 | */ |
||
205 | 1 | public function offsetExists($offset) |
|
206 | { |
||
207 | 1 | return isset($this->$offset); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | 1 | View Code Duplication | public function offsetGet($offset) |
|
|||
214 | { |
||
215 | 1 | if ($this->offsetExists($offset)) { |
|
216 | 1 | $method = 'get'.ucfirst($offset); |
|
217 | |||
218 | 1 | return $this->$method(); |
|
219 | } |
||
220 | return null; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @inheritDoc |
||
225 | */ |
||
226 | 1 | public function offsetSet($offset, $value) |
|
227 | { |
||
228 | 1 | if (property_exists($this, $offset)) { |
|
229 | 1 | $method = 'set'.ucfirst($offset); |
|
230 | |||
231 | 1 | $this->$method($value); |
|
232 | } |
||
233 | 1 | } |
|
234 | |||
235 | /** |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | View Code Duplication | public function offsetUnset($offset) |
|
246 | } |
||
247 |
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.