1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext\Extractors; |
4
|
|
|
|
5
|
|
|
use Gettext\Translations; |
6
|
|
|
use Gettext\Utils\PhpFunctionsScanner; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to get gettext strings from php files returning arrays. |
10
|
|
|
*/ |
11
|
|
|
class PhpCode extends Extractor implements ExtractorInterface |
12
|
|
|
{ |
13
|
|
|
public static $functions = array( |
14
|
|
|
'__' => '__', |
15
|
|
|
'__e' => '__', |
16
|
|
|
'n__' => 'n__', |
17
|
|
|
'n__e' => 'n__', |
18
|
|
|
'p__' => 'p__', |
19
|
|
|
'p__e' => 'p__', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public static function fromString($string, Translations $translations = null, $file = '') |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if ($translations === null) { |
28
|
|
|
$translations = new Translations(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$functions = new PhpFunctionsScanner($string); |
32
|
|
|
$functions->saveGettextFunctions(self::$functions, $translations, $file); |
33
|
|
|
|
34
|
|
|
return $translations; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Decodes a T_CONSTANT_ENCAPSED_STRING string. |
39
|
|
|
* |
40
|
|
|
* @param string $value |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public static function convertString($value) |
45
|
|
|
{ |
46
|
|
|
if ($value[0] === "'" || strpos($value, '$') === false) { |
47
|
|
|
if (strpos($value, '\\') === false) { |
48
|
|
|
return substr($value, 1, -1); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return eval("return $value;"); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$result = ''; |
55
|
|
|
$value = substr($value, 1, -1); |
56
|
|
|
|
57
|
|
|
while (($p = strpos($value, '\\')) !== false) { |
58
|
|
|
if (!isset($value[$p + 1])) { |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($p > 0) { |
63
|
|
|
$result .= substr($value, 0, $p); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$value = substr($value, $p + 1); |
67
|
|
|
$p = strpos($value, '$'); |
68
|
|
|
|
69
|
|
|
if ($p === false) { |
70
|
|
|
$result .= eval('return "\\'.$value.'";'); |
71
|
|
|
$value = ''; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($p === 0) { |
76
|
|
|
$result .= '$'; |
77
|
|
|
$value = substr($value, 1); |
78
|
|
|
} else { |
79
|
|
|
$result .= eval('return "\\'.substr($value, 0, $p).'";'); |
80
|
|
|
$value = substr($value, $p); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result.$value; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.