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 |
||
| 38 | class Factory implements IFactory { |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | protected $requestLanguage = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * cached instances |
||
| 45 | * @var array Structure: Lang => App => \OCP\IL10N |
||
| 46 | */ |
||
| 47 | protected $instances = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array Structure: App => string[] |
||
| 51 | */ |
||
| 52 | protected $availableLanguages = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Structure: string => callable |
||
| 56 | */ |
||
| 57 | protected $pluralFunctions = []; |
||
| 58 | |||
| 59 | /** @var IConfig */ |
||
| 60 | protected $config; |
||
| 61 | |||
| 62 | /** @var IRequest */ |
||
| 63 | protected $request; |
||
| 64 | |||
| 65 | /** @var IUserSession */ |
||
| 66 | protected $userSession; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | protected $serverRoot; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param IConfig $config |
||
| 73 | * @param IRequest $request |
||
| 74 | * @param IUserSession $userSession |
||
| 75 | * @param string $serverRoot |
||
| 76 | */ |
||
| 77 | public function __construct(IConfig $config, |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get a language instance |
||
| 89 | * |
||
| 90 | * @param string $app |
||
| 91 | * @param string|null $lang |
||
| 92 | * @return \OCP\IL10N |
||
| 93 | */ |
||
| 94 | public function get($app, $lang = null) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Find the best language |
||
| 115 | * |
||
| 116 | * @param string|null $app App id or null for core |
||
| 117 | * @return string language If nothing works it returns 'en' |
||
| 118 | */ |
||
| 119 | public function findLanguage($app = null) { |
||
| 120 | if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
||
| 121 | return $this->requestLanguage; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * At this point ownCloud might not yet be installed and thus the lookup |
||
| 126 | * in the preferences table might fail. For this reason we need to check |
||
| 127 | * whether the instance has already been installed |
||
| 128 | * |
||
| 129 | * @link https://github.com/owncloud/core/issues/21955 |
||
| 130 | */ |
||
| 131 | if(!is_null($this->userSession) && $this->config->getSystemValue('installed', false)) { |
||
| 132 | $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
||
| 133 | if(!is_null($userId)) { |
||
| 134 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
||
| 135 | } else { |
||
| 136 | $userLang = null; |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $userId = null; |
||
| 140 | $userLang = null; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($userLang) { |
||
|
|
|||
| 144 | $this->requestLanguage = $userLang; |
||
| 145 | if ($this->languageExists($app, $userLang)) { |
||
| 146 | return $userLang; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $defaultLanguage = $this->config->getSystemValue('default_language', false); |
||
| 151 | |||
| 152 | if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
||
| 153 | return $defaultLanguage; |
||
| 154 | } |
||
| 155 | |||
| 156 | $lang = $this->setLanguageFromRequest($app); |
||
| 157 | if ($userId !== null && $app === null && !$userLang) { |
||
| 158 | $this->config->setUserValue($userId, 'core', 'lang', $lang); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $lang; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Find all available languages for an app |
||
| 166 | * |
||
| 167 | * @param string|null $app App id or null for core |
||
| 168 | * @return array an array of available languages |
||
| 169 | */ |
||
| 170 | public function findAvailableLanguages($app = null) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param string|null $app App id or null for core |
||
| 217 | * @param string $lang |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function languageExists($app, $lang) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string|null $app App id or null for core |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function setLanguageFromRequest($app = null) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get a list of language files that should be loaded |
||
| 275 | * |
||
| 276 | * @param string $app |
||
| 277 | * @param string $lang |
||
| 278 | * @return string[] |
||
| 279 | */ |
||
| 280 | // FIXME This method is only public, until \OCP\IL10N does not need it anymore, |
||
| 281 | // FIXME This is also the reason, why it is not in the public interface |
||
| 282 | public function getL10nFilesForApp($app, $lang) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * find the l10n directory |
||
| 312 | * |
||
| 313 | * @param string $app App id or empty string for core |
||
| 314 | * @return string directory |
||
| 315 | */ |
||
| 316 | protected function findL10nDir($app = null) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Creates a function from the plural string |
||
| 330 | * |
||
| 331 | * @param string $string |
||
| 332 | * @return string Unique function name |
||
| 333 | * @since 9.0.0 |
||
| 334 | */ |
||
| 335 | public function createPluralFunction($string) { |
||
| 338 | } |
||
| 339 |
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: