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 |
||
35 | class Translator |
||
36 | { |
||
37 | /** |
||
38 | * None error. |
||
39 | */ |
||
40 | const ERROR_NONE = 0; |
||
41 | /** |
||
42 | * File does not exist. |
||
43 | */ |
||
44 | const ERROR_DOES_NOT_EXIST = 1; |
||
45 | /** |
||
46 | * File has bad magic number. |
||
47 | */ |
||
48 | const ERROR_BAD_MAGIC = 2; |
||
49 | /** |
||
50 | * Error while reading file, probably too short. |
||
51 | */ |
||
52 | const ERROR_READING = 3; |
||
53 | |||
54 | /** |
||
55 | * Big endian mo file magic bytes. |
||
56 | */ |
||
57 | const MAGIC_BE = "\x95\x04\x12\xde"; |
||
58 | /** |
||
59 | * Little endian mo file magic bytes. |
||
60 | */ |
||
61 | const MAGIC_LE = "\xde\x12\x04\x95"; |
||
62 | |||
63 | /** |
||
64 | * Parse error code (0 if no error). |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | public $error = self::ERROR_NONE; |
||
69 | |||
70 | /** |
||
71 | * Cache header field for plural forms. |
||
72 | * |
||
73 | * @var string|null |
||
74 | */ |
||
75 | private $pluralequation = null; |
||
76 | /** |
||
77 | * @var ExpressionLanguage|null Evaluator for plurals |
||
78 | */ |
||
79 | private $pluralexpression = null; |
||
80 | /** |
||
81 | * @var int|null number of plurals |
||
82 | */ |
||
83 | private $pluralcount = null; |
||
84 | /** |
||
85 | * Array with original -> translation mapping. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $cache_translations = array(); |
||
90 | |||
91 | /** |
||
92 | * Constructor. |
||
93 | * |
||
94 | * @param string $filename Name of mo file to load |
||
95 | */ |
||
96 | 45 | public function __construct($filename) |
|
139 | |||
140 | /** |
||
141 | * Translates a string. |
||
142 | * |
||
143 | * @param string $msgid String to be translated |
||
144 | * |
||
145 | * @return string translated string (or original, if not found) |
||
146 | */ |
||
147 | 30 | public function gettext($msgid) |
|
155 | |||
156 | /** |
||
157 | * Check if a string is translated. |
||
158 | * |
||
159 | * @param string $msgid String to be checked |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 6 | public function exists($msgid) |
|
167 | |||
168 | /** |
||
169 | * Sanitize plural form expression for use in ExpressionLanguage. |
||
170 | * |
||
171 | * @param string $expr Expression to sanitize |
||
172 | * |
||
173 | * @return string sanitized plural form expression |
||
174 | */ |
||
175 | 14 | public static function sanitizePluralExpression($expr) |
|
199 | |||
200 | /** |
||
201 | * Extracts number of plurals from plurals form expression. |
||
202 | * |
||
203 | * @param string $expr Expression to process |
||
204 | * |
||
205 | * @return int Total number of plurals |
||
206 | */ |
||
207 | 13 | public static function extractPluralCount($expr) |
|
220 | |||
221 | /** |
||
222 | * Parse full PO header and extract only plural forms line. |
||
223 | * |
||
224 | * @param string $header Gettext header |
||
225 | * |
||
226 | * @return string verbatim plural form header field |
||
227 | */ |
||
228 | 11 | public static function extractPluralsForms($header) |
|
240 | |||
241 | /** |
||
242 | * Get possible plural forms from MO header. |
||
243 | * |
||
244 | * @return string plural form header |
||
245 | */ |
||
246 | 8 | private function getPluralForms() |
|
265 | |||
266 | /** |
||
267 | * Detects which plural form to take. |
||
268 | * |
||
269 | * @param int $n count of objects |
||
270 | * |
||
271 | * @return int array index of the right plural form |
||
272 | */ |
||
273 | 8 | private function selectString($n) |
|
292 | |||
293 | /** |
||
294 | * Plural version of gettext. |
||
295 | * |
||
296 | * @param string $msgid Single form |
||
297 | * @param string $msgidPlural Plural form |
||
298 | * @param int $number Number of objects |
||
299 | * |
||
300 | * @return string translated plural form |
||
301 | */ |
||
302 | 18 | public function ngettext($msgid, $msgidPlural, $number) |
|
322 | |||
323 | /** |
||
324 | * Translate with context. |
||
325 | * |
||
326 | * @param string $msgctxt Context |
||
327 | * @param string $msgid String to be translated |
||
328 | * |
||
329 | * @return string translated plural form |
||
330 | */ |
||
331 | 14 | View Code Duplication | public function pgettext($msgctxt, $msgid) |
341 | |||
342 | /** |
||
343 | * Plural version of pgettext. |
||
344 | * |
||
345 | * @param string $msgctxt Context |
||
346 | * @param string $msgid Single form |
||
347 | * @param string $msgidPlural Plural form |
||
348 | * @param int $number Number of objects |
||
349 | * |
||
350 | * @return string translated plural form |
||
351 | */ |
||
352 | 4 | View Code Duplication | public function npgettext($msgctxt, $msgid, $msgidPlural, $number) |
362 | |||
363 | /** |
||
364 | * Set translation in place |
||
365 | * |
||
366 | * @param string $msgid String to be set |
||
367 | * @param string $msgstr Translation |
||
368 | * |
||
369 | * @return void |
||
370 | */ |
||
371 | 1 | public function setTranslation($msgid, $msgstr) |
|
375 | |||
376 | /** |
||
377 | * @param array $cachedTranslations |
||
378 | * |
||
379 | * @return $this |
||
380 | */ |
||
381 | public function setCachedTranslations($cachedTranslations) |
||
387 | |||
388 | /** |
||
389 | * @return array |
||
390 | */ |
||
391 | public function getCachedTranslations() |
||
395 | } |
||
396 |
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.