Complex classes like TGlobalization 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 TGlobalization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class TGlobalization extends \Prado\TModule |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Default character set is 'UTF-8'. |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $_defaultCharset = 'UTF-8'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Default culture is 'en'. |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $_defaultCulture = 'en'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The current charset. |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $_charset=null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The current culture. |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $_culture=null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Translation source parameters. |
||
| 58 | * @var TMap |
||
| 59 | */ |
||
| 60 | private $_translation; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var boolean whether we should translate the default culture |
||
| 64 | */ |
||
| 65 | private $_translateDefaultCulture=true; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Initialize the Culture and Charset for this application. |
||
| 69 | * You should override this method if you want a different way of |
||
| 70 | * setting the Culture and/or Charset for your application. |
||
| 71 | * If you override this method, call parent::init($xml) first. |
||
| 72 | * @param mixed application configuration |
||
| 73 | */ |
||
| 74 | public function init($config) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string default culture |
||
| 98 | */ |
||
| 99 | public function getTranslateDefaultCulture() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param bool default culture, e.g. <tt>en_US</tt> for American English |
||
| 106 | */ |
||
| 107 | public function setTranslateDefaultCulture($value) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string default culture |
||
| 114 | */ |
||
| 115 | public function getDefaultCulture() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string default culture, e.g. <tt>en_US</tt> for American English |
||
| 122 | */ |
||
| 123 | public function setDefaultCulture($culture) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return string default charset set |
||
| 130 | */ |
||
| 131 | public function getDefaultCharset() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string default localization charset, e.g. <tt>UTF-8</tt> |
||
| 138 | */ |
||
| 139 | public function setDefaultCharset($charset) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string current application culture |
||
| 146 | */ |
||
| 147 | public function getCulture() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string culture, e.g. <tt>en_US</tt> for American English |
||
| 154 | */ |
||
| 155 | public function setCulture($culture) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return string localization charset |
||
| 162 | */ |
||
| 163 | public function getCharset() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string localization charset, e.g. <tt>UTF-8</tt> |
||
| 170 | */ |
||
| 171 | public function setCharset($charset) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return TMap translation source configuration. |
||
| 178 | */ |
||
| 179 | public function getTranslationConfiguration() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets the translation configuration. Example configuration: |
||
| 188 | * <code> |
||
| 189 | * $config['type'] = 'XLIFF'; //XLIFF, gettext, PHP, Database or MySQL (deprecated) |
||
| 190 | * $config['source'] = 'Path.to.directory'; // for types XLIFF, PHP and gettext |
||
| 191 | * $config['source'] = 'connectionId'; // for type Database |
||
| 192 | * $config['source'] = 'mysql://user:pw@host/db'; // for type MySQL (deprecated) |
||
| 193 | * $config['catalogue'] = 'messages'; //default catalog |
||
| 194 | * $config['autosave'] = 'true'; //save untranslated message |
||
| 195 | * $config['cache'] = 'true'; //cache translated message |
||
| 196 | * $config['marker'] = '@@'; // surround untranslated text with '@@' |
||
| 197 | * </code> |
||
| 198 | * Throws exception is source is not found. |
||
| 199 | * @param TMap|array configuration options |
||
| 200 | */ |
||
| 201 | protected function setTranslationConfiguration($config) |
||
| 202 | { |
||
| 203 | if($config['type'] == 'XLIFF' || $config['type'] == 'gettext' || $config['type'] == 'PHP') |
||
| 204 | { |
||
| 205 | if($config['source']) |
||
| 206 | { |
||
| 207 | $config['source'] = Prado::getPathOfNamespace($config['source']); |
||
| 208 | if(!is_dir($config['source'])) |
||
| 209 | { |
||
| 210 | if(@mkdir($config['source'])===false) |
||
| 211 | throw new TConfigurationException('globalization_source_path_failed', |
||
| 212 | $config['source']); |
||
| 213 | chmod($config['source'], PRADO_CHMOD); //make it deletable |
||
| 214 | } |
||
| 215 | } |
||
| 216 | else |
||
| 217 | { |
||
| 218 | throw new TConfigurationException("invalid source dir '{$config['source']}'"); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | if(isset($config['cache']) && TPropertyValue::ensureBoolean($config['cache'])) |
||
| 222 | { |
||
| 223 | $config['cache'] = $this->getApplication()->getRunTimePath().'/i18n'; |
||
| 224 | if(!is_dir($config['cache'])) |
||
| 225 | { |
||
| 226 | if(@mkdir($config['cache'])===false) |
||
| 227 | throw new TConfigurationException('globalization_cache_path_failed', |
||
| 228 | $config['cache']); |
||
| 229 | chmod($config['cache'], PRADO_CHMOD); //make it deletable |
||
| 230 | } |
||
| 231 | } |
||
| 232 | else |
||
| 233 | { |
||
| 234 | unset($config['cache']); |
||
| 235 | } |
||
| 236 | $this->_translation = $config; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string current translation catalogue. |
||
| 241 | */ |
||
| 242 | public function getTranslationCatalogue() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string update the translation catalogue. |
||
| 249 | */ |
||
| 250 | public function setTranslationCatalogue($value) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Gets all the variants of a specific culture. If the parameter |
||
| 257 | * $culture is null, the current culture is used. |
||
| 258 | * @param string $culture the Culture string |
||
|
|
|||
| 259 | * @return array variants of the culture. |
||
| 260 | */ |
||
| 261 | public function getCultureVariants($culture=null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns a list of possible localized files. Example |
||
| 273 | * <code> |
||
| 274 | * $files = $app->getLocalizedResource("path/to/Home.page","en_US"); |
||
| 275 | * </code> |
||
| 276 | * will return |
||
| 277 | * <pre> |
||
| 278 | * array |
||
| 279 | * 0 => 'path/to/en_US/Home.page' |
||
| 280 | * 1 => 'path/to/en/Home.page' |
||
| 281 | * 2 => 'path/to/Home.en_US.page' |
||
| 282 | * 3 => 'path/to/Home.en.page' |
||
| 283 | * 4 => 'path/to/Home.page' |
||
| 284 | * </pre> |
||
| 285 | * Note that you still need to verify the existance of these files. |
||
| 286 | * @param string filename |
||
| 287 | * @param string culture string, null to use current culture |
||
| 288 | * @return array list of possible localized resource files. |
||
| 289 | */ |
||
| 290 | public function getLocalizedResource($file,$culture=null) |
||
| 303 | |||
| 304 | } |
||
| 305 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.