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 = []; |
||
| 15 | protected $references = []; |
||
| 16 | protected $comments = []; |
||
| 17 | protected $extractedComments = []; |
||
| 18 | protected $flags = []; |
||
| 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 | * @return Translation |
||
| 55 | */ |
||
| 56 | public function getClone($context = null, $original = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns the id of this translation. |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getId() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Checks whether the translation matches with the arguments. |
||
| 83 | * |
||
| 84 | * @param string $context |
||
| 85 | * @param string $original |
||
| 86 | * |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function is($context, $original = '') |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets the original string. |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function getOriginal() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Checks if the original string is empty or not. |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function hasOriginal() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the translation string. |
||
| 116 | * |
||
| 117 | * @param string $translation |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | public function setTranslation($translation) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the translation string. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getTranslation() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Checks if the translation string is empty or not. |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function hasTranslation() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Sets the plural translation string. |
||
| 150 | * |
||
| 151 | * @param string $plural |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | public function setPlural($plural) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the plural translation string. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getPlural() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Checks if the plural translation string is empty or not. |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function hasPlural() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set a new plural translation. |
||
| 184 | * |
||
| 185 | * @param array $plural |
||
| 186 | * |
||
| 187 | * @return self |
||
| 188 | */ |
||
| 189 | public function setPluralTranslations(array $plural) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets all plural translations. |
||
| 198 | * |
||
| 199 | * @param int $size |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function getPluralTranslations($size = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Checks if there are any plural translation. |
||
| 224 | * |
||
| 225 | * @param bool $checkContent |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function hasPluralTranslations($checkContent = false) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Removes all plural translations. |
||
| 240 | * |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | public function deletePluralTranslation() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Gets the context of this translation. |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getContext() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Checks if the context is empty or not. |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function hasContext() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Adds a new reference for this translation. |
||
| 272 | * |
||
| 273 | * @param string $filename The file path where the translation has been found |
||
| 274 | * @param null|int $line The line number where the translation has been found |
||
| 275 | * |
||
| 276 | * @return self |
||
| 277 | */ |
||
| 278 | public function addReference($filename, $line = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Checks if the translation has any reference. |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function hasReferences() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Return all references for this translation. |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function getReferences() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Removes all references. |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | public function deleteReferences() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Adds a new comment for this translation. |
||
| 320 | * |
||
| 321 | * @param string $comment |
||
| 322 | * |
||
| 323 | * @return self |
||
| 324 | */ |
||
| 325 | public function addComment($comment) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Checks if the translation has any comment. |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function hasComments() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns all comments for this translation. |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function getComments() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Removes all comments. |
||
| 356 | * |
||
| 357 | * @return self |
||
| 358 | */ |
||
| 359 | public function deleteComments() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Adds a new extracted comment for this translation. |
||
| 368 | * |
||
| 369 | * @param string $comment |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | */ |
||
| 373 | public function addExtractedComment($comment) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Checks if the translation has any extracted comment. |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function hasExtractedComments() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns all extracted comments for this translation. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function getExtractedComments() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Removes all extracted comments. |
||
| 404 | * |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function deleteExtractedComments() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Adds a new flag for this translation. |
||
| 416 | * |
||
| 417 | * @param string $flag |
||
| 418 | * |
||
| 419 | * @return self |
||
| 420 | */ |
||
| 421 | public function addFlag($flag) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Checks if the translation has any flag. |
||
| 432 | * |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public function hasFlags() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns all extracted flags for this translation. |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | public function getFlags() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Removes all flags. |
||
| 452 | * |
||
| 453 | * @return self |
||
| 454 | */ |
||
| 455 | public function deleteFlags() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Merges this translation with other translation. |
||
| 464 | * |
||
| 465 | * @param Translation $translation The translation to merge with |
||
| 466 | * @param int $options |
||
| 467 | * |
||
| 468 | * @return self |
||
| 469 | */ |
||
| 470 | public function mergeWith(Translation $translation, $options = Merge::DEFAULTS) |
||
| 480 | } |
||
| 481 |