Conditions | 11 |
Paths | 388 |
Total Lines | 74 |
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 |
||
14 | public static function toString(Translations $translations, array $options = []) |
||
15 | { |
||
16 | $dom = new DOMDocument('1.0', 'utf-8'); |
||
17 | $dom->formatOutput = true; |
||
18 | $xliff = $dom->appendChild($dom->createElement('xliff')); |
||
19 | $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
||
20 | $xliff->setAttribute('version', '2.0'); |
||
21 | $xliff->setAttribute('srcLang', $translations->getLanguage()); |
||
22 | $xliff->setAttribute('trgLang', $translations->getLanguage()); |
||
23 | $file = $xliff->appendChild($dom->createElement('file')); |
||
24 | $file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage()); |
||
25 | |||
26 | //Save headers as notes |
||
27 | $notes = $dom->createElement('notes'); |
||
28 | |||
29 | foreach ($translations->getHeaders() as $name => $value) { |
||
30 | $notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name); |
||
31 | } |
||
32 | |||
33 | if ($notes->hasChildNodes()) { |
||
34 | $file->appendChild($notes); |
||
35 | } |
||
36 | |||
37 | foreach ($translations as $translation) { |
||
38 | $unit = $dom->createElement('unit'); |
||
39 | if ($translation instanceof XliffTranslation) { |
||
40 | $unit->setAttribute('id', $translation->getUnitId()); |
||
41 | } else { |
||
42 | $unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal())); |
||
43 | } |
||
44 | |||
45 | //Save comments as notes |
||
46 | $notes = $dom->createElement('notes'); |
||
47 | |||
48 | $notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext())) |
||
49 | ->setAttribute('category', 'context'); |
||
50 | |||
51 | foreach ($translation->getComments() as $comment) { |
||
52 | $notes->appendChild(self::createTextNode($dom, 'note', $comment)) |
||
53 | ->setAttribute('category', 'comment'); |
||
54 | } |
||
55 | |||
56 | foreach ($translation->getExtractedComments() as $comment) { |
||
57 | $notes->appendChild(self::createTextNode($dom, 'note', $comment)) |
||
58 | ->setAttribute('category', 'extracted-comment'); |
||
59 | } |
||
60 | |||
61 | foreach ($translation->getFlags() as $flag) { |
||
62 | $notes->appendChild(self::createTextNode($dom, 'note', $flag)) |
||
63 | ->setAttribute('category', 'flag'); |
||
64 | } |
||
65 | |||
66 | foreach ($translation->getReferences() as $reference) { |
||
67 | $notes->appendChild(self::createTextNode($dom, 'note', $reference[0].':'.$reference[1])) |
||
68 | ->setAttribute('category', 'reference'); |
||
69 | } |
||
70 | |||
71 | $unit->appendChild($notes); |
||
72 | |||
73 | $segment = $unit->appendChild($dom->createElement('segment')); |
||
74 | $segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal())); |
||
75 | $segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation())); |
||
76 | |||
77 | foreach ($translation->getPluralTranslations() as $plural) { |
||
78 | if ($plural !== '') { |
||
79 | $segment->appendChild(self::createTextNode($dom, 'target', $plural)); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $file->appendChild($unit); |
||
84 | } |
||
85 | |||
86 | return $dom->saveXML(); |
||
87 | } |
||
88 | |||
100 |