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 Structure: string => callable |
||
| 59 | */ |
||
| 60 | protected $pluralFunctions = []; |
||
| 61 | |||
| 62 | /** @var IConfig */ |
||
| 63 | protected $config; |
||
| 64 | |||
| 65 | /** @var IRequest */ |
||
| 66 | protected $request; |
||
| 67 | |||
| 68 | /** @var IUserSession */ |
||
| 69 | protected $userSession; |
||
| 70 | |||
| 71 | /** @var IThemeService */ |
||
| 72 | protected $themeService; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | protected $serverRoot; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param IConfig $config |
||
| 79 | * @param IRequest $request |
||
| 80 | * @param IThemeService $themeService |
||
| 81 | * @param IUserSession $userSession |
||
| 82 | * @param string $serverRoot |
||
| 83 | */ |
||
| 84 | public function __construct(IConfig $config, |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get a language instance |
||
| 98 | * |
||
| 99 | * @param string $app |
||
| 100 | * @param string|null $lang |
||
| 101 | * @return \OCP\IL10N |
||
| 102 | */ |
||
| 103 | public function get($app, $lang = null) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Find the best language |
||
| 124 | * |
||
| 125 | * @param string|null $app App id or null for core |
||
| 126 | * @return string language If nothing works it returns 'en' |
||
| 127 | */ |
||
| 128 | public function findLanguage($app = null) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Find all available languages for an app |
||
| 175 | * |
||
| 176 | * @param string|null $app App id or null for core |
||
| 177 | * @return array an array of available languages |
||
| 178 | */ |
||
| 179 | public function findAvailableLanguages($app = null) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string|null $app App id or null for core |
||
| 208 | * @param string $lang |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function languageExists($app, $lang) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string|null $app App id or null for core |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function setLanguageFromRequest($app = null) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get a list of language files that should be loaded |
||
| 266 | * |
||
| 267 | * @param string $app |
||
| 268 | * @param string $lang |
||
| 269 | * @return string[] |
||
| 270 | */ |
||
| 271 | // FIXME This method is only public, until \OCP\IL10N does not need it anymore, |
||
| 272 | // FIXME This is also the reason, why it is not in the public interface |
||
| 273 | public function getL10nFilesForApp($app, $lang) { |
||
| 274 | $languageFiles = []; |
||
| 275 | |||
| 276 | $i18nDir = $this->findL10nDir($app); |
||
| 277 | $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
||
| 278 | |||
| 279 | if ((\OC_Helper::isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
||
| 280 | || \OC_Helper::isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
||
| 281 | || \OC_Helper::isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
||
| 282 | || \OC_Helper::isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
||
| 283 | ) |
||
| 284 | && file_exists($transFile)) { |
||
| 285 | // load the translations file |
||
| 286 | $languageFiles[] = $transFile; |
||
| 287 | } |
||
| 288 | |||
| 289 | // merge with translations from themes |
||
| 290 | $relativePath = substr($transFile, strlen($this->serverRoot)); |
||
| 291 | $themeDir = $this->getActiveThemeDirectory(); |
||
| 292 | if ($themeDir !== '') { |
||
| 293 | $themeTransFile = $themeDir . $relativePath; |
||
| 294 | if (file_exists($themeTransFile)) { |
||
| 295 | $languageFiles[] = $themeTransFile; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | return $languageFiles; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * find the l10n directory |
||
| 304 | * |
||
| 305 | * @param string $app App id or empty string for core |
||
| 306 | * @return string directory |
||
| 307 | */ |
||
| 308 | protected function findL10nDir($app = null) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $dir |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | protected function findAvailableLanguageFiles($dir) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the currently active theme |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | protected function getActiveThemeDirectory() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get the currently active legacy theme |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function getActiveLegacyThemeDirectory() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the currently active app-theme |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | protected function getActiveAppThemeDirectory() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Creates a function from the plural string |
||
| 382 | * |
||
| 383 | * @param string $string |
||
| 384 | * @return string Unique function name |
||
| 385 | * @since 9.0.0 |
||
| 386 | */ |
||
| 387 | public function createPluralFunction($string) { |
||
| 390 | } |
||
| 391 |
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: