PhpCode   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 126
rs 10
c 4
b 0
f 0
wmc 21
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 12 2
C convertString() 0 45 14
B unicodeChar() 0 24 5
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 $options = [
14
         // - false: to not extract comments
15
         // - empty string: to extract all comments
16
         // - non-empty string: to extract comments that start with that string
17
         // - array with strings to extract comments format.
18
        'extractComments' => false,
19
20
        'constants' => [],
21
22
        'functions' => [
23
            'gettext' => 'gettext',
24
            '__' => 'gettext',
25
            'ngettext' => 'ngettext',
26
            'n__' => 'ngettext',
27
            'pgettext' => 'pgettext',
28
            'p__' => 'pgettext',
29
            'dgettext' => 'dgettext',
30
            'd__' => 'dgettext',
31
            'dpgettext' => 'dpgettext',
32
            'dp__' => 'dpgettext',
33
            'npgettext' => 'npgettext',
34
            'np__' => 'npgettext',
35
            'dnpgettext' => 'dnpgettext',
36
            'dnp__' => 'dnpgettext',
37
            'noop' => 'noop',
38
            'noop__' => 'noop',
39
        ],
40
    ];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public static function fromString($string, Translations $translations, array $options = [])
46
    {
47
        $options += static::$options;
48
49
        $functions = new PhpFunctionsScanner($string);
50
51
        if ($options['extractComments'] !== false) {
52
            $functions->enableCommentsExtraction($options['extractComments']);
53
        }
54
55
        $functions->saveGettextFunctions($translations, $options);
56
    }
57
58
    /**
59
     * Decodes a T_CONSTANT_ENCAPSED_STRING string.
60
     *
61
     * @param string $value
62
     *
63
     * @return string
64
     */
65
    public static function convertString($value)
66
    {
67
        if (strpos($value, '\\') === false) {
68
            return substr($value, 1, -1);
69
        }
70
71
        if ($value[0] === "'") {
72
            return strtr(substr($value, 1, -1), ['\\\\' => '\\', '\\\'' => '\'']);
73
        }
74
75
        $value = substr($value, 1, -1);
76
77
        return preg_replace_callback(
78
            '/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/',
79
            function ($match) {
80
                switch ($match[1][0]) {
81
                    case 'n':
82
                        return "\n";
83
                    case 'r':
84
                        return "\r";
85
                    case 't':
86
                        return "\t";
87
                    case 'v':
88
                        return "\v";
89
                    case 'e':
90
                        return "\e";
91
                    case 'f':
92
                        return "\f";
93
                    case '$':
94
                        return '$';
95
                    case '"':
96
                        return '"';
97
                    case '\\':
98
                        return '\\';
99
                    case 'x':
100
                        return chr(hexdec(substr($match[0], 1)));
101
                    case 'u':
102
                        return self::unicodeChar(hexdec(substr($match[0], 1)));
103
                    default:
104
                        return chr(octdec($match[0]));
105
                }
106
            },
107
            $value
108
        );
109
    }
110
111
    //http://php.net/manual/en/function.chr.php#118804
112
    private static function unicodeChar($dec)
113
    {
114
        if ($dec < 0x80) {
115
            return chr($dec);
116
        }
117
118
        if ($dec < 0x0800) {
119
            return chr(0xC0 + ($dec >> 6))
120
                .chr(0x80 + ($dec & 0x3f));
121
        }
122
123
        if ($dec < 0x010000) {
124
            return chr(0xE0 + ($dec >> 12))
125
                    .chr(0x80 + (($dec >> 6) & 0x3f))
126
                    .chr(0x80 + ($dec & 0x3f));
127
        }
128
129
        if ($dec < 0x200000) {
130
            return chr(0xF0 + ($dec >> 18))
131
                    .chr(0x80 + (($dec >> 12) & 0x3f))
132
                    .chr(0x80 + (($dec >> 6) & 0x3f))
133
                    .chr(0x80 + ($dec & 0x3f));
134
        }
135
    }
136
}
137