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 string */ |
||
| 72 | protected $serverRoot; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param IConfig $config |
||
| 76 | * @param IRequest $request |
||
| 77 | * @param IUserSession $userSession |
||
| 78 | * @param string $serverRoot |
||
| 79 | */ |
||
| 80 | public function __construct(IConfig $config, |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get a language instance |
||
| 92 | * |
||
| 93 | * @param string $app |
||
| 94 | * @param string|null $lang |
||
| 95 | * @return \OCP\IL10N |
||
| 96 | */ |
||
| 97 | public function get($app, $lang = null) { |
||
| 98 | $app = \OC_App::cleanAppId($app); |
||
| 99 | if ($lang !== null) { |
||
| 100 | $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
||
| 101 | } |
||
| 102 | if ($lang === null || !$this->languageExists($app, $lang)) { |
||
| 103 | $lang = $this->findLanguage($app); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!isset($this->instances[$lang][$app])) { |
||
| 107 | $this->instances[$lang][$app] = new L10N( |
||
| 108 | $this, $app, $lang, |
||
| 109 | $this->getL10nFilesForApp($app, $lang) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $this->instances[$lang][$app]; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Find the best language |
||
| 118 | * |
||
| 119 | * @param string|null $app App id or null for core |
||
| 120 | * @return string language If nothing works it returns 'en' |
||
| 121 | */ |
||
| 122 | public function findLanguage($app = null) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Find all available languages for an app |
||
| 174 | * |
||
| 175 | * @param string|null $app App id or null for core |
||
| 176 | * @return array an array of available languages |
||
| 177 | */ |
||
| 178 | public function findAvailableLanguages($app = null) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string|null $app App id or null for core |
||
| 225 | * @param string $lang |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function languageExists($app, $lang) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string|null $app |
||
| 239 | * @return string |
||
| 240 | * @throws LanguageNotFoundException |
||
| 241 | */ |
||
| 242 | private function getLanguageFromRequest($app) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Checks if $sub is a subdirectory of $parent |
||
| 275 | * |
||
| 276 | * @param string $sub |
||
| 277 | * @param string $parent |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | private function isSubDirectory($sub, $parent) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get a list of language files that should be loaded |
||
| 296 | * |
||
| 297 | * @param string $app |
||
| 298 | * @param string $lang |
||
| 299 | * @return string[] |
||
| 300 | */ |
||
| 301 | // FIXME This method is only public, until OC_L10N does not need it anymore, |
||
| 302 | // FIXME This is also the reason, why it is not in the public interface |
||
| 303 | public function getL10nFilesForApp($app, $lang) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * find the l10n directory |
||
| 333 | * |
||
| 334 | * @param string $app App id or empty string for core |
||
| 335 | * @return string directory |
||
| 336 | */ |
||
| 337 | protected function findL10nDir($app = null) { |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Creates a function from the plural string |
||
| 352 | * |
||
| 353 | * Parts of the code is copied from Habari: |
||
| 354 | * https://github.com/habari/system/blob/master/classes/locale.php |
||
| 355 | * @param string $string |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function createPluralFunction($string) { |
||
| 412 | } |
||
| 413 |
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: