Complex classes like Lang 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 Lang, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Lang |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Array of transliterations |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | private static $_transliterations; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Code of active language |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private static $_lang; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Context information of all available languages |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private static $_languages; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Array of localized strings |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private static $_dictionary; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Array of localized date and time strings |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $_datetime_dictionary; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the current dictionary |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | * Return the dictionary |
||
| 46 | */ |
||
| 47 | public static function Dictionary() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get a list of either enabled or disabled languages. Example: |
||
| 54 | * |
||
| 55 | * array( |
||
| 56 | * [...] |
||
| 57 | * |
||
| 58 | * 'en' => array( |
||
| 59 | * 'name' => 'English', |
||
| 60 | * 'handle' => 'english', |
||
| 61 | * 'extensions' => array() |
||
| 62 | * ), |
||
| 63 | * |
||
| 64 | * 'it' => array( |
||
| 65 | * 'name' => 'Italiano', |
||
| 66 | * 'handle' => 'italian', |
||
| 67 | * 'extensions' => array( |
||
| 68 | * [...] |
||
| 69 | * ) |
||
| 70 | * ), |
||
| 71 | * |
||
| 72 | * [...] |
||
| 73 | * ) |
||
| 74 | * |
||
| 75 | * @see toolkit.Lang#createLanguage() |
||
| 76 | * @since Symphony 2.3 |
||
| 77 | * @return array |
||
| 78 | * Return an array of languages (both enabled and disabled) |
||
| 79 | */ |
||
| 80 | public static function Languages() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get transliterations |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | * Returns the transliterations array |
||
| 90 | */ |
||
| 91 | public static function Transliterations() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Initialize transliterations, datetime dictionary and languages array. |
||
| 98 | */ |
||
| 99 | public static function initialize() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Create an array of Language information for internal use. |
||
| 128 | * |
||
| 129 | * @since Symphony 2.3 |
||
| 130 | * @param string $code |
||
| 131 | * Language code, e. g. 'en' or 'pt-br' |
||
| 132 | * @param string $name |
||
| 133 | * Language name |
||
| 134 | * @param string $handle (optional) |
||
| 135 | * Handle for the given language, used to build a valid 'lang_$handle' extension's handle. |
||
| 136 | * Defaults to null. |
||
| 137 | * @param array $extensions (optional) |
||
| 138 | * An array of extensions that support the given language. |
||
| 139 | * @return array |
||
| 140 | * An array of Language information. |
||
| 141 | */ |
||
| 142 | private static function createLanguage($code, $name, $handle = null, array $extensions = array()) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Fetch all languages available in the core language folder and the language extensions. |
||
| 155 | * The function stores all language information in the private variable `$_languages`. |
||
| 156 | * It contains an array with the name and handle of each language and an array of all |
||
| 157 | * extensions available in that language. |
||
| 158 | * |
||
| 159 | * @throws UnexpectedValueException |
||
| 160 | * @throws RuntimeException |
||
| 161 | */ |
||
| 162 | private static function fetch() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set system language, load translations for core and extensions. If the specified language |
||
| 242 | * cannot be found, Symphony will default to English. |
||
| 243 | * |
||
| 244 | * Note: Beginning with Symphony 2.2 translations bundled with extensions will only be loaded |
||
| 245 | * when the core dictionary of the specific language is available. |
||
| 246 | * |
||
| 247 | * @param string $code |
||
| 248 | * Language code, e. g. 'en' or 'pt-br' |
||
| 249 | * @param boolean $checkStatus (optional) |
||
| 250 | * If false, set the language even if it's not enabled. Defaults to true. |
||
| 251 | */ |
||
| 252 | public static function set($code, $checkStatus = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Given a valid language code, this function checks if the language is enabled. |
||
| 300 | * |
||
| 301 | * @since Symphony 2.3 |
||
| 302 | * @param string $code |
||
| 303 | * Language code, e. g. 'en' or 'pt-br' |
||
| 304 | * @return boolean |
||
| 305 | * If true, the language is enabled. |
||
| 306 | */ |
||
| 307 | public static function isLanguageEnabled($code) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Load language file. Each language file contains three arrays: |
||
| 326 | * about, dictionary and transliterations. |
||
| 327 | * |
||
| 328 | * @param string $path |
||
| 329 | * Path of the language file that should be loaded |
||
| 330 | */ |
||
| 331 | private static function load($path) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get current language |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public static function get() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * This function is an internal alias for `__()`. |
||
| 361 | * |
||
| 362 | * @since Symphony 2.3 |
||
| 363 | * @see toolkit.__() |
||
| 364 | * @param string $string |
||
| 365 | * The string that should be translated |
||
| 366 | * @param array $inserts (optional) |
||
| 367 | * Optional array used to replace translation placeholders, defaults to NULL |
||
| 368 | * @param string $namespace (optional) |
||
| 369 | * Optional string used to define the namespace, defaults to NULL. |
||
| 370 | * @return string |
||
| 371 | * Returns the translated string |
||
| 372 | */ |
||
| 373 | public static function translate($string, array $inserts = null, $namespace = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get an array of the codes and names of all languages that are available system wide. |
||
| 399 | * |
||
| 400 | * Note: Beginning with Symphony 2.2 language files are only available |
||
| 401 | * when the language extension is explicitly enabled. |
||
| 402 | * |
||
| 403 | * @param boolean $checkStatus (optional) |
||
| 404 | * If false, retrieves a list a languages that support core translation. |
||
| 405 | * @return array |
||
| 406 | * Returns an associative array of language codes and names, e. g. 'en' => 'English' |
||
| 407 | */ |
||
| 408 | public static function getAvailableLanguages($checkStatus = true) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Check if Symphony is localised. |
||
| 425 | * |
||
| 426 | * @return boolean |
||
| 427 | * Returns true for localized system, false for English system |
||
| 428 | */ |
||
| 429 | public static function isLocalized() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Localize dates. |
||
| 436 | * |
||
| 437 | * @param string $string |
||
| 438 | * Standard date that should be localized |
||
| 439 | * @return string |
||
| 440 | * Return the given date with translated month and day names |
||
| 441 | */ |
||
| 442 | public static function localizeDate($string) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Standardize dates. |
||
| 456 | * |
||
| 457 | * @param string $string |
||
| 458 | * Localized date that should be standardized |
||
| 459 | * @return string |
||
| 460 | * Returns the given date with English month and day names |
||
| 461 | */ |
||
| 462 | public static function standardizeDate($string) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Given a string, this will clean it for use as a Symphony handle. Preserves multi-byte characters. |
||
| 487 | * |
||
| 488 | * @param string $string |
||
| 489 | * String to be cleaned up |
||
| 490 | * @param integer $max_length |
||
| 491 | * The maximum number of characters in the handle |
||
| 492 | * @param string $delim |
||
| 493 | * All non-valid characters will be replaced with this |
||
| 494 | * @param boolean $uriencode |
||
| 495 | * Force the resultant string to be uri encoded making it safe for URLs |
||
| 496 | * @param boolean $apply_transliteration |
||
| 497 | * If true, this will run the string through an array of substitution characters |
||
| 498 | * @param array $additional_rule_set |
||
| 499 | * An array of REGEX patterns that should be applied to the `$string`. This |
||
| 500 | * occurs after the string has been trimmed and joined with the `$delim` |
||
| 501 | * @return string |
||
| 502 | * Returns resultant handle |
||
| 503 | */ |
||
| 504 | public static function createHandle($string, $max_length = 255, $delim = '-', $uriencode = false, $apply_transliteration = true, $additional_rule_set = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Given a string, this will clean it for use as a filename. Preserves multi-byte characters. |
||
| 516 | * |
||
| 517 | * @param string $string |
||
| 518 | * String to be cleaned up |
||
| 519 | * @param string $delim |
||
| 520 | * Replacement for invalid characters |
||
| 521 | * @param boolean $apply_transliteration |
||
| 522 | * If true, umlauts and special characters will be substituted |
||
| 523 | * @return string |
||
| 524 | * Returns created filename |
||
| 525 | */ |
||
| 526 | public static function createFilename($string, $delim='-', $apply_transliteration = true) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * This function replaces special characters according to the values stored inside |
||
| 539 | * `$_transliterations`. |
||
| 540 | * |
||
| 541 | * @since Symphony 2.3 |
||
| 542 | * @param string $string |
||
| 543 | * The string that should be cleaned-up |
||
| 544 | * @return mixed |
||
| 545 | * Returns the transliterated string |
||
| 546 | */ |
||
| 547 | private static function applyTransliterations($string) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns boolean if PHP has been compiled with unicode support. This is |
||
| 562 | * useful to determine if unicode modifier's can be used in regular expression's |
||
| 563 | * |
||
| 564 | * @link http://stackoverflow.com/questions/4509576/detect-if-pcre-was-built-without-the-enable-unicode-properties-or-enable-utf8 |
||
| 565 | * @since Symphony 2.2.2 |
||
| 566 | * @return boolean |
||
| 567 | */ |
||
| 568 | public static function isUnicodeCompiled() |
||
| 572 | } |
||
| 573 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.