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 |
||
58 | class Translations extends \ArrayObject |
||
59 | { |
||
60 | const HEADER_LANGUAGE = 'Language'; |
||
61 | const HEADER_PLURAL = 'Plural-Forms'; |
||
62 | const HEADER_DOMAIN = 'X-Domain'; |
||
63 | |||
64 | public static $insertDate = true; |
||
65 | |||
66 | private $headers; |
||
67 | |||
68 | /** |
||
69 | * @see \ArrayObject::__construct() |
||
70 | */ |
||
71 | public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator') |
||
90 | |||
91 | /** |
||
92 | * Magic method to create new instances using extractors |
||
93 | * For example: Translations::fromMoFile($filename, $options);. |
||
94 | * |
||
95 | * @return Translations |
||
96 | */ |
||
97 | public static function __callStatic($name, $arguments) |
||
105 | |||
106 | /** |
||
107 | * Magic method to import/export the translations to a specific format |
||
108 | * For example: $translations->toMoFile($filename, $options); |
||
109 | * For example: $translations->addFromMoFile($filename, $options);. |
||
110 | * |
||
111 | * @return self|bool |
||
112 | */ |
||
113 | public function __call($name, $arguments) |
||
135 | |||
136 | /** |
||
137 | * Magic method to clone each translation on clone the translations object. |
||
138 | */ |
||
139 | public function __clone() |
||
149 | |||
150 | /** |
||
151 | * Control the new translations added. |
||
152 | * |
||
153 | * @param mixed $index |
||
154 | * @param Translation $value |
||
155 | * |
||
156 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
157 | * |
||
158 | * @return Translation |
||
159 | */ |
||
160 | public function offsetSet($index, $value) |
||
178 | |||
179 | /** |
||
180 | * Set the plural definition. |
||
181 | * |
||
182 | * @param int $count |
||
183 | * @param string $rule |
||
184 | * |
||
185 | * @return self |
||
186 | */ |
||
187 | public function setPluralForms($count, $rule) |
||
193 | |||
194 | /** |
||
195 | * Returns the parsed plural definition. |
||
196 | * |
||
197 | * @param null|array [count, rule] |
||
198 | */ |
||
199 | public function getPluralForms() |
||
207 | |||
208 | /** |
||
209 | * Set a new header. |
||
210 | * |
||
211 | * @param string $name |
||
212 | * @param string $value |
||
213 | * |
||
214 | * @return self |
||
215 | */ |
||
216 | public function setHeader($name, $value) |
||
223 | |||
224 | /** |
||
225 | * Returns a header value. |
||
226 | * |
||
227 | * @param string $name |
||
228 | * |
||
229 | * @return null|string |
||
230 | */ |
||
231 | public function getHeader($name) |
||
235 | |||
236 | /** |
||
237 | * Returns all header for this translations (in alphabetic order). |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | public function getHeaders() |
||
247 | |||
248 | /** |
||
249 | * Removes all headers. |
||
250 | * |
||
251 | * @return self |
||
252 | */ |
||
253 | public function deleteHeaders() |
||
259 | |||
260 | /** |
||
261 | * Removes one header. |
||
262 | * |
||
263 | * @param string $name |
||
264 | * |
||
265 | * @return self |
||
266 | */ |
||
267 | public function deleteHeader($name) |
||
273 | |||
274 | /** |
||
275 | * Returns the language value. |
||
276 | * |
||
277 | * @return string $language |
||
278 | */ |
||
279 | public function getLanguage() |
||
283 | |||
284 | /** |
||
285 | * Sets the language and the plural forms. |
||
286 | * |
||
287 | * @param string $language |
||
288 | * |
||
289 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
290 | * |
||
291 | * @return self |
||
292 | */ |
||
293 | public function setLanguage($language) |
||
303 | |||
304 | /** |
||
305 | * Checks whether the language is empty or not. |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function hasLanguage() |
||
315 | |||
316 | /** |
||
317 | * Set a new domain for this translations. |
||
318 | * |
||
319 | * @param string $domain |
||
320 | * |
||
321 | * @return self |
||
322 | */ |
||
323 | public function setDomain($domain) |
||
329 | |||
330 | /** |
||
331 | * Returns the domain. |
||
332 | * |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getDomain() |
||
339 | |||
340 | /** |
||
341 | * Checks whether the domain is empty or not. |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function hasDomain() |
||
351 | |||
352 | /** |
||
353 | * Search for a specific translation. |
||
354 | * |
||
355 | * @param string|Translation $context The context of the translation or a translation instance |
||
356 | * @param string $original The original string |
||
357 | * |
||
358 | * @return Translation|false |
||
359 | */ |
||
360 | public function find($context, $original = '') |
||
370 | |||
371 | /** |
||
372 | * Creates and insert/merges a new translation. |
||
373 | * |
||
374 | * @param string $context The translation context |
||
375 | * @param string $original The translation original string |
||
376 | * @param string $plural The translation original plural string |
||
377 | * |
||
378 | * @return Translation The translation created |
||
379 | */ |
||
380 | public function insert($context, $original, $plural = '') |
||
384 | |||
385 | /** |
||
386 | * Merges this translations with other translations. |
||
387 | * |
||
388 | * @param Translations $translations The translations instance to merge with |
||
389 | * @param int $options |
||
390 | * |
||
391 | * @return self |
||
392 | */ |
||
393 | public function mergeWith(Translations $translations, $options = Merge::DEFAULT) |
||
399 | |||
400 | /** |
||
401 | * Merge the headers of two translations |
||
402 | * |
||
403 | * @param Translations $translations |
||
404 | * @param int $options |
||
405 | * |
||
406 | * @return self |
||
407 | */ |
||
408 | public function mergeHeadersWith(Translations $translations, $options = Merge::DEFAULT) |
||
455 | |||
456 | /** |
||
457 | * Merge the translations of two translations |
||
458 | * |
||
459 | * @param Translations $translations |
||
460 | * @param int $options |
||
461 | * |
||
462 | * @return self |
||
463 | */ |
||
464 | public function mergeTranslationsWith(Translations $translations, $options = Merge::DEFAULT) |
||
488 | } |
||
489 |