Completed
Pull Request — master (#219)
by David
01:14
created

FunctionsScanner   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 28
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
getFunctions() 0 1 ?
F saveGettextFunctions() 0 114 28
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
            $domain = $context = $original = $plural = null;
43
44
            switch ($functions[$name]) {
45
                case 'noop':
46
                case 'gettext':
47
                    if (!isset($args[0])) {
48
                        continue 2;
49
                    }
50
51
                    $original = $args[0];
52
                    break;
53
54
                case 'ngettext':
55
                    if (!isset($args[1])) {
56
                        continue 2;
57
                    }
58
59
                    list($original, $plural) = $args;
60
                    break;
61
62
                case 'pgettext':
63
                    if (!isset($args[1])) {
64
                        continue 2;
65
                    }
66
67
                    list($context, $original) = $args;
68
                    break;
69
70
                case 'dgettext':
71
                    if (!isset($args[1])) {
72
                        continue 2;
73
                    }
74
75
                    list($domain, $original) = $args;
76
                    break;
77
78
                case 'dpgettext':
79
                    if (!isset($args[2])) {
80
                        continue 2;
81
                    }
82
83
                    list($domain, $context, $original) = $args;
84
                    break;
85
86
                case 'npgettext':
87
                    if (!isset($args[2])) {
88
                        continue 2;
89
                    }
90
91
                    list($context, $original, $plural) = $args;
92
                    break;
93
94
                case 'dnpgettext':
95
                    if (!isset($args[3])) {
96
                        continue 2;
97
                    }
98
99
                    list($domain, $context, $original, $plural) = $args;
100
                    break;
101
102
                case 'dngettext':
103
                    if (!isset($args[2])) {
104
                        continue 2;
105
                    }
106
107
                    list($domain, $original, $plural) = $args;
108
                    break;
109
110
                default:
111
                    throw new Exception(sprintf('Not valid function %s', $functions[$name]));
112
            }
113
114
            if ((string)$original === '') {
115
                continue;
116
            }
117
118
            $isDefaultDomain = $domain === null;
119
            $isMatchingDomain = $domain === $translations->getDomain();
120
121
            if (!empty($options['domainOnly']) && $isDefaultDomain) {
122
                // If we want to find translations for a specific domain, skip default domain messages
123
                continue;
124
            }
125
126
            if (!$isDefaultDomain && !$isMatchingDomain) {
127
                continue;
128
            }
129
130
            $translation = $translations->insert($context, $original, $plural);
131
            $translation->addReference($file, $line);
132
133
            if (isset($function[3])) {
134
                foreach ($function[3] as $extractedComment) {
135
                    $translation->addExtractedComment($extractedComment);
136
                }
137
            }
138
        }
139
    }
140
}
141