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 |
||
| 29 | class ImportManager |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $locales; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Repository |
||
| 38 | */ |
||
| 39 | private $repository; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Manager |
||
| 43 | */ |
||
| 44 | private $manager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var YamlFileLoader |
||
| 48 | */ |
||
| 49 | private $parser; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $kernelRoot; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Repository $repository |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getLocales() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param array $locales |
||
| 80 | */ |
||
| 81 | public function setLocales(array $locales) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Write translations to storage. |
||
| 88 | * |
||
| 89 | * @param $translations |
||
| 90 | */ |
||
| 91 | public function writeToStorage($domain, $translations) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $translations |
||
| 122 | */ |
||
| 123 | public function cleanTranslations(array $translations) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Imports translation files from a directory. |
||
| 137 | * @param string $dir |
||
| 138 | */ |
||
| 139 | public function importTranslationFiles($domain, $dir) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Return a Finder object if $path has a Resources/translations folder. |
||
| 147 | * |
||
| 148 | * @param string $domain |
||
| 149 | * @param string $path |
||
| 150 | * @param array $directories |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function getTranslationsFromFiles($domain, $path, array $directories = []) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param SplFileInfo $file |
||
| 183 | * @param string $domain |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getFileTranslationMessages(SplFileInfo $file, $domain) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Flatten multidimensional array and concatenating keys |
||
| 218 | * |
||
| 219 | * @param $array |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | View Code Duplication | private function flatten($array, $prefix = '') { |
|
| 223 | $result = []; |
||
| 224 | foreach($array as $key=>$value) { |
||
| 225 | if(is_array($value)) { |
||
| 226 | $result = $result + $this->flatten($value, $prefix . $key . '.'); |
||
| 227 | } |
||
| 228 | else { |
||
| 229 | $result[$prefix . $key] = $value; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | return $result; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | private function getFileNamePattern() |
||
| 247 | } |
||
| 248 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..