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 |
||
| 34 | class L10N implements IL10N { |
||
| 35 | |||
| 36 | /** @var IFactory */ |
||
| 37 | protected $factory; |
||
| 38 | |||
| 39 | /** @var string App of this object */ |
||
| 40 | protected $app; |
||
| 41 | |||
| 42 | /** @var string Language of this object */ |
||
| 43 | protected $lang; |
||
| 44 | |||
| 45 | /** @var string Locale of this object */ |
||
| 46 | protected $locale; |
||
| 47 | |||
| 48 | /** @var string Plural forms (string) */ |
||
| 49 | private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
||
| 50 | |||
| 51 | /** @var string Plural forms (function) */ |
||
| 52 | private $pluralFormFunction = null; |
||
| 53 | |||
| 54 | /** @var string[] */ |
||
| 55 | private $translations = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param IFactory $factory |
||
| 59 | * @param string $app |
||
| 60 | * @param string $lang |
||
| 61 | * @param string $locale |
||
| 62 | * @param array $files |
||
| 63 | */ |
||
| 64 | public function __construct(IFactory $factory, $app, $lang, $locale, array $files) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The code (en, de, ...) of the language that is used for this instance |
||
| 77 | * |
||
| 78 | * @return string language |
||
| 79 | */ |
||
| 80 | public function getLanguageCode(): string { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The code (en_US, fr_CA, ...) of the locale that is used for this instance |
||
| 86 | * |
||
| 87 | * @return string locale |
||
| 88 | */ |
||
| 89 | public function getLocaleCode(): string { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Translating |
||
| 95 | * @param string $text The text we need a translation for |
||
| 96 | * @param array|string $parameters default:array() Parameters for sprintf |
||
| 97 | * @return string Translation or the same text |
||
| 98 | * |
||
| 99 | * Returns the translation. If no translation is found, $text will be |
||
| 100 | * returned. |
||
| 101 | */ |
||
| 102 | public function t(string $text, $parameters = []): string { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Translating |
||
| 112 | * @param string $text_singular the string to translate for exactly one object |
||
| 113 | * @param string $text_plural the string to translate for n objects |
||
| 114 | * @param integer $count Number of objects |
||
| 115 | * @param array $parameters default:array() Parameters for sprintf |
||
| 116 | * @return string Translation or the same text |
||
| 117 | * |
||
| 118 | * Returns the translation. If no translation is found, $text will be |
||
| 119 | * returned. %n will be replaced with the number of objects. |
||
| 120 | * |
||
| 121 | * The correct plural is determined by the plural_forms-function |
||
| 122 | * provided by the po file. |
||
| 123 | * |
||
| 124 | */ |
||
| 125 | public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Localization |
||
| 140 | * @param string $type Type of localization |
||
| 141 | * @param \DateTime|int|string $data parameters for this localization |
||
| 142 | * @param array $options |
||
| 143 | * @return string|int|false |
||
| 144 | * |
||
| 145 | * Returns the localized data. |
||
| 146 | * |
||
| 147 | * Implemented types: |
||
| 148 | * - date |
||
| 149 | * - Creates a date |
||
| 150 | * - params: timestamp (int/string) |
||
| 151 | * - datetime |
||
| 152 | * - Creates date and time |
||
| 153 | * - params: timestamp (int/string) |
||
| 154 | * - time |
||
| 155 | * - Creates a time |
||
| 156 | * - params: timestamp (int/string) |
||
| 157 | * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
||
| 158 | * - jsdate: Returns the short JS date format |
||
| 159 | */ |
||
| 160 | public function l(string $type, $data = null, array $options = []) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns an associative array with all translations |
||
| 205 | * |
||
| 206 | * Called by \OC_L10N_String |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getTranslations(): array { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returnsed function accepts the argument $n |
||
| 215 | * |
||
| 216 | * Called by \OC_L10N_String |
||
| 217 | * @return \Closure the plural form function |
||
| 218 | */ |
||
| 219 | public function getPluralFormFunction(): \Closure { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $translationFile |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | protected function load(string $translationFile): bool { |
||
| 248 | } |
||
| 249 |
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..