Completed
Push — v5-dev ( a42eb3...269a1b )
by Oscar
01:14
created

ArrayLoader::loadArray()   C

Complexity

Conditions 12
Paths 120

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 120
nop 2
dl 0
loc 47
rs 6.8
c 0
b 0
f 0

How to fix   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\Loader;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use Gettext\Headers;
8
use BadMethodCallException;
9
10
/**
11
 * Class to load a array file
12
 */
13
final class ArrayLoader extends Loader
14
{
15
    use HeadersLoaderTrait;
16
17
    public function loadFile(string $filename, Translations $translations = null): Translations
18
    {
19
        $array = self::includeSafe($filename);
20
21
        return $this->loadArray($array, $translations);
22
    }
23
24
    public function loadString(string $string, Translations $translations = null): Translations
25
    {
26
        throw new BadMethodCallException('Arrays cannot be loaded from string. Use ArrayLoader::loadFile() instead');
27
    }
28
29
    private static function includeSafe($filename): array
30
    {
31
        return include($filename);
32
    }
33
34
    public function loadArray(array $array, Translations $translations = null): Translations
35
    {
36
        if (!$translations) {
37
            $translations = $this->createTranslations();
38
        }
39
40
        $messages = $array['messages'] ?? [];
41
42
        foreach ($messages as $context => $contextTranslations) {
43
            if ($context === '') {
44
                $context = null;
45
            }
46
47
            foreach ($contextTranslations as $original => $value) {
48
                //Headers
49
                if ($context === null && $original === '') {
50
                    $string = is_array($value) ? array_shift($value) : $value;
51
                    $headers = $translations->getHeaders();
52
53
                    foreach (static::parseHeaders($string) as $name => $value) {
0 ignored issues
show
Comprehensibility introduced by
Since Gettext\Loader\ArrayLoader is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
54
                        $headers->set($name, $value);
55
                    }
56
                    continue;
57
                }
58
59
                $translation = $this->createTranslation($context, $original);
60
                $translations->add($translation);
61
62
                if (is_array($value)) {
63
                    $translation->translate(array_shift($value));
64
                    $translation->translatePlural(...$value);
65
                } else {
66
                    $translation->translate($value);
67
                }
68
            }
69
        }
70
71
        if (!empty($array['domain'])) {
72
            $translations->setDomain($array['domain']);
73
        }
74
75
        if (!empty($array['plural-forms'])) {
76
            $translations->getHeaders()->set(Headers::HEADER_PLURAL, $array['plural-forms']);
77
        }
78
79
        return $translations;
80
    }
81
}
82