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:
Complex classes like Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Factory implements IFactory { |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | protected $requestLanguage = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * cached instances |
||
| 48 | * @var array Structure: Lang => App => \OCP\IL10N |
||
| 49 | */ |
||
| 50 | protected $instances = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array Structure: App => string[] |
||
| 54 | */ |
||
| 55 | protected $availableLanguages = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $availableLocales = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array Structure: string => callable |
||
| 64 | */ |
||
| 65 | protected $pluralFunctions = []; |
||
| 66 | |||
| 67 | const COMMON_LANGUAGE_CODES = [ |
||
| 68 | 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
||
| 69 | 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** @var IConfig */ |
||
| 73 | protected $config; |
||
| 74 | |||
| 75 | /** @var IRequest */ |
||
| 76 | protected $request; |
||
| 77 | |||
| 78 | /** @var IUserSession */ |
||
| 79 | protected $userSession; |
||
| 80 | |||
| 81 | /** @var string */ |
||
| 82 | protected $serverRoot; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param IConfig $config |
||
| 86 | * @param IRequest $request |
||
| 87 | * @param IUserSession $userSession |
||
| 88 | * @param string $serverRoot |
||
| 89 | */ |
||
| 90 | public function __construct(IConfig $config, |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a language instance |
||
| 102 | * |
||
| 103 | * @param string $app |
||
| 104 | * @param string|null $lang |
||
| 105 | * @param string|null $locale |
||
| 106 | * @return \OCP\IL10N |
||
| 107 | */ |
||
| 108 | public function get($app, $lang = null, $locale = null) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Find the best language |
||
| 144 | * |
||
| 145 | * @param string|null $app App id or null for core |
||
| 146 | * @return string language If nothing works it returns 'en' |
||
| 147 | */ |
||
| 148 | public function findLanguage($app = null) { |
||
| 149 | $forceLang = $this->config->getSystemValue('force_language', false); |
||
| 150 | if (is_string($forceLang)) { |
||
| 151 | $this->requestLanguage = $forceLang; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
||
| 155 | return $this->requestLanguage; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * At this point Nextcloud might not yet be installed and thus the lookup |
||
| 160 | * in the preferences table might fail. For this reason we need to check |
||
| 161 | * whether the instance has already been installed |
||
| 162 | * |
||
| 163 | * @link https://github.com/owncloud/core/issues/21955 |
||
| 164 | */ |
||
| 165 | if ($this->config->getSystemValue('installed', false)) { |
||
| 166 | $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
||
| 167 | if (!is_null($userId)) { |
||
| 168 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
||
| 169 | } else { |
||
| 170 | $userLang = null; |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $userId = null; |
||
| 174 | $userLang = null; |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($userLang) { |
||
|
|
|||
| 178 | $this->requestLanguage = $userLang; |
||
| 179 | if ($this->languageExists($app, $userLang)) { |
||
| 180 | return $userLang; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | try { |
||
| 185 | // Try to get the language from the Request |
||
| 186 | $lang = $this->getLanguageFromRequest($app); |
||
| 187 | if ($userId !== null && $app === null && !$userLang) { |
||
| 188 | $this->config->setUserValue($userId, 'core', 'lang', $lang); |
||
| 189 | } |
||
| 190 | return $lang; |
||
| 191 | } catch (LanguageNotFoundException $e) { |
||
| 192 | // Finding language from request failed fall back to default language |
||
| 193 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 194 | if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
||
| 195 | return $defaultLanguage; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // We could not find any language so fall back to english |
||
| 200 | return 'en'; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * find the best locale |
||
| 205 | * |
||
| 206 | * @param string $lang |
||
| 207 | * @return null|string |
||
| 208 | */ |
||
| 209 | public function findLocale($lang = null) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Find all available languages for an app |
||
| 247 | * |
||
| 248 | * @param string|null $app App id or null for core |
||
| 249 | * @return array an array of available languages |
||
| 250 | */ |
||
| 251 | public function findAvailableLanguages($app = null) { |
||
| 252 | $key = $app; |
||
| 253 | if ($key === null) { |
||
| 254 | $key = 'null'; |
||
| 255 | } |
||
| 256 | |||
| 257 | // also works with null as key |
||
| 258 | if (!empty($this->availableLanguages[$key])) { |
||
| 259 | return $this->availableLanguages[$key]; |
||
| 260 | } |
||
| 261 | |||
| 262 | $available = ['en']; //english is always available |
||
| 263 | $dir = $this->findL10nDir($app); |
||
| 264 | View Code Duplication | if (is_dir($dir)) { |
|
| 265 | $files = scandir($dir); |
||
| 266 | if ($files !== false) { |
||
| 267 | foreach ($files as $file) { |
||
| 268 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 269 | $available[] = substr($file, 0, -5); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | // merge with translations from theme |
||
| 276 | $theme = $this->config->getSystemValue('theme'); |
||
| 277 | if (!empty($theme)) { |
||
| 278 | $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
||
| 279 | |||
| 280 | View Code Duplication | if (is_dir($themeDir)) { |
|
| 281 | $files = scandir($themeDir); |
||
| 282 | if ($files !== false) { |
||
| 283 | foreach ($files as $file) { |
||
| 284 | if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
||
| 285 | $available[] = substr($file, 0, -5); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->availableLanguages[$key] = $available; |
||
| 293 | return $available; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return array|mixed |
||
| 298 | */ |
||
| 299 | public function findAvailableLocales() { |
||
| 300 | if (!empty($this->availableLocales)) { |
||
| 301 | return $this->availableLocales; |
||
| 302 | } |
||
| 303 | |||
| 304 | $localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json'); |
||
| 305 | $this->availableLocales = \json_decode($localeData, true); |
||
| 306 | |||
| 307 | return $this->availableLocales; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string|null $app App id or null for core |
||
| 312 | * @param string $lang |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function languageExists($app, $lang) { |
||
| 316 | if ($lang === 'en') {//english is always available |
||
| 317 | return true; |
||
| 318 | } |
||
| 319 | |||
| 320 | $languages = $this->findAvailableLanguages($app); |
||
| 321 | return array_search($lang, $languages) !== false; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $locale |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function localeExists($locale) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string|null $app |
||
| 343 | * @return string |
||
| 344 | * @throws LanguageNotFoundException |
||
| 345 | */ |
||
| 346 | private function getLanguageFromRequest($app) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * if default language is set to de_DE (formal German) this should be |
||
| 379 | * preferred to 'de' (non-formal German) if possible |
||
| 380 | * |
||
| 381 | * @param string|null $app |
||
| 382 | * @param string $lang |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | protected function respectDefaultLanguage($app, $lang) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Checks if $sub is a subdirectory of $parent |
||
| 404 | * |
||
| 405 | * @param string $sub |
||
| 406 | * @param string $parent |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | private function isSubDirectory($sub, $parent) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get a list of language files that should be loaded |
||
| 425 | * |
||
| 426 | * @param string $app |
||
| 427 | * @param string $lang |
||
| 428 | * @return string[] |
||
| 429 | */ |
||
| 430 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
||
| 431 | // FIXME This is also the reason, why it is not in the public interface |
||
| 432 | public function getL10nFilesForApp($app, $lang) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * find the l10n directory |
||
| 462 | * |
||
| 463 | * @param string $app App id or empty string for core |
||
| 464 | * @return string directory |
||
| 465 | */ |
||
| 466 | protected function findL10nDir($app = null) { |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Creates a function from the plural string |
||
| 481 | * |
||
| 482 | * Parts of the code is copied from Habari: |
||
| 483 | * https://github.com/habari/system/blob/master/classes/locale.php |
||
| 484 | * @param string $string |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function createPluralFunction($string) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * returns the common language and other languages in an |
||
| 545 | * associative array |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | public function getLanguages() { |
||
| 612 | } |
||
| 613 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: