|
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
|
|
|
$domain = $context = $original = $plural = $translation = null; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
switch ($functions[$name]) { |
|
36
|
|
|
case 'gettext': |
|
37
|
|
|
if (!isset($args[0])) { |
|
38
|
|
|
continue 2; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$original = $args[0]; |
|
42
|
|
|
break; |
|
43
|
|
|
|
|
44
|
|
|
case 'ngettext': |
|
45
|
|
|
if (!isset($args[1])) { |
|
46
|
|
|
continue 2; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
list($original, $plural) = $args; |
|
50
|
|
|
break; |
|
51
|
|
|
|
|
52
|
|
|
case 'pgettext': |
|
53
|
|
|
if (!isset($args[1])) { |
|
54
|
|
|
continue 2; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
list($context, $original) = $args; |
|
58
|
|
|
break; |
|
59
|
|
|
|
|
60
|
|
|
case 'dgettext': |
|
61
|
|
|
if (!isset($args[1])) { |
|
62
|
|
|
continue 2; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
list($domain, $original) = $args; |
|
66
|
|
|
break; |
|
67
|
|
|
|
|
68
|
|
|
case 'dpgettext': |
|
69
|
|
|
if (!isset($args[2])) { |
|
70
|
|
|
continue 2; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
list($domain, $context, $original) = $args; |
|
74
|
|
|
break; |
|
75
|
|
|
|
|
76
|
|
|
case 'npgettext': |
|
77
|
|
|
if (!isset($args[2])) { |
|
78
|
|
|
continue 2; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
list($context, $original, $plural) = $args; |
|
82
|
|
|
break; |
|
83
|
|
|
|
|
84
|
|
|
case 'dnpgettext': |
|
85
|
|
|
if (!isset($args[4])) { |
|
86
|
|
|
continue 2; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
list($domain, $context, $original, $plural) = $args; |
|
90
|
|
|
break; |
|
91
|
|
|
|
|
92
|
|
|
default: |
|
93
|
|
|
throw new Exception(sprintf('Not valid function %s', $functions[$name])); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ((string) $original !== '' && ($domain === null || $domain === $translations->getDomain())) { |
|
97
|
|
|
$translation = $translations->insert($context, $original, $plural); |
|
98
|
|
|
$translation->addReference($file, $line); |
|
99
|
|
|
|
|
100
|
|
|
if (isset($function[3])) { |
|
101
|
|
|
foreach ($function[3] as $extractedComment) { |
|
102
|
|
|
$translation->addExtractedComment($extractedComment); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.