FunctionsScanner   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 23
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
getFunctions() 0 1 ?
D saveGettextFunctions() 0 88 23
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
     */
25
    public function saveGettextFunctions(Translations $translations, array $options)
26
    {
27
        $functions = $options['functions'];
28
        $file = $options['file'];
29
30
        foreach ($this->getFunctions($options['constants']) as $function) {
31
            list($name, $line, $args) = $function;
32
33
            if (!isset($functions[$name])) {
34
                continue;
35
            }
36
37
            $domain = $context = $original = $plural = null;
38
39
            switch ($functions[$name]) {
40
                case 'noop':
41
                case 'gettext':
42
                    if (!isset($args[0])) {
43
                        continue 2;
44
                    }
45
46
                    $original = $args[0];
47
                    break;
48
49
                case 'ngettext':
50
                    if (!isset($args[1])) {
51
                        continue 2;
52
                    }
53
54
                    list($original, $plural) = $args;
55
                    break;
56
57
                case 'pgettext':
58
                    if (!isset($args[1])) {
59
                        continue 2;
60
                    }
61
62
                    list($context, $original) = $args;
63
                    break;
64
65
                case 'dgettext':
66
                    if (!isset($args[1])) {
67
                        continue 2;
68
                    }
69
70
                    list($domain, $original) = $args;
71
                    break;
72
73
                case 'dpgettext':
74
                    if (!isset($args[2])) {
75
                        continue 2;
76
                    }
77
78
                    list($domain, $context, $original) = $args;
79
                    break;
80
81
                case 'npgettext':
82
                    if (!isset($args[2])) {
83
                        continue 2;
84
                    }
85
86
                    list($context, $original, $plural) = $args;
87
                    break;
88
89
                case 'dnpgettext':
90
                    if (!isset($args[4])) {
91
                        continue 2;
92
                    }
93
94
                    list($domain, $context, $original, $plural) = $args;
95
                    break;
96
97
                default:
98
                    throw new Exception(sprintf('Not valid function %s', $functions[$name]));
99
            }
100
101
            if ((string) $original !== '' && ($domain === null || $domain === $translations->getDomain())) {
102
                $translation = $translations->insert($context, $original, $plural);
103
                $translation->addReference($file, $line);
104
105
                if (isset($function[3])) {
106
                    foreach ($function[3] as $extractedComment) {
107
                        $translation->addExtractedComment($extractedComment);
108
                    }
109
                }
110
            }
111
        }
112
    }
113
}
114