| Total Complexity | 60 |
| Total Lines | 407 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like gettext_reader 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.
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 gettext_reader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class gettext_reader { |
||
| 37 | //public: |
||
| 38 | var $error = 0; // public variable that holds error code (0 if no error) |
||
| 39 | |||
| 40 | //private: |
||
| 41 | var $BYTEORDER = 0; // 0: low endian, 1: big endian |
||
| 42 | var $STREAM = null; |
||
| 43 | var $short_circuit = false; |
||
| 44 | var $enable_cache = false; |
||
| 45 | var $originals = null; // offset of original table |
||
| 46 | var $translations = null; // offset of translation table |
||
| 47 | var $pluralheader = null; // cache header field for plural forms |
||
| 48 | var $total = 0; // total string count |
||
| 49 | var $table_originals = null; // table for original strings (offsets) |
||
| 50 | var $table_translations = null; // table for translated strings (offsets) |
||
| 51 | var $cache_translations = null; // original -> translation mapping |
||
| 52 | |||
| 53 | |||
| 54 | /* Methods */ |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Reads a 32bit Integer from the Stream |
||
| 59 | * |
||
| 60 | * @access private |
||
| 61 | * @return Integer from the Stream |
||
| 62 | */ |
||
| 63 | function readint() { |
||
|
|
|||
| 64 | if ($this->BYTEORDER == 0) { |
||
| 65 | // low endian |
||
| 66 | $input = unpack('V', $this->STREAM->read(4)); |
||
| 67 | return array_shift($input); |
||
| 68 | } else { |
||
| 69 | // big endian |
||
| 70 | $input = unpack('N', $this->STREAM->read(4)); |
||
| 71 | return array_shift($input); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | function read($bytes) { |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Reads an array of Integers from the Stream |
||
| 81 | * |
||
| 82 | * @param int count How many elements should be read |
||
| 83 | * @return Array of Integers |
||
| 84 | */ |
||
| 85 | function readintarray($count) { |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Constructor |
||
| 97 | * |
||
| 98 | * @param object Reader the StreamReader object |
||
| 99 | * @param boolean enable_cache Enable or disable caching of strings (default on) |
||
| 100 | */ |
||
| 101 | function __construct($Reader, $enable_cache = true) { |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Loads the translation tables from the MO file into the cache |
||
| 135 | * If caching is enabled, also loads all strings into a cache |
||
| 136 | * to speed up translation lookups |
||
| 137 | * |
||
| 138 | * @access private |
||
| 139 | */ |
||
| 140 | function load_tables() { |
||
| 141 | if (is_array($this->cache_translations) && |
||
| 142 | is_array($this->table_originals) && |
||
| 143 | is_array($this->table_translations)) { |
||
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | /* get original and translations tables */ |
||
| 148 | if (!is_array($this->table_originals)) { |
||
| 149 | $this->STREAM->seekto($this->originals); |
||
| 150 | $this->table_originals = $this->readintarray($this->total * 2); |
||
| 151 | } |
||
| 152 | if (!is_array($this->table_translations)) { |
||
| 153 | $this->STREAM->seekto($this->translations); |
||
| 154 | $this->table_translations = $this->readintarray($this->total * 2); |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($this->enable_cache) { |
||
| 158 | $this->cache_translations = array(); |
||
| 159 | /* read all strings in the cache */ |
||
| 160 | for ($i = 0; $i < $this->total; $i++) { |
||
| 161 | $this->STREAM->seekto($this->table_originals[$i * 2 + 2]); |
||
| 162 | $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]); |
||
| 163 | $this->STREAM->seekto($this->table_translations[$i * 2 + 2]); |
||
| 164 | $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]); |
||
| 165 | $this->cache_translations[$original] = $translation; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns a string from the "originals" table |
||
| 172 | * |
||
| 173 | * @access private |
||
| 174 | * @param int num Offset number of original string |
||
| 175 | * @return string Requested string if found, otherwise '' |
||
| 176 | */ |
||
| 177 | function get_original_string($num) { |
||
| 178 | $length = $this->table_originals[$num * 2 + 1]; |
||
| 179 | $offset = $this->table_originals[$num * 2 + 2]; |
||
| 180 | if (!$length) { |
||
| 181 | return ''; |
||
| 182 | } |
||
| 183 | $this->STREAM->seekto($offset); |
||
| 184 | $data = $this->STREAM->read($length); |
||
| 185 | return (string) $data; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a string from the "translations" table |
||
| 190 | * |
||
| 191 | * @access private |
||
| 192 | * @param int num Offset number of original string |
||
| 193 | * @return string Requested string if found, otherwise '' |
||
| 194 | */ |
||
| 195 | function get_translation_string($num) { |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Binary search for string |
||
| 208 | * |
||
| 209 | * @access private |
||
| 210 | * @param string string |
||
| 211 | * @param int start (internally used in recursive function) |
||
| 212 | * @param int end (internally used in recursive function) |
||
| 213 | * @return int string number (offset in originals table) |
||
| 214 | */ |
||
| 215 | function find_string($string, $start = -1, $end = -1) { |
||
| 216 | if (($start == -1) or ($end == -1)) { |
||
| 217 | // find_string is called with only one parameter, set start end end |
||
| 218 | $start = 0; |
||
| 219 | $end = $this->total; |
||
| 220 | } |
||
| 221 | if (abs($start - $end) <= 1) { |
||
| 222 | // We're done, now we either found the string, or it doesn't exist |
||
| 223 | $txt = $this->get_original_string($start); |
||
| 224 | if ($string == $txt) { |
||
| 225 | return $start; |
||
| 226 | } else { |
||
| 227 | return -1; |
||
| 228 | } |
||
| 229 | } else if ($start > $end) { |
||
| 230 | // start > end -> turn around and start over |
||
| 231 | return $this->find_string($string, $end, $start); |
||
| 232 | } else { |
||
| 233 | // Divide table in two parts |
||
| 234 | $half = (int) (($start + $end) / 2); |
||
| 235 | $cmp = strcmp($string, $this->get_original_string($half)); |
||
| 236 | if ($cmp == 0) { |
||
| 237 | // string is exactly in the middle => return it |
||
| 238 | return $half; |
||
| 239 | } else if ($cmp < 0) { |
||
| 240 | // The string is in the upper half |
||
| 241 | return $this->find_string($string, $start, $half); |
||
| 242 | } else { |
||
| 243 | // The string is in the lower half |
||
| 244 | return $this->find_string($string, $half, $end); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Translates a string |
||
| 251 | * |
||
| 252 | * @access public |
||
| 253 | * @param string string to be translated |
||
| 254 | * @return string translated string (or original, if not found) |
||
| 255 | */ |
||
| 256 | function translate($string) { |
||
| 257 | if ($this->short_circuit) { |
||
| 258 | return $string; |
||
| 259 | } |
||
| 260 | $this->load_tables(); |
||
| 261 | |||
| 262 | if ($this->enable_cache) { |
||
| 263 | // Caching enabled, get translated string from cache |
||
| 264 | if (array_key_exists($string, $this->cache_translations)) { |
||
| 265 | return $this->cache_translations[$string]; |
||
| 266 | } else { |
||
| 267 | return $string; |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | // Caching not enabled, try to find string |
||
| 271 | $num = $this->find_string($string); |
||
| 272 | if ($num == -1) { |
||
| 273 | return $string; |
||
| 274 | } else { |
||
| 275 | return $this->get_translation_string($num); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Sanitize plural form expression for use in PHP eval call. |
||
| 282 | * |
||
| 283 | * @access private |
||
| 284 | * @return string sanitized plural form expression |
||
| 285 | */ |
||
| 286 | function sanitize_plural_expression($expr) { |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Parse full PO header and extract only plural forms line. |
||
| 317 | * |
||
| 318 | * @access private |
||
| 319 | * @return string verbatim plural form header field |
||
| 320 | */ |
||
| 321 | function extract_plural_forms_header_from_po_header($header) { |
||
| 322 | if (preg_match("/(^|\n)plural-forms: ([^\n]*)\n/i", $header, $regs)) { |
||
| 323 | $expr = $regs[2]; |
||
| 324 | } else { |
||
| 325 | $expr = "nplurals=2; plural=n == 1 ? 0 : 1;"; |
||
| 326 | } |
||
| 327 | return $expr; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get possible plural forms from MO header |
||
| 332 | * |
||
| 333 | * @access private |
||
| 334 | * @return string plural form header |
||
| 335 | */ |
||
| 336 | function get_plural_forms() { |
||
| 337 | // lets assume message number 0 is header |
||
| 338 | // this is true, right? |
||
| 339 | $this->load_tables(); |
||
| 340 | |||
| 341 | // cache header field for plural forms |
||
| 342 | if (!is_string($this->pluralheader)) { |
||
| 343 | if ($this->enable_cache) { |
||
| 344 | $header = $this->cache_translations[""]; |
||
| 345 | } else { |
||
| 346 | $header = $this->get_translation_string(0); |
||
| 347 | } |
||
| 348 | $expr = $this->extract_plural_forms_header_from_po_header($header); |
||
| 349 | $this->pluralheader = $this->sanitize_plural_expression($expr); |
||
| 350 | } |
||
| 351 | return $this->pluralheader; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Detects which plural form to take |
||
| 356 | * |
||
| 357 | * @access private |
||
| 358 | * @param n count |
||
| 359 | * @return int array index of the right plural form |
||
| 360 | */ |
||
| 361 | function select_string($n) { |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Plural version of gettext |
||
| 383 | * |
||
| 384 | * @access public |
||
| 385 | * @param string single |
||
| 386 | * @param string plural |
||
| 387 | * @param string number |
||
| 388 | * @return translated plural form |
||
| 389 | */ |
||
| 390 | function ngettext($single, $plural, $number) { |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | function pgettext($context, $msgid) { |
||
| 427 | $key = $context.chr(4).$msgid; |
||
| 428 | $ret = $this->translate($key); |
||
| 429 | if (strpos($ret, "\004") !== false) { |
||
| 430 | return $msgid; |
||
| 431 | } else { |
||
| 432 | return $ret; |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | function npgettext($context, $singular, $plural, $number) { |
||
| 443 | } |
||
| 444 | |||
| 445 | } |
||
| 449 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.