| Conditions | 11 |
| Paths | 13 |
| Total Lines | 61 |
| Code Lines | 32 |
| 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 |
||
| 146 | public function loadLanguage($code) |
||
| 147 | { |
||
| 148 | static $languages_seen = array(); // recursion guard |
||
| 149 | |||
| 150 | // abort if we've already loaded it |
||
| 151 | if (isset($this->cache[$code])) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | // generate filename |
||
| 156 | $filename = $this->dir . '/Language/messages/' . $code . '.php'; |
||
| 157 | |||
| 158 | // default fallback : may be overwritten by the ensuing include |
||
| 159 | $fallback = ($code != 'en') ? 'en' : false; |
||
| 160 | |||
| 161 | // load primary localisation |
||
| 162 | if (!file_exists($filename)) { |
||
| 163 | // skip the include: will rely solely on fallback |
||
| 164 | $filename = $this->dir . '/Language/messages/en.php'; |
||
| 165 | $cache = array(); |
||
| 166 | } else { |
||
| 167 | include $filename; |
||
| 168 | $cache = compact($this->keys); |
||
| 169 | } |
||
| 170 | |||
| 171 | // load fallback localisation |
||
| 172 | if (!empty($fallback)) { |
||
| 173 | |||
| 174 | // infinite recursion guard |
||
| 175 | if (isset($languages_seen[$code])) { |
||
| 176 | trigger_error( |
||
| 177 | 'Circular fallback reference in language ' . |
||
| 178 | $code, |
||
| 179 | E_USER_ERROR |
||
| 180 | ); |
||
| 181 | $fallback = 'en'; |
||
| 182 | } |
||
| 183 | $language_seen[$code] = true; |
||
| 184 | |||
| 185 | // load the fallback recursively |
||
| 186 | $this->loadLanguage($fallback); |
||
| 187 | $fallback_cache = $this->cache[$fallback]; |
||
| 188 | |||
| 189 | // merge fallback with current language |
||
| 190 | foreach ($this->keys as $key) { |
||
| 191 | if (isset($cache[$key]) && isset($fallback_cache[$key])) { |
||
| 192 | if (isset($this->mergeable_keys_map[$key])) { |
||
| 193 | $cache[$key] = $cache[$key] + $fallback_cache[$key]; |
||
| 194 | } elseif (isset($this->mergeable_keys_list[$key])) { |
||
| 195 | $cache[$key] = array_merge($fallback_cache[$key], $cache[$key]); |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | $cache[$key] = $fallback_cache[$key]; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // save to cache for later retrieval |
||
| 204 | $this->cache[$code] = $cache; |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | } |
||
| 210 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.