Completed
Pull Request — master (#222)
by Mārtiņš
08:21
created

FunctionsScanner::saveGettextFunctions()   C

Complexity

Conditions 12
Paths 17

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 17
nop 2
dl 0
loc 50
rs 6.9666
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\Utils;
4
5
use Exception;
6
use Gettext\Translations;
7
8
abstract class FunctionsScanner
9
{
10
    /**
11
     * Scan and returns the functions and the arguments.
12
     *
13
     * @param array $constants Constants used in the code to replace
14
     *
15
     * @return array
16
     */
17
    abstract public function getFunctions(array $constants = []);
18
19
    /**
20
     * Search for specific functions and create translations.
21
     *
22
     * @param Translations $translations The translations instance where save the values
23
     * @param array $options The extractor options
24
     * @throws Exception
25
     */
26
    public function saveGettextFunctions(Translations $translations, array $options)
27
    {
28
        $functions = $options['functions'];
29
        $file = $options['file'];
30
31
        foreach ($this->getFunctions($options['constants']) as $function) {
32
            list($name, $line, $args) = $function;
33
34
            if (isset($options['lineOffset'])) {
35
                $line += $options['lineOffset'];
36
            }
37
38
            if (!isset($functions[$name])) {
39
                continue;
40
            }
41
42
            $deconstructed = $this->deconstructArgs($functions[$name], $args);
43
44
            if (!$deconstructed) {
45
                continue;
46
            }
47
48
            list($domain, $context, $original, $plural) = $deconstructed;
49
50
            if ((string)$original === '') {
51
                continue;
52
            }
53
54
            $isDefaultDomain = $domain === null;
55
            $isMatchingDomain = $domain === $translations->getDomain();
56
57
            if (!empty($options['domainOnly']) && $isDefaultDomain) {
58
                // If we want to find translations for a specific domain, skip default domain messages
59
                continue;
60
            }
61
62
            if (!$isDefaultDomain && !$isMatchingDomain) {
63
                continue;
64
            }
65
66
            $translation = $translations->insert($context, $original, $plural);
67
            $translation->addReference($file, $line);
68
69
            if (isset($function[3])) {
70
                foreach ($function[3] as $extractedComment) {
71
                    $translation->addExtractedComment($extractedComment);
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * Deconstruct arguments to translation values
79
     *
80
     * @param $function
81
     * @param $args
82
     * @return array|null
83
     * @throws Exception
84
     */
85
    private function deconstructArgs($function, $args)
86
    {
87
        $domain = null;
88
        $context = null;
89
        $original = null;
90
        $plural = null;
91
92
        switch ($function) {
93
            case 'noop':
94
            case 'gettext':
95
                if (!isset($args[0])) {
96
                    return null;
97
                }
98
99
                $original = $args[0];
100
                break;
101
            case 'ngettext':
102
                if (!isset($args[1])) {
103
                    return null;
104
                }
105
106
                list($original, $plural) = $args;
107
                break;
108
            case 'pgettext':
109
                if (!isset($args[1])) {
110
                    return null;
111
                }
112
113
                list($context, $original) = $args;
114
                break;
115
            case 'dgettext':
116
                if (!isset($args[1])) {
117
                    return null;
118
                }
119
120
                list($domain, $original) = $args;
121
                break;
122
            case 'dpgettext':
123
                if (!isset($args[2])) {
124
                    return null;
125
                }
126
127
                list($domain, $context, $original) = $args;
128
                break;
129
            case 'npgettext':
130
                if (!isset($args[2])) {
131
                    return null;
132
                }
133
134
                list($context, $original, $plural) = $args;
135
                break;
136
            case 'dnpgettext':
137
                if (!isset($args[3])) {
138
                    return null;
139
                }
140
141
                list($domain, $context, $original, $plural) = $args;
142
                break;
143
            case 'dngettext':
144
                if (!isset($args[2])) {
145
                    return null;
146
                }
147
148
                list($domain, $original, $plural) = $args;
149
                break;
150
            default:
151
                throw new Exception(sprintf('Not valid function %s', $function));
152
        }
153
154
        return [$domain, $context, $original, $plural];
155
    }
156
}
157