Complex classes like Translations 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 Translations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 100 | class Translations extends ArrayObject |
||
| 101 | { |
||
| 102 | const HEADER_LANGUAGE = 'Language'; |
||
| 103 | const HEADER_PLURAL = 'Plural-Forms'; |
||
| 104 | const HEADER_DOMAIN = 'X-Domain'; |
||
| 105 | |||
| 106 | public static $options = [ |
||
| 107 | 'defaultHeaders' => [ |
||
| 108 | 'Project-Id-Version' => '', |
||
| 109 | 'Report-Msgid-Bugs-To' => '', |
||
| 110 | 'Last-Translator' => '', |
||
| 111 | 'Language-Team' => '', |
||
| 112 | 'MIME-Version' => '1.0', |
||
| 113 | 'Content-Type' => 'text/plain; charset=UTF-8', |
||
| 114 | 'Content-Transfer-Encoding' => '8bit', |
||
| 115 | ], |
||
| 116 | 'headersSorting' => false, |
||
| 117 | 'defaultDateHeaders' => [ |
||
| 118 | 'POT-Creation-Date', |
||
| 119 | 'PO-Revision-Date', |
||
| 120 | ], |
||
| 121 | ]; |
||
| 122 | |||
| 123 | protected $headers; |
||
| 124 | |||
| 125 | protected $translationClass; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @see ArrayObject::__construct() |
||
| 129 | */ |
||
| 130 | public function __construct( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Magic method to create new instances using extractors |
||
| 151 | * For example: Translations::fromMoFile($filename, $options);. |
||
| 152 | * |
||
| 153 | * @return Translations |
||
| 154 | */ |
||
| 155 | public static function __callStatic($name, $arguments) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Magic method to import/export the translations to a specific format |
||
| 166 | * For example: $translations->toMoFile($filename, $options); |
||
| 167 | * For example: $translations->addFromMoFile($filename, $options);. |
||
| 168 | * |
||
| 169 | * @return self|bool |
||
| 170 | */ |
||
| 171 | public function __call($name, $arguments) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Magic method to clone each translation on clone the translations object. |
||
| 196 | */ |
||
| 197 | public function __clone() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Control the new translations added. |
||
| 210 | * |
||
| 211 | * @param mixed $index |
||
| 212 | * @param Translation $value |
||
| 213 | * |
||
| 214 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
| 215 | * |
||
| 216 | * @return Translation |
||
| 217 | */ |
||
| 218 | public function offsetSet($index, $value) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set the plural definition. |
||
| 241 | * |
||
| 242 | * @param int $count |
||
| 243 | * @param string $rule |
||
| 244 | * |
||
| 245 | * @return self |
||
| 246 | */ |
||
| 247 | public function setPluralForms($count, $rule) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns the parsed plural definition. |
||
| 259 | * |
||
| 260 | * @param null|array [count, rule] |
||
| 261 | */ |
||
| 262 | public function getPluralForms() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set a new header. |
||
| 275 | * |
||
| 276 | * @param string $name |
||
| 277 | * @param string $value |
||
| 278 | * |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | public function setHeader($name, $value) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Returns a header value. |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | * |
||
| 294 | * @return null|string |
||
| 295 | */ |
||
| 296 | public function getHeader($name) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns all header for this translations (in alphabetic order). |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function getHeaders() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Removes all headers. |
||
| 317 | * |
||
| 318 | * @return self |
||
| 319 | */ |
||
| 320 | public function deleteHeaders() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Removes one header. |
||
| 329 | * |
||
| 330 | * @param string $name |
||
| 331 | * |
||
| 332 | * @return self |
||
| 333 | */ |
||
| 334 | public function deleteHeader($name) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the language value. |
||
| 343 | * |
||
| 344 | * @return string $language |
||
| 345 | */ |
||
| 346 | public function getLanguage() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Sets the language and the plural forms. |
||
| 353 | * |
||
| 354 | * @param string $language |
||
| 355 | * |
||
| 356 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
| 357 | * |
||
| 358 | * @return self |
||
| 359 | */ |
||
| 360 | public function setLanguage($language) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Checks whether the language is empty or not. |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function hasLanguage() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set a new domain for this translations. |
||
| 385 | * |
||
| 386 | * @param string $domain |
||
| 387 | * |
||
| 388 | * @return self |
||
| 389 | */ |
||
| 390 | public function setDomain($domain) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the domain. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getDomain() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Checks whether the domain is empty or not. |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function hasDomain() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Search for a specific translation. |
||
| 421 | * |
||
| 422 | * @param string|Translation $context The context of the translation or a translation instance |
||
| 423 | * @param string $original The original string |
||
| 424 | * @warning Translations with custom identifiers (e.g. XLIFF unit IDs) cannot be found using this function. |
||
| 425 | * |
||
| 426 | * @return Translation|false |
||
| 427 | */ |
||
| 428 | public function find($context, $original = '') |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Count all elements translated |
||
| 441 | * |
||
| 442 | * @return integer |
||
| 443 | */ |
||
| 444 | public function countTranslated() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Creates and insert/merges a new translation. |
||
| 457 | * |
||
| 458 | * @param string $context The translation context |
||
| 459 | * @param string $original The translation original string |
||
| 460 | * @param string $plural The translation original plural string |
||
| 461 | * |
||
| 462 | * @return Translation The translation created |
||
| 463 | */ |
||
| 464 | public function insert($context, $original, $plural = '') |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Merges this translations with other translations. |
||
| 471 | * |
||
| 472 | * @param Translations $translations The translations instance to merge with |
||
| 473 | * @param int $options |
||
| 474 | * |
||
| 475 | * @return self |
||
| 476 | */ |
||
| 477 | public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Create a new instance of a Translation object. |
||
| 487 | * |
||
| 488 | * @param string $context The context of the translation |
||
| 489 | * @param string $original The original string |
||
| 490 | * @param string $plural The original plural string |
||
| 491 | * @return Translation New Translation instance |
||
| 492 | */ |
||
| 493 | public function createNewTranslation($context, $original, $plural = '') |
||
| 498 | } |
||
| 499 |