JsCode::fromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 3
rs 10
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Exception;
6
use Gettext\Translations;
7
use Gettext\Utils\FunctionsScanner;
8
9
/**
10
 * Class to get gettext strings from javascript files.
11
 */
12
class JsCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
13
{
14
    public static $options = [
15
        'constants' => [],
16
17
        'functions' => [
18
            'gettext' => 'gettext',
19
            '__' => 'gettext',
20
            'ngettext' => 'ngettext',
21
            'n__' => 'ngettext',
22
            'pgettext' => 'pgettext',
23
            'p__' => 'pgettext',
24
            'dgettext' => 'dgettext',
25
            'd__' => 'dgettext',
26
            'dngettext' => 'dngettext',
27
            'dn__' => 'dngettext',
28
            'dpgettext' => 'dpgettext',
29
            'dp__' => 'dpgettext',
30
            'npgettext' => 'npgettext',
31
            'np__' => 'npgettext',
32
            'dnpgettext' => 'dnpgettext',
33
            'dnp__' => 'dnpgettext',
34
            'noop' => 'noop',
35
            'noop__' => 'noop',
36
        ],
37
    ];
38
39
    protected static $functionsScannerClass = 'Gettext\Utils\JsFunctionsScanner';
40
41
    /**
42
     * @inheritdoc
43
     * @throws Exception
44
     */
45
    public static function fromString($string, Translations $translations, array $options = [])
46
    {
47
        static::fromStringMultiple($string, [$translations], $options);
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @throws Exception
53
     */
54
    public static function fromStringMultiple($string, array $translations, array $options = [])
55
    {
56
        $options += static::$options;
57
58
        /** @var FunctionsScanner $functions */
59
        $functions = new static::$functionsScannerClass($string);
60
        $functions->saveGettextFunctions($translations, $options);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     * @throws Exception
66
     */
67 View Code Duplication
    public static function fromFileMultiple($file, array $translations, array $options = [])
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...
68
    {
69
        foreach (static::getFiles($file) as $file) {
70
            $options['file'] = $file;
71
            static::fromStringMultiple(static::readFile($file), $translations, $options);
72
        }
73
    }
74
}
75