PhpCode   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 159
Duplicated Lines 4.4 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 7
loc 159
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 4 1
A fromStringMultiple() 0 13 2
A fromFileMultiple() 7 7 2
C convertString() 0 45 14
A unicodeChar() 0 26 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Exception;
6
use Gettext\Translations;
7
use Gettext\Utils\FunctionsScanner;
8
9
/**
10
 * Class to get gettext strings from php files returning arrays.
11
 */
12
class PhpCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
13
{
14
    public static $options = [
15
        // - false: to not extract comments
16
        // - empty string: to extract all comments
17
        // - non-empty string: to extract comments that start with that string
18
        // - array with strings to extract comments format.
19
        'extractComments' => false,
20
21
        'constants' => [],
22
23
        'functions' => [
24
            'gettext' => 'gettext',
25
            '__' => 'gettext',
26
            'ngettext' => 'ngettext',
27
            'n__' => 'ngettext',
28
            'pgettext' => 'pgettext',
29
            'p__' => 'pgettext',
30
            'dgettext' => 'dgettext',
31
            'd__' => 'dgettext',
32
            'dngettext' => 'dngettext',
33
            'dn__' => 'dngettext',
34
            'dpgettext' => 'dpgettext',
35
            'dp__' => 'dpgettext',
36
            'npgettext' => 'npgettext',
37
            'np__' => 'npgettext',
38
            'dnpgettext' => 'dnpgettext',
39
            'dnp__' => 'dnpgettext',
40
            'noop' => 'noop',
41
            'noop__' => 'noop',
42
        ],
43
    ];
44
45
    protected static $functionsScannerClass = 'Gettext\Utils\PhpFunctionsScanner';
46
47
    /**
48
     * {@inheritdoc}
49
     * @throws Exception
50
     */
51
    public static function fromString($string, Translations $translations, array $options = [])
52
    {
53
        static::fromStringMultiple($string, [$translations], $options);
54
    }
55
56
    /**
57
     * @inheritDoc
58
     * @throws Exception
59
     */
60
    public static function fromStringMultiple($string, array $translations, array $options = [])
61
    {
62
        $options += static::$options;
63
64
        /** @var FunctionsScanner $functions */
65
        $functions = new static::$functionsScannerClass($string);
66
67
        if ($options['extractComments'] !== false) {
68
            $functions->enableCommentsExtraction($options['extractComments']);
69
        }
70
71
        $functions->saveGettextFunctions($translations, $options);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 View Code Duplication
    public static function fromFileMultiple($file, array $translations, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        foreach (static::getFiles($file) as $file) {
80
            $options['file'] = $file;
81
            static::fromStringMultiple(static::readFile($file), $translations, $options);
82
        }
83
    }
84
85
86
    /**
87
     * Decodes a T_CONSTANT_ENCAPSED_STRING string.
88
     *
89
     * @param string $value
90
     *
91
     * @return string
92
     */
93
    public static function convertString($value)
94
    {
95
        if (strpos($value, '\\') === false) {
96
            return substr($value, 1, -1);
97
        }
98
99
        if ($value[0] === "'") {
100
            return strtr(substr($value, 1, -1), ['\\\\' => '\\', '\\\'' => '\'']);
101
        }
102
103
        $value = substr($value, 1, -1);
104
105
        return preg_replace_callback(
106
            '/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/',
107
            function ($match) {
108
                switch ($match[1][0]) {
109
                    case 'n':
110
                        return "\n";
111
                    case 'r':
112
                        return "\r";
113
                    case 't':
114
                        return "\t";
115
                    case 'v':
116
                        return "\v";
117
                    case 'e':
118
                        return "\e";
119
                    case 'f':
120
                        return "\f";
121
                    case '$':
122
                        return '$';
123
                    case '"':
124
                        return '"';
125
                    case '\\':
126
                        return '\\';
127
                    case 'x':
128
                        return chr(hexdec(substr($match[1], 1)));
129
                    case 'u':
130
                        return static::unicodeChar(hexdec(substr($match[1], 1)));
131
                    default:
132
                        return chr(octdec($match[1]));
133
                }
134
            },
135
            $value
136
        );
137
    }
138
139
    /**
140
     * @param $dec
141
     * @return string|null
142
     * @see http://php.net/manual/en/function.chr.php#118804
143
     */
144
    protected static function unicodeChar($dec)
145
    {
146
        if ($dec < 0x80) {
147
            return chr($dec);
148
        }
149
150
        if ($dec < 0x0800) {
151
            return chr(0xC0 + ($dec >> 6))
152
                . chr(0x80 + ($dec & 0x3f));
153
        }
154
155
        if ($dec < 0x010000) {
156
            return chr(0xE0 + ($dec >> 12))
157
                . chr(0x80 + (($dec >> 6) & 0x3f))
158
                . chr(0x80 + ($dec & 0x3f));
159
        }
160
161
        if ($dec < 0x200000) {
162
            return chr(0xF0 + ($dec >> 18))
163
                . chr(0x80 + (($dec >> 12) & 0x3f))
164
                . chr(0x80 + (($dec >> 6) & 0x3f))
165
                . chr(0x80 + ($dec & 0x3f));
166
        }
167
168
        return null;
169
    }
170
}
171