Complex classes like Translation 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 Translation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Translation |
||
| 9 | { |
||
| 10 | protected $context; |
||
| 11 | protected $original; |
||
| 12 | protected $translation = ''; |
||
| 13 | protected $plural; |
||
| 14 | protected $pluralTranslation = array(); |
||
| 15 | protected $references = array(); |
||
| 16 | protected $comments = array(); |
||
| 17 | protected $extractedComments = array(); |
||
| 18 | protected $flags = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Generates the id of a translation (context + glue + original). |
||
| 22 | * |
||
| 23 | * @param string $context |
||
| 24 | * @param string $original |
||
| 25 | * |
||
| 26 | * @return string |
||
| 27 | */ |
||
| 28 | public static function generateId($context, $original) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Construct. |
||
| 35 | * |
||
| 36 | * @param string $context The context of the translation |
||
| 37 | * @param string $original The original string |
||
| 38 | * @param string $plural The original plural string |
||
| 39 | */ |
||
| 40 | public function __construct($context, $original, $plural = '') |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Clones this translation. |
||
| 50 | * |
||
| 51 | * @param null|string $context Optional new context |
||
| 52 | * @param null|string $original Optional new original |
||
| 53 | */ |
||
| 54 | public function getClone($context = null, $original = null) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the id of this translation. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public function getId() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Checks whether the translation matches with the arguments. |
||
| 81 | * |
||
| 82 | * @param string $context |
||
| 83 | * @param string $original |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | public function is($context, $original = '') |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the original string. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getOriginal() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Checks if the original string is empty or not. |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function hasOriginal() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Sets the translation string. |
||
| 114 | * |
||
| 115 | * @param string $translation |
||
| 116 | * |
||
| 117 | * @return self |
||
| 118 | */ |
||
| 119 | public function setTranslation($translation) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets the translation string. |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getTranslation() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Checks if the translation string is empty or not. |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function hasTranslation() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Sets the plural translation string. |
||
| 148 | * |
||
| 149 | * @param string $plural |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | public function setPlural($plural) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Gets the plural translation string. |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getPlural() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Checks if the plural translation string is empty or not. |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function hasPlural() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set a new plural translation. |
||
| 182 | * |
||
| 183 | * @param array $plural |
||
| 184 | * |
||
| 185 | * @return self |
||
| 186 | */ |
||
| 187 | public function setPluralTranslations(array $plural) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets all plural translations. |
||
| 196 | * |
||
| 197 | * @param int $limit |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function getPluralTranslations($limit = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Checks if there are any plural translation. |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | public function hasPluralTranslations() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Removes all plural translations. |
||
| 232 | */ |
||
| 233 | public function deletePluralTranslation() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gets the context of this translation. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getContext() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Checks if the context is empty or not. |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function hasContext() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds a new reference for this translation. |
||
| 260 | * |
||
| 261 | * @param string $filename The file path where the translation has been found |
||
| 262 | * @param null|int $line The line number where the translation has been found |
||
| 263 | */ |
||
| 264 | public function addReference($filename, $line = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Checks if the translation has any reference. |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function hasReferences() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return all references for this translation. |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getReferences() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Removes all references. |
||
| 292 | */ |
||
| 293 | public function deleteReferences() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Adds a new comment for this translation. |
||
| 300 | * |
||
| 301 | * @param string $comment |
||
| 302 | */ |
||
| 303 | public function addComment($comment) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Checks if the translation has any comment. |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function hasComments() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns all comments for this translation. |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | public function getComments() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Removes all comments. |
||
| 332 | */ |
||
| 333 | public function deleteComments() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Adds a new extracted comment for this translation. |
||
| 340 | * |
||
| 341 | * @param string $comment |
||
| 342 | */ |
||
| 343 | public function addExtractedComment($comment) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Checks if the translation has any extracted comment. |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | public function hasExtractedComments() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns all extracted comments for this translation. |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function getExtractedComments() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Removes all extracted comments. |
||
| 372 | */ |
||
| 373 | public function deleteExtractedComments() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Adds a new flat for this translation. |
||
| 380 | * |
||
| 381 | * @param string $flag |
||
| 382 | */ |
||
| 383 | public function addFlag($flag) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Checks if the translation has any flag. |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | public function hasFlags() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns all extracted flags for this translation. |
||
| 402 | * |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | public function getFlags() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Removes all flags. |
||
| 412 | */ |
||
| 413 | public function deleteFlags() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Merges this translation with other translation. |
||
| 420 | * |
||
| 421 | * @param Translation $translation The translation to merge with |
||
| 422 | * @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations |
||
| 423 | */ |
||
| 424 | public function mergeWith(Translation $translation, $method = null) |
||
| 454 | } |
||
| 455 |