| Conditions | 16 |
| Paths | 6 |
| Total Lines | 64 |
| 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 |
||
| 22 | public static function fromString($string, Translations $translations, array $options = []) |
||
| 23 | { |
||
| 24 | $options += static::$options; |
||
| 25 | |||
| 26 | $xml = new SimpleXMLElement($string, null, false); |
||
| 27 | |||
| 28 | foreach ($xml->file as $file) { |
||
| 29 | if (isset($file->notes)) { |
||
| 30 | foreach ($file->notes->note as $note) { |
||
| 31 | $translations->setHeader($note['id'], (string) $note); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | foreach ($file->unit as $unit) { |
||
| 36 | foreach ($unit->segment as $segment) { |
||
| 37 | $targets = []; |
||
| 38 | |||
| 39 | foreach ($segment->target as $target) { |
||
| 40 | $targets[] = (string) $target; |
||
| 41 | } |
||
| 42 | |||
| 43 | $translation = $translations->createNewTranslation(null, (string) $segment->source); |
||
| 44 | if (isset($unit['id'])) { |
||
| 45 | $unitId = (string) $unit['id']; |
||
| 46 | $translation->addComment("XLIFF_UNIT_ID: $unitId"); |
||
| 47 | if ($options['unitid_as_id']) { |
||
| 48 | $translation->setId($unitId); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | $translation->setTranslation(array_shift($targets)); |
||
| 52 | $translation->setPluralTranslations($targets); |
||
| 53 | |||
| 54 | if (isset($unit->notes)) { |
||
| 55 | foreach ($unit->notes->note as $note) { |
||
| 56 | switch ($note['category']) { |
||
| 57 | case 'context': |
||
| 58 | $translation = $translation->getClone((string) $note); |
||
| 59 | break; |
||
| 60 | |||
| 61 | case 'extracted-comment': |
||
| 62 | $translation->addExtractedComment((string) $note); |
||
| 63 | break; |
||
| 64 | |||
| 65 | case 'flag': |
||
| 66 | $translation->addFlag((string) $note); |
||
| 67 | break; |
||
| 68 | |||
| 69 | case 'reference': |
||
| 70 | $ref = explode(':', (string) $note, 2); |
||
| 71 | $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null); |
||
| 72 | break; |
||
| 73 | |||
| 74 | default: |
||
| 75 | $translation->addComment((string) $note); |
||
| 76 | break; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $translations[] = $translation; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 |