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 |
||
96 | class Translations extends ArrayObject |
||
97 | { |
||
98 | const HEADER_LANGUAGE = 'Language'; |
||
99 | const HEADER_PLURAL = 'Plural-Forms'; |
||
100 | const HEADER_DOMAIN = 'X-Domain'; |
||
101 | |||
102 | public static $options = [ |
||
103 | 'defaultHeaders' => [ |
||
104 | 'Project-Id-Version' => '', |
||
105 | 'Report-Msgid-Bugs-To' => '', |
||
106 | 'Last-Translator' => '', |
||
107 | 'Language-Team' => '', |
||
108 | 'MIME-Version' => '1.0', |
||
109 | 'Content-Type' => 'text/plain; charset=UTF-8', |
||
110 | 'Content-Transfer-Encoding' => '8bit', |
||
111 | ], |
||
112 | 'headersSorting' => false, |
||
113 | 'defaultDateHeaders' => [ |
||
114 | 'POT-Creation-Date', |
||
115 | 'PO-Revision-Date', |
||
116 | ], |
||
117 | ]; |
||
118 | |||
119 | private $headers; |
||
120 | |||
121 | /** |
||
122 | * @see ArrayObject::__construct() |
||
123 | */ |
||
124 | public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator') |
||
136 | |||
137 | /** |
||
138 | * Magic method to create new instances using extractors |
||
139 | * For example: Translations::fromMoFile($filename, $options);. |
||
140 | * |
||
141 | * @return Translations |
||
142 | */ |
||
143 | public static function __callStatic($name, $arguments) |
||
151 | |||
152 | /** |
||
153 | * Magic method to import/export the translations to a specific format |
||
154 | * For example: $translations->toMoFile($filename, $options); |
||
155 | * For example: $translations->addFromMoFile($filename, $options);. |
||
156 | * |
||
157 | * @return self|bool |
||
158 | */ |
||
159 | public function __call($name, $arguments) |
||
181 | |||
182 | /** |
||
183 | * Magic method to clone each translation on clone the translations object. |
||
184 | */ |
||
185 | public function __clone() |
||
195 | |||
196 | /** |
||
197 | * Control the new translations added. |
||
198 | * |
||
199 | * @param mixed $index |
||
200 | * @param Translation $value |
||
201 | * |
||
202 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
203 | * |
||
204 | * @return Translation |
||
205 | */ |
||
206 | public function offsetSet($index, $value) |
||
226 | |||
227 | /** |
||
228 | * Set the plural definition. |
||
229 | * |
||
230 | * @param int $count |
||
231 | * @param string $rule |
||
232 | * |
||
233 | * @return self |
||
234 | */ |
||
235 | public function setPluralForms($count, $rule) |
||
241 | |||
242 | /** |
||
243 | * Returns the parsed plural definition. |
||
244 | * |
||
245 | * @param null|array [count, rule] |
||
246 | */ |
||
247 | public function getPluralForms() |
||
257 | |||
258 | /** |
||
259 | * Set a new header. |
||
260 | * |
||
261 | * @param string $name |
||
262 | * @param string $value |
||
263 | * |
||
264 | * @return self |
||
265 | */ |
||
266 | public function setHeader($name, $value) |
||
273 | |||
274 | /** |
||
275 | * Returns a header value. |
||
276 | * |
||
277 | * @param string $name |
||
278 | * |
||
279 | * @return null|string |
||
280 | */ |
||
281 | public function getHeader($name) |
||
285 | |||
286 | /** |
||
287 | * Returns all header for this translations (in alphabetic order). |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getHeaders() |
||
299 | |||
300 | /** |
||
301 | * Removes all headers. |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | public function deleteHeaders() |
||
311 | |||
312 | /** |
||
313 | * Removes one header. |
||
314 | * |
||
315 | * @param string $name |
||
316 | * |
||
317 | * @return self |
||
318 | */ |
||
319 | public function deleteHeader($name) |
||
325 | |||
326 | /** |
||
327 | * Returns the language value. |
||
328 | * |
||
329 | * @return string $language |
||
330 | */ |
||
331 | public function getLanguage() |
||
335 | |||
336 | /** |
||
337 | * Sets the language and the plural forms. |
||
338 | * |
||
339 | * @param string $language |
||
340 | * |
||
341 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
342 | * |
||
343 | * @return self |
||
344 | */ |
||
345 | public function setLanguage($language) |
||
355 | |||
356 | /** |
||
357 | * Checks whether the language is empty or not. |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function hasLanguage() |
||
367 | |||
368 | /** |
||
369 | * Set a new domain for this translations. |
||
370 | * |
||
371 | * @param string $domain |
||
372 | * |
||
373 | * @return self |
||
374 | */ |
||
375 | public function setDomain($domain) |
||
381 | |||
382 | /** |
||
383 | * Returns the domain. |
||
384 | * |
||
385 | * @return string |
||
386 | */ |
||
387 | public function getDomain() |
||
391 | |||
392 | /** |
||
393 | * Checks whether the domain is empty or not. |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | public function hasDomain() |
||
403 | |||
404 | /** |
||
405 | * Search for a specific translation. |
||
406 | * |
||
407 | * @param string|Translation $context The context of the translation or a translation instance |
||
408 | * @param string $original The original string |
||
409 | * |
||
410 | * @return Translation|false |
||
411 | */ |
||
412 | public function find($context, $original = '') |
||
422 | |||
423 | /** |
||
424 | * Count all elements translated |
||
425 | * |
||
426 | * @return integer |
||
427 | */ |
||
428 | public function countTranslated() |
||
436 | |||
437 | /** |
||
438 | * Creates and insert/merges a new translation. |
||
439 | * |
||
440 | * @param string $context The translation context |
||
441 | * @param string $original The translation original string |
||
442 | * @param string $plural The translation original plural string |
||
443 | * |
||
444 | * @return Translation The translation created |
||
445 | */ |
||
446 | public function insert($context, $original, $plural = '') |
||
450 | |||
451 | /** |
||
452 | * Merges this translations with other translations. |
||
453 | * |
||
454 | * @param Translations $translations The translations instance to merge with |
||
455 | * @param int $options |
||
456 | * |
||
457 | * @return self |
||
458 | */ |
||
459 | public function mergeWith(Translations $translations, $options = Merge::DEFAULTS) |
||
466 | } |
||
467 |