| Conditions | 14 |
| Paths | 1 |
| Total Lines | 67 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 43 | public function register(DiInterface $di): void |
||
| 44 | { |
||
| 45 | $coreConfig = $di->getShared(ConfigProvider::SERVICE_NAME)->path('core'); |
||
| 46 | $di->setShared( |
||
| 47 | self::SERVICE_NAME, |
||
| 48 | function () use ($di, $coreConfig) { |
||
| 49 | $cacheKey = false; |
||
| 50 | $language = $di->get(LanguageProvider::SERVICE_NAME); |
||
| 51 | if (php_sapi_name() !== 'cli') { |
||
| 52 | $version = PBXConfModulesProvider::getVersionsHash(); |
||
| 53 | $cacheKey = 'LocalisationArray:' . $version . ':' . $language; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Check if translations exist in the cache |
||
| 57 | if ($cacheKey) { |
||
| 58 | $translates = $di->get(ManagedCacheProvider::SERVICE_NAME)->get($cacheKey, 3600); |
||
| 59 | if (is_array($translates)) { |
||
| 60 | return $translates; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | // Load English translations |
||
| 65 | $translates = self::includeLanguageFile(appPath('/src/Common/Messages/en.php')); |
||
| 66 | |||
| 67 | if ($language !== 'en') { |
||
| 68 | // Check if translation file exists for the selected language |
||
| 69 | $langFile = appPath("/src/Common/Messages/{$language}.php"); |
||
| 70 | if (file_exists($langFile)) { |
||
| 71 | $langArr = self::includeLanguageFile($langFile); |
||
| 72 | if (!empty($langArr)) { |
||
| 73 | $translates = array_merge($translates, $langArr); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // Load English translations for extensions |
||
| 79 | $extensionsTranslates = [[]]; |
||
| 80 | $results = glob($coreConfig->modulesDir . '/*/{Messages}/en.php', GLOB_BRACE); |
||
| 81 | foreach ($results as $path) { |
||
| 82 | $langArr = self::includeLanguageFile($path); |
||
| 83 | if (!empty($langArr)) { |
||
| 84 | $extensionsTranslates[] = $langArr; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | if ($extensionsTranslates !== [[]]) { |
||
| 88 | $translates = array_merge($translates, ...$extensionsTranslates); |
||
| 89 | } |
||
| 90 | if ($language !== 'en') { |
||
| 91 | $additionalTranslates = [[]]; |
||
| 92 | $results = glob( |
||
| 93 | $coreConfig->modulesDir . "/*/{Messages}/{$language}.php", |
||
| 94 | GLOB_BRACE |
||
| 95 | ); |
||
| 96 | foreach ($results as $path) { |
||
| 97 | $langArr = self::includeLanguageFile($path); |
||
| 98 | if (!empty($langArr)) { |
||
| 99 | $additionalTranslates[] = $langArr; |
||
| 100 | $translates = array_merge($translates, ...$additionalTranslates); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | if ($cacheKey) { |
||
| 105 | $di->get(ManagedCacheProvider::SERVICE_NAME)->set($cacheKey, $translates); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Return a translation object |
||
| 109 | return $translates; |
||
| 110 | } |
||
| 140 | } |