| Conditions | 14 |
| Paths | 6 |
| Total Lines | 55 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | public static function fromString($string, Translations $translations, array $options = []) |
||
| 18 | { |
||
| 19 | $xml = new SimpleXMLElement($string, null, false); |
||
| 20 | |||
| 21 | foreach ($xml->file as $file) { |
||
| 22 | if (isset($file->notes)) { |
||
| 23 | foreach ($file->notes->note as $note) { |
||
| 24 | $translations->setHeader($note['id'], (string) $note); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | foreach ($file->unit as $unit) { |
||
| 29 | foreach ($unit->segment as $segment) { |
||
| 30 | $targets = []; |
||
| 31 | |||
| 32 | foreach ($segment->target as $target) { |
||
| 33 | $targets[] = (string) $target; |
||
| 34 | } |
||
| 35 | |||
| 36 | $translation = new Translation(null, (string) $segment->source); |
||
| 37 | $translation->setTranslation(array_shift($targets)); |
||
| 38 | $translation->setPluralTranslations($targets); |
||
| 39 | |||
| 40 | if (isset($unit->notes)) { |
||
| 41 | foreach ($unit->notes->note as $note) { |
||
| 42 | switch ($note['category']) { |
||
| 43 | case 'context': |
||
| 44 | $translation = $translation->getClone((string) $note); |
||
| 45 | break; |
||
| 46 | |||
| 47 | case 'extracted-comment': |
||
| 48 | $translation->addExtractedComment((string) $note); |
||
| 49 | break; |
||
| 50 | |||
| 51 | case 'flag': |
||
| 52 | $translation->addFlag((string) $note); |
||
| 53 | break; |
||
| 54 | |||
| 55 | case 'reference': |
||
| 56 | $ref = explode(':', (string) $note, 2); |
||
| 57 | $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null); |
||
| 58 | break; |
||
| 59 | |||
| 60 | default: |
||
| 61 | $translation->addComment((string) $note); |
||
| 62 | break; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | $translations[] = $translation; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 |