Completed
Push — v4.0-dev ( 627b53 )
by Oscar
03:24
created

PhpArray::extract()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 6
eloc 12
nc 24
nop 2
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Exception;
6
use Gettext\Translations;
7
8
/**
9
 * Class to get gettext strings from php files returning arrays.
10
 */
11
class PhpArray extends Extractor implements ExtractorInterface
12
{
13
    /**
14
     * Extract the translations from a file.
15
     *
16
     * @param array|string      $file         A path of a file or files
17
     * @param null|Translations $translations The translations instance to append the new translations.
18
     *
19
     * @return Translations
20
     */
21 View Code Duplication
    public static function fromFile($file, Translations $translations = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        if ($translations === null) {
24
            $translations = new Translations();
25
        }
26
27
        foreach (static::getFiles($file) as $file) {
28
            static::extract(include($file), $translations);
29
        }
30
31
        return $translations;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public static function fromString($string, Translations $translations = null, $file = '')
38
    {
39
        throw new Exception('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()');
40
    }
41
42
    /**
43
     * Handle an array of translations and append to the Translations instance.
44
     *
45
     * @param array        $content
46
     * @param Translations $translations
47
     */
48
    public static function extract(array $content, Translations $translations)
49
    {
50
        if (!empty($content['domain'])) {
51
            $translations->setDomain($content['domain']);
52
        }
53
54
        if (!empty($content['lang'])) {
55
            $translations->setLanguage($content['language']);
56
        }
57
58
        if (!empty($content['plural-forms'])) {
59
            $translations->setPluralForms($content['plural-forms']);
0 ignored issues
show
Bug introduced by
The call to setPluralForms() misses a required argument $rule.

This check looks for function calls that miss required arguments.

Loading history...
60
        }
61
62
        foreach ($content['messages'] as $context => $messages) {
63
            foreach ($messages as $original => $translations) {
64
                $translations->insert($context, $original)
65
                    ->setTranslation(array_shift($translations))
66
                    ->setPluralTranslations($translations);
67
            }
68
        }
69
    }
70
}
71