| Total Complexity | 46 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 2 |
Complex classes like LangHelper 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.
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 LangHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class LangHelper |
||
| 19 | { |
||
| 20 | use Configurable; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The default key for global translation |
||
| 24 | */ |
||
| 25 | const GLOBAL_ENTITY = 'Global'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Provision fluent locales defined in yml |
||
| 29 | * Use LangHelper::provisionLocales |
||
| 30 | * |
||
| 31 | * eg: |
||
| 32 | * LeKoala\Multilingual\LangHelper: |
||
| 33 | * default_locales: |
||
| 34 | * - en_US |
||
| 35 | * - fr_FR |
||
| 36 | * |
||
| 37 | * @var array<string> |
||
| 38 | */ |
||
| 39 | private static $default_locales = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @config |
||
| 43 | * @var boolean |
||
| 44 | */ |
||
| 45 | private static $persist_cookie = true; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array<string,string> |
||
| 49 | */ |
||
| 50 | protected static $locale_cache = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get a global translation |
||
| 54 | * |
||
| 55 | * By default all global translation are stored under the Global key |
||
| 56 | * |
||
| 57 | * @param string $entity If no entity is specified, Global is assumed |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public static function globalTranslation(string $entity): string |
||
| 61 | { |
||
| 62 | $parts = explode('.', $entity); |
||
| 63 | if (count($parts) == 1) { |
||
| 64 | array_unshift($parts, self::GLOBAL_ENTITY); |
||
| 65 | } |
||
| 66 | return i18n::_t(implode('.', $parts), $entity); |
||
| 67 | } |
||
| 68 | |||
| 69 | public static function hasFluent(): bool |
||
| 70 | { |
||
| 71 | return class_exists('\\TractorCow\\Fluent\\Middleware\\DetectLocaleMiddleware'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Call this to make sure we are not setting any cookies that has |
||
| 76 | * not been accepted |
||
| 77 | */ |
||
| 78 | public static function persistLocaleIfCookiesAreAllowed(): void |
||
| 111 | } |
||
| 112 | |||
| 113 | public static function getLanguageName($locale, $short = true) |
||
| 114 | { |
||
| 115 | $locale = self::get_locale_from_lang($locale); |
||
| 116 | |||
| 117 | $allLocales = i18n::getSources()->getKnownLocales(); |
||
| 118 | |||
| 119 | $foundLocale = $allLocales[$locale] ?? null; |
||
| 120 | if ($foundLocale && $short) { |
||
| 121 | $foundLocale = preg_replace('/\s*\(.*?\)/', '', $foundLocale); |
||
| 122 | $foundLocale = ucfirst(trim($foundLocale)); |
||
| 123 | } |
||
| 124 | return $foundLocale; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Persist locale according to fluent settings |
||
| 129 | */ |
||
| 130 | public static function persistLocale(): void |
||
| 131 | { |
||
| 132 | if (!self::hasFluent()) { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | $curr = Controller::curr(); |
||
| 137 | $request = $curr->getRequest(); |
||
| 138 | |||
| 139 | $secure = Director::is_https($request) |
||
| 140 | && Session::config()->get('cookie_secure'); |
||
| 141 | |||
| 142 | $class = \TractorCow\Fluent\Middleware\DetectLocaleMiddleware::class; |
||
| 143 | $persistIds = $class::config()->get('persist_ids'); |
||
| 144 | $persistKey = FluentState::singleton()->getIsFrontend() |
||
| 145 | ? $persistIds['frontend'] |
||
| 146 | : $persistIds['cms']; |
||
| 147 | |||
| 148 | $locale = $request->getSession()->get($persistKey); |
||
| 149 | // If session is not started or set, it may not be set |
||
| 150 | if (!$locale) { |
||
| 151 | $locale = self::get_locale(); |
||
| 152 | } |
||
| 153 | |||
| 154 | Cookie::set( |
||
| 155 | $persistKey, |
||
| 156 | $locale, |
||
| 157 | $class::config()->get('persist_cookie_expiry'), |
||
| 158 | $class::config()->get('persist_cookie_path'), |
||
| 159 | $class::config()->get('persist_cookie_domain'), |
||
| 160 | $secure, |
||
| 161 | $class::config()->get('persist_cookie_http_only') |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Provision locales defined in default_locales |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public static function provisionLocales() |
||
| 171 | { |
||
| 172 | $locales = self::config()->default_locales; |
||
| 173 | if (empty($locales)) { |
||
| 174 | throw new Exception("No locales defined in LangHelper:default_locales"); |
||
| 175 | } |
||
| 176 | |||
| 177 | foreach ($locales as $loc) { |
||
| 178 | $Locale = Locale::get()->filter('Locale', $loc)->first(); |
||
| 179 | $allLocales = i18n::getData()->getLocales(); |
||
| 180 | if (!$Locale) { |
||
| 181 | $Locale = new Locale(); |
||
| 182 | $Locale->Title = $allLocales[$loc]; |
||
| 183 | $Locale->Locale = $loc; |
||
| 184 | $Locale->URLSegment = self::get_lang($loc); |
||
| 185 | $Locale->IsGlobalDefault = $loc == i18n::get_locale(); |
||
| 186 | $Locale->write(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Make sure we get a proper two characters lang |
||
| 193 | * |
||
| 194 | * @param string|object $lang a string or a fluent locale object |
||
| 195 | * @return string a two chars lang |
||
| 196 | */ |
||
| 197 | public static function get_lang($lang = null) |
||
| 198 | { |
||
| 199 | if (!$lang) { |
||
| 200 | $lang = self::get_locale(); |
||
| 201 | } |
||
| 202 | if (is_object($lang)) { |
||
| 203 | $lang = $lang->Locale; |
||
| 204 | } |
||
| 205 | return substr($lang, 0, 2); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the right locale (using fluent data if exists) |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public static function get_locale() |
||
| 214 | { |
||
| 215 | if (class_exists(FluentState::class)) { |
||
| 216 | $locale = FluentState::singleton()->getLocale(); |
||
| 217 | // Locale may not be set, in tests for instance |
||
| 218 | if ($locale) { |
||
| 219 | return $locale; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | return i18n::get_locale(); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get a locale from the lang |
||
| 227 | * |
||
| 228 | * @param string $lang |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public static function get_locale_from_lang($lang) |
||
| 232 | { |
||
| 233 | // Use fluent data |
||
| 234 | if (class_exists(Locale::class)) { |
||
| 235 | if (empty(self::$locale_cache)) { |
||
| 236 | $fluentLocales = Locale::getLocales(); |
||
| 237 | foreach ($fluentLocales as $locale) { |
||
| 238 | self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | if (isset(self::$locale_cache[$lang])) { |
||
| 242 | return self::$locale_cache[$lang]; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | // Guess |
||
| 246 | $localesData = i18n::getData(); |
||
| 247 | return $localesData->localeFromLang($lang); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Do we have the subsite module installed |
||
| 252 | * TODO: check if it might be better to use module manifest instead? |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public static function usesFluent() |
||
| 257 | { |
||
| 258 | return class_exists(FluentState::class); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return array<string> |
||
| 263 | */ |
||
| 264 | public static function get_available_langs() |
||
| 265 | { |
||
| 266 | if (!self::usesFluent()) { |
||
| 267 | return [ |
||
| 268 | self::get_lang() |
||
| 269 | ]; |
||
| 270 | } |
||
| 271 | |||
| 272 | $allLocales = Locale::get(); |
||
| 273 | $results = []; |
||
| 274 | foreach ($allLocales as $locale) { |
||
| 275 | $results[] = $locale->URLSegment; |
||
| 276 | } |
||
| 277 | return $results; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Execute the callback in given subsite |
||
| 282 | * |
||
| 283 | * @param string $locale |
||
| 284 | * @param callable $cb |
||
| 285 | * @return mixed the callback result |
||
| 286 | */ |
||
| 287 | public static function withLocale($locale, $cb) |
||
| 300 | }); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Execute the callback for all locales |
||
| 305 | * |
||
| 306 | * @param callable $cb |
||
| 307 | * @return array an array of callback results |
||
| 308 | */ |
||
| 309 | public static function withLocales($cb) |
||
| 321 | } |
||
| 322 | } |
||
| 323 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths