Completed
Pull Request — master (#221)
by Alec
01:19
created

Xliff::toString()   F

Complexity

Conditions 11
Paths 388

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 388
nop 2
dl 0
loc 74
rs 3.8739
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
use Gettext\XliffTranslation;
7
use DOMDocument;
8
9
class Xliff extends Generator implements GeneratorInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
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
89
    private static function createTextNode(DOMDocument $dom, $name, $string)
90
    {
91
        $node = $dom->createElement($name);
92
        $text = (preg_match('/[&<>]/', $string) === 1)
93
             ? $dom->createCDATASection($string)
94
             : $dom->createTextNode($string);
95
        $node->appendChild($text);
96
97
        return $node;
98
    }
99
}
100