Completed
Push — master ( d902d2...5712dc )
by Oscar
02:01
created

Xliff::fromString()   C

Complexity

Conditions 14
Paths 6

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 14
eloc 35
c 1
b 0
f 1
nc 6
nop 3
dl 0
loc 58
rs 6.4881

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\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use SimpleXMLElement;
8
9
/**
10
 * Class to get gettext strings from xliff format.
11
 */
12
class Xliff extends Extractor implements ExtractorInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
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
37
                    $translation = new Translation(null, (string) $segment->source);
38
                    $translation->setTranslation(array_shift($targets));
39
                    $translation->setPluralTranslations($targets);
40
41
                    if (isset($unit->notes)) {
42
                        foreach ($unit->notes->note as $note) {
43
                            switch ($note['category']) {
44
                                case 'context':
45
                                    $translation = $translation->getClone((string) $note);
46
                                    break;
47
48
                                case 'extracted-comment':
49
                                    $translation->addExtractedComment((string) $note);
50
                                    break;
51
52
                                case 'flag':
53
                                    $translation->addFlag((string) $note);
54
                                    break;
55
56
                                case 'reference':
57
                                    $ref = explode(':', (string) $note, 2);
58
                                    $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null);
59
                                    break;
60
                                
61
                                default:
62
                                    $translation->addComment((string) $note);
63
                                    break;
64
                            }
65
                        }
66
                    }
67
68
                    $translations[] = $translation;
69
                }
70
            }
71
        }
72
73
        return $translations;
74
    }
75
}
76