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 |
||
| 12 | class Translations extends \ArrayObject |
||
| 13 | { |
||
| 14 | const MERGE_ADD = 1; |
||
| 15 | const MERGE_REMOVE = 2; |
||
| 16 | const MERGE_HEADERS = 4; |
||
| 17 | const MERGE_REFERENCES = 8; |
||
| 18 | const MERGE_COMMENTS = 16; |
||
| 19 | const MERGE_LANGUAGE = 32; |
||
| 20 | const MERGE_PLURAL = 64; |
||
| 21 | const MERGE_OVERRIDE = 128; |
||
| 22 | |||
| 23 | const HEADER_LANGUAGE = 'Language'; |
||
| 24 | const HEADER_PLURAL = 'Plural-Forms'; |
||
| 25 | const HEADER_DOMAIN = 'X-Domain'; |
||
| 26 | |||
| 27 | public static $mergeDefault = 221; // self::MERGE_ADD | self::MERGE_OVERRIDE | self::MERGE_HEADERS | self::MERGE_COMMENTS | self::MERGE_REFERENCES | self::MERGE_PLURAL |
||
| 28 | public static $insertDate = true; |
||
| 29 | |||
| 30 | private $headers; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @see \ArrayObject::__construct() |
||
| 34 | */ |
||
| 35 | public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator') |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Magic method to create new instances using extractors |
||
| 57 | * For example: Translations::fromMoFile($filename, $options);. |
||
| 58 | * |
||
| 59 | * @return Translations |
||
| 60 | */ |
||
| 61 | public static function __callStatic($name, $arguments) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Magic method to import/export the translations to a specific format |
||
| 72 | * For example: $translations->toMoFile($filename, $options); |
||
| 73 | * For example: $translations->addFromMoFile($filename, $options);. |
||
| 74 | * |
||
| 75 | * @return self|bool |
||
| 76 | */ |
||
| 77 | public function __call($name, $arguments) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Magic method to clone each translation on clone the translations object. |
||
| 102 | */ |
||
| 103 | public function __clone() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Control the new translations added. |
||
| 116 | * |
||
| 117 | * @param mixed $index |
||
| 118 | * @param Translation $value |
||
| 119 | * |
||
| 120 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
| 121 | * |
||
| 122 | * @return Translation |
||
| 123 | */ |
||
| 124 | public function offsetSet($index, $value) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set the plural definition. |
||
| 145 | * |
||
| 146 | * @param int $count |
||
| 147 | * @param string $rule |
||
| 148 | * |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | public function setPluralForms($count, $rule) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns the parsed plural definition. |
||
| 160 | * |
||
| 161 | * @param null|array [count, rule] |
||
| 162 | */ |
||
| 163 | public function getPluralForms() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set a new header. |
||
| 174 | * |
||
| 175 | * @param string $name |
||
| 176 | * @param string $value |
||
| 177 | * |
||
| 178 | * @return self |
||
| 179 | */ |
||
| 180 | public function setHeader($name, $value) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a header value. |
||
| 190 | * |
||
| 191 | * @param string $name |
||
| 192 | * |
||
| 193 | * @return null|string |
||
| 194 | */ |
||
| 195 | public function getHeader($name) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns all header for this translations. |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function getHeaders() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Removes all headers. |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | public function deleteHeaders() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Removes one header. |
||
| 224 | * |
||
| 225 | * @param string $name |
||
| 226 | * |
||
| 227 | * @return self |
||
| 228 | */ |
||
| 229 | public function deleteHeader($name) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the language value. |
||
| 238 | * |
||
| 239 | * @return string $language |
||
| 240 | */ |
||
| 241 | public function getLanguage() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sets the language and the plural forms. |
||
| 248 | * |
||
| 249 | * @param string $language |
||
| 250 | * |
||
| 251 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
| 252 | * |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | public function setLanguage($language) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Checks whether the language is empty or not. |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function hasLanguage() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set a new domain for this translations. |
||
| 280 | * |
||
| 281 | * @param string $domain |
||
| 282 | * |
||
| 283 | * @return self |
||
| 284 | */ |
||
| 285 | public function setDomain($domain) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the domain. |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getDomain() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Checks whether the domain is empty or not. |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function hasDomain() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Search for a specific translation. |
||
| 316 | * |
||
| 317 | * @param string|Translation $context The context of the translation or a translation instance |
||
| 318 | * @param string $original The original string |
||
| 319 | * |
||
| 320 | * @return Translation|false |
||
| 321 | */ |
||
| 322 | public function find($context, $original = '') |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Creates and insert/merges a new translation. |
||
| 335 | * |
||
| 336 | * @param string $context The translation context |
||
| 337 | * @param string $original The translation original string |
||
| 338 | * @param string $plural The translation original plural string |
||
| 339 | * |
||
| 340 | * @return Translation The translation created |
||
| 341 | */ |
||
| 342 | public function insert($context, $original, $plural = '') |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Merges this translations with other translations. |
||
| 349 | * |
||
| 350 | * @param Translations $translations The translations instance to merge with |
||
| 351 | * @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations |
||
| 352 | * |
||
| 353 | * @return self |
||
| 354 | */ |
||
| 355 | public function mergeWith(Translations $translations, $method = null) |
||
| 410 | } |
||
| 411 |