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 | 25 | 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 | 21 | public function gettext($msgid) |
|
155 | |||
156 | /** |
||
157 | * Sanitize plural form expression for use in ExpressionLanguage. |
||
158 | * |
||
159 | * @param string $expr Expression to sanitize |
||
160 | * |
||
161 | * @return string sanitized plural form expression |
||
162 | */ |
||
163 | 11 | public static function sanitizePluralExpression($expr) |
|
187 | |||
188 | /** |
||
189 | * Extracts number of plurals from plurals form expression. |
||
190 | * |
||
191 | * @param string $expr Expression to process |
||
192 | * |
||
193 | * @return int Total number of plurals |
||
194 | */ |
||
195 | 10 | public static function extractPluralCount($expr) |
|
208 | |||
209 | /** |
||
210 | * Parse full PO header and extract only plural forms line. |
||
211 | * |
||
212 | * @param string $header Gettext header |
||
213 | * |
||
214 | * @return string verbatim plural form header field |
||
215 | */ |
||
216 | 8 | public static function extractPluralsForms($header) |
|
228 | |||
229 | /** |
||
230 | * Get possible plural forms from MO header. |
||
231 | * |
||
232 | * @return string plural form header |
||
233 | */ |
||
234 | 5 | private function getPluralForms() |
|
253 | |||
254 | /** |
||
255 | * Detects which plural form to take. |
||
256 | * |
||
257 | * @param int $n count of objects |
||
258 | * |
||
259 | * @return int array index of the right plural form |
||
260 | */ |
||
261 | 5 | private function selectString($n) |
|
276 | |||
277 | /** |
||
278 | * Plural version of gettext. |
||
279 | * |
||
280 | * @param string $msgid Single form |
||
281 | * @param string $msgidPlural Plural form |
||
282 | * @param int $number Number of objects |
||
283 | * |
||
284 | * @return string translated plural form |
||
285 | */ |
||
286 | 14 | public function ngettext($msgid, $msgidPlural, $number) |
|
302 | |||
303 | /** |
||
304 | * Translate with context. |
||
305 | * |
||
306 | * @param string $msgctxt Context |
||
307 | * @param string $msgid String to be translated |
||
308 | * |
||
309 | * @return string translated plural form |
||
310 | */ |
||
311 | 11 | View Code Duplication | public function pgettext($msgctxt, $msgid) |
321 | |||
322 | /** |
||
323 | * Plural version of pgettext. |
||
324 | * |
||
325 | * @param string $msgctxt Context |
||
326 | * @param string $msgid Single form |
||
327 | * @param string $msgidPlural Plural form |
||
328 | * @param int $number Number of objects |
||
329 | * |
||
330 | * @return string translated plural form |
||
331 | */ |
||
332 | 4 | View Code Duplication | public function npgettext($msgctxt, $msgid, $msgidPlural, $number) |
342 | } |
||
343 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.