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 |
||
10 | class Builder |
||
11 | { |
||
12 | /** |
||
13 | * Get the translation. |
||
14 | * |
||
15 | * @param string $string |
||
16 | * |
||
17 | * @return object |
||
18 | */ |
||
19 | public static function trans($string, $vars = []) |
||
34 | |||
35 | /** |
||
36 | * Generate files json for a specify package. |
||
37 | * |
||
38 | * @param string $string |
||
|
|||
39 | * |
||
40 | * @return object |
||
41 | */ |
||
42 | public static function generateTranslations($is_package, $package, $translationsPath, $toLangs = 'es') |
||
65 | |||
66 | private static function transArray($array, $lang) |
||
67 | { |
||
68 | foreach ($array as $var => $text) { |
||
69 | $vars = []; |
||
70 | if (is_array($text)) { |
||
71 | $array[$var] = self::transArray($text, $lang); |
||
72 | } else { |
||
73 | foreach (explode(' ', $text) as $word) { |
||
74 | if (substr($word, 0, 1) == ':' && !in_array($word, $vars)) { |
||
75 | array_push($vars, $word); |
||
76 | } |
||
77 | } |
||
78 | $entry = $text; |
||
79 | foreach ($vars as $key => $varreplace) { |
||
80 | $entry = str_replace($varreplace, 'VAR_'.$key, $entry); |
||
81 | } |
||
82 | $array[$var] = self::trans($entry, $vars)->from('en')->to($lang)->Save(false)->__toString(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return stripslashes(var_export($array, true)); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get the languages that translations has been translated. |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | View Code Duplication | public static function toLanguages() |
|
104 | |||
105 | /** |
||
106 | * Get the languages from translations has been translated. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | View Code Duplication | public static function fromLanguages() |
|
120 | |||
121 | /** |
||
122 | * Get all the available languages. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public static function allLanguages() |
||
141 | } |
||
142 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.