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:
| 1 | <?php |
||
| 27 | class Loader |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Loader instance. |
||
| 31 | * |
||
| 32 | * @static |
||
| 33 | * @var Loader |
||
| 34 | */ |
||
| 35 | private static $_instance; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Default gettext domain to use. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $default_domain = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Configured locale. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $locale = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Loaded domains. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $domains = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Bound paths for domains. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $paths = ['' => './']; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns the singleton Loader object. |
||
| 67 | * |
||
| 68 | * @return Loader object |
||
| 69 | */ |
||
| 70 | 5 | public static function getInstance() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Loads global localizaton functions. |
||
| 81 | */ |
||
| 82 | 2 | public static function loadFunctions() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Figure out all possible locale names and start with the most |
||
| 89 | * specific ones. I.e. for sr_CS.UTF-8@latin, look through all of |
||
| 90 | * sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr. |
||
| 91 | * |
||
| 92 | * @param string $locale Locale code |
||
| 93 | * |
||
| 94 | * @return array list of locales to try for any POSIX-style locale specification |
||
| 95 | */ |
||
| 96 | 20 | public static function listLocales($locale) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Returns Translator object for domain or for default domain. |
||
| 152 | * |
||
| 153 | 10 | * @param string $domain Translation domain |
|
| 154 | 6 | * |
|
| 155 | * @return Translator |
||
| 156 | */ |
||
| 157 | 10 | public function getTranslator($domain = '') |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Sets the path for a domain. |
||
| 194 | 10 | * |
|
| 195 | 10 | * @param string $domain Domain name |
|
| 196 | * @param string $path Path where to find locales |
||
| 197 | */ |
||
| 198 | public function bindtextdomain($domain, $path) |
||
| 202 | |||
| 203 | /** |
||
| 204 | 10 | * Sets the default domain. |
|
| 205 | 10 | * |
|
| 206 | * @param string $domain Domain name |
||
| 207 | */ |
||
| 208 | public function textdomain($domain) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Sets a requested locale. |
||
| 215 | * |
||
| 216 | 10 | * @param string $locale Locale name |
|
| 217 | 10 | * |
|
| 218 | * @return string Set or current locale |
||
| 219 | */ |
||
| 220 | 10 | public function setlocale($locale) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Detects currently configured locale. |
||
| 231 | * |
||
| 232 | * It checks: |
||
| 233 | * |
||
| 234 | * - global lang variable |
||
| 235 | 2 | * - environment for LC_ALL, LC_MESSAGES and LANG |
|
| 236 | 1 | * |
|
| 237 | 1 | * @return string with locale name |
|
| 238 | 1 | */ |
|
| 239 | 1 | public function detectlocale() |
|
| 253 | } |
||
| 254 |
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: