Xliff::fromString()   C
last analyzed

Complexity

Conditions 14
Paths 6

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 34
nc 6
nop 3
dl 0
loc 55
rs 6.7491
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\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
                    $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