Completed
Push — master ( f6a7a5...ffb2f2 )
by Oscar
02:31
created

FunctionsScanner::getFunctions()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
nc 1
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
     * @return array
14
     */
15
    abstract public function getFunctions();
16
17
    /**
18
     * Search for specific functions and create translations.
19
     *
20
     * @param array        $functions    The gettext functions to search
21
     * @param Translations $translations The translations instance where save the values
22
     * @param string       $file         The filename used to the reference
23
     */
24
    public function saveGettextFunctions(array $functions, Translations $translations, $file = '')
25
    {
26
        foreach ($this->getFunctions() as $function) {
27
            list($name, $line, $args) = $function;
28
29
            if (!isset($functions[$name])) {
30
                continue;
31
            }
32
33
            $translation = null;
34
35
            switch ($functions[$name]) {
36
                case '__':
37
                    if (!isset($args[0])) {
38
                        continue 2;
39
                    }
40
41
                    $original = $args[0];
42
43
                    if ($original !== '') {
44
                        $translation = $translations->insert('', $original);
45
                    }
46
                    break;
47
48
                case 'n__':
49
                    if (!isset($args[1])) {
50
                        continue 2;
51
                    }
52
53
                    list($original, $plural) = $args;
54
55
                    if ($original !== '') {
56
                        $translation = $translations->insert('', $original, $plural);
57
                    }
58
                    break;
59
60
                case 'p__':
61
                    if (!isset($args[1])) {
62
                        continue 2;
63
                    }
64
65
                    list($context, $original) = $args;
66
67
                    if ($original !== '') {
68
                        $translation = $translations->insert($context, $original);
69
                    }
70
                    break;
71
72
                case 'd__':
73
                    if (!isset($args[1])) {
74
                        continue 2;
75
                    }
76
77
                    list($domain, $original) = $args;
78
79
                    if ($original !== '' && $domain === $translations->getDomain()) {
80
                        $translation = $translations->insert('', $original);
81
                    }
82
                    break;
83
84 View Code Duplication
                case 'dp__':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
                    if (!isset($args[2])) {
86
                        continue 2;
87
                    }
88
89
                    list($domain, $context, $original) = $args;
90
91
                    if ($original !== '' && $domain === $translations->getDomain()) {
92
                        $translation = $translations->insert($context, $original);
93
                    }
94
                    break;
95
96
                case 'np__':
97
                    if (!isset($args[2])) {
98
                        continue 2;
99
                    }
100
101
                    list($context, $original, $plural) = $args;
102
103
                    if ($original !== '') {
104
                        $translation = $translations->insert($context, $original, $plural);
105
                    }
106
                    break;
107
108 View Code Duplication
                case 'dnp__':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
                    if (!isset($args[4])) {
110
                        continue 2;
111
                    }
112
113
                    list($domain, $context, $original, $plural) = $args;
114
115
                    if ($original !== '' && $domain === $translations->getDomain()) {
116
                        $translation = $translations->insert($context, $original, $plural);
117
                    }
118
                    break;
119
120
                default:
121
                    throw new Exception('Not valid functions');
122
            }
123
124
            if (isset($translation)) {
125
                $translation->addReference($file, $line);
126
                if (isset($function[3])) {
127
                    foreach ($function[3] as $extractedComment) {
128
                        $translation->addExtractedComment($extractedComment);
129
                    }
130
                }
131
            }
132
        }
133
    }
134
}
135