Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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_MINES = 8; |
||
17 | const MERGE_HEADERS_THEIRS = 16; |
||
18 | const MERGE_LANGUAGE_OVERRIDE = 32; |
||
19 | const MERGE_DOMAIN_OVERRIDE = 64; |
||
20 | |||
21 | const HEADER_LANGUAGE = 'Language'; |
||
22 | const HEADER_PLURAL = 'Plural-Forms'; |
||
23 | const HEADER_DOMAIN = 'X-Domain'; |
||
24 | |||
25 | public static $insertDate = true; |
||
26 | |||
27 | private $headers; |
||
28 | |||
29 | /** |
||
30 | * @see \ArrayObject::__construct() |
||
31 | */ |
||
32 | public function __construct($input = [], $flags = 0, $iterator_class = 'ArrayIterator') |
||
51 | |||
52 | /** |
||
53 | * Magic method to create new instances using extractors |
||
54 | * For example: Translations::fromMoFile($filename, $options);. |
||
55 | * |
||
56 | * @return Translations |
||
57 | */ |
||
58 | public static function __callStatic($name, $arguments) |
||
66 | |||
67 | /** |
||
68 | * Magic method to import/export the translations to a specific format |
||
69 | * For example: $translations->toMoFile($filename, $options); |
||
70 | * For example: $translations->addFromMoFile($filename, $options);. |
||
71 | * |
||
72 | * @return self|bool |
||
73 | */ |
||
74 | public function __call($name, $arguments) |
||
96 | |||
97 | /** |
||
98 | * Magic method to clone each translation on clone the translations object. |
||
99 | */ |
||
100 | public function __clone() |
||
110 | |||
111 | /** |
||
112 | * Control the new translations added. |
||
113 | * |
||
114 | * @param mixed $index |
||
115 | * @param Translation $value |
||
116 | * |
||
117 | * @throws InvalidArgumentException If the value is not an instance of Gettext\Translation |
||
118 | * |
||
119 | * @return Translation |
||
120 | */ |
||
121 | public function offsetSet($index, $value) |
||
139 | |||
140 | /** |
||
141 | * Set the plural definition. |
||
142 | * |
||
143 | * @param int $count |
||
144 | * @param string $rule |
||
145 | * |
||
146 | * @return self |
||
147 | */ |
||
148 | public function setPluralForms($count, $rule) |
||
154 | |||
155 | /** |
||
156 | * Returns the parsed plural definition. |
||
157 | * |
||
158 | * @param null|array [count, rule] |
||
159 | */ |
||
160 | public function getPluralForms() |
||
168 | |||
169 | /** |
||
170 | * Set a new header. |
||
171 | * |
||
172 | * @param string $name |
||
173 | * @param string $value |
||
174 | * |
||
175 | * @return self |
||
176 | */ |
||
177 | public function setHeader($name, $value) |
||
184 | |||
185 | /** |
||
186 | * Returns a header value. |
||
187 | * |
||
188 | * @param string $name |
||
189 | * |
||
190 | * @return null|string |
||
191 | */ |
||
192 | public function getHeader($name) |
||
196 | |||
197 | /** |
||
198 | * Returns all header for this translations (in alphabetic order). |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getHeaders() |
||
208 | |||
209 | /** |
||
210 | * Removes all headers. |
||
211 | * |
||
212 | * @return self |
||
213 | */ |
||
214 | public function deleteHeaders() |
||
220 | |||
221 | /** |
||
222 | * Removes one header. |
||
223 | * |
||
224 | * @param string $name |
||
225 | * |
||
226 | * @return self |
||
227 | */ |
||
228 | public function deleteHeader($name) |
||
234 | |||
235 | /** |
||
236 | * Returns the language value. |
||
237 | * |
||
238 | * @return string $language |
||
239 | */ |
||
240 | public function getLanguage() |
||
244 | |||
245 | /** |
||
246 | * Sets the language and the plural forms. |
||
247 | * |
||
248 | * @param string $language |
||
249 | * |
||
250 | * @throws InvalidArgumentException if the language hasn't been recognized |
||
251 | * |
||
252 | * @return self |
||
253 | */ |
||
254 | public function setLanguage($language) |
||
264 | |||
265 | /** |
||
266 | * Checks whether the language is empty or not. |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function hasLanguage() |
||
276 | |||
277 | /** |
||
278 | * Set a new domain for this translations. |
||
279 | * |
||
280 | * @param string $domain |
||
281 | * |
||
282 | * @return self |
||
283 | */ |
||
284 | public function setDomain($domain) |
||
290 | |||
291 | /** |
||
292 | * Returns the domain. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getDomain() |
||
300 | |||
301 | /** |
||
302 | * Checks whether the domain is empty or not. |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function hasDomain() |
||
312 | |||
313 | /** |
||
314 | * Search for a specific translation. |
||
315 | * |
||
316 | * @param string|Translation $context The context of the translation or a translation instance |
||
317 | * @param string $original The original string |
||
318 | * |
||
319 | * @return Translation|false |
||
320 | */ |
||
321 | public function find($context, $original = '') |
||
331 | |||
332 | /** |
||
333 | * Creates and insert/merges a new translation. |
||
334 | * |
||
335 | * @param string $context The translation context |
||
336 | * @param string $original The translation original string |
||
337 | * @param string $plural The translation original plural string |
||
338 | * |
||
339 | * @return Translation The translation created |
||
340 | */ |
||
341 | public function insert($context, $original, $plural = '') |
||
345 | |||
346 | /** |
||
347 | * Merges this translations with other translations. |
||
348 | * |
||
349 | * @param Translations $translations The translations instance to merge with |
||
350 | * @param int $options One or various Translations::MERGE_* constants to define how to merge the translations |
||
351 | * @param int $translationOptions One or various Translation::MERGE_* constants to define how to merge each translation |
||
352 | * |
||
353 | * @return self |
||
354 | */ |
||
355 | public function mergeWith(Translations $translations, $options = self::MERGE_ADD, $translationOptions = Translation::MERGE_TRANSLATION_OVERRIDE) |
||
420 | } |
||
421 |
This check looks for function calls that miss required arguments.