Completed
Push — master ( a496f2...51cf07 )
by Oscar
01:58
created

PhpCode   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 101
Duplicated Lines 10.89 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
lcom 1
cbo 3
dl 11
loc 101
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 11 11 2
C convertString() 0 41 13
B unicodeChar() 0 24 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 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 = '')
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...
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] === "'") {
47
            return substr(str_replace(['\\\\', '\\\''], ['\\', '\''], $value), 1, -1);
48
        }
49
50
        $value = substr($value, 1, -1);
51
52
        return preg_replace_callback('/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/', function ($match) {
53
            switch ($match[1]) {
54
                case 'n':
55
                    return "\n";
56
                case 'r':
57
                    return "\r";
58
                case 't':
59
                    return "\t";
60
                case 'v':
61
                    return "\v";
62
                case 'e':
63
                    return "\e";
64
                case 'f':
65
                    return "\f";
66
                case '$':
67
                    return '$';
68
                case '"':
69
                    return '"';
70
                case '\\':
71
                    return '\\';
72
            }
73
74
            switch ($match[1][0]) {
75
                case 'x':
76
                    return chr(hexdec(substr($match[0], 1)));
77
78
                case 'u':
79
                    return self::unicodeChar(hexdec(substr($match[0], 1)));
80
            }
81
82
            return chr(octdec($match[0]));
83
        }, $value);
84
    }
85
86
    //http://php.net/manual/en/function.chr.php#118804
87
    private static function unicodeChar($dec)
88
    {
89
        if ($dec < 0x80) {
90
            return chr($dec);
91
        }
92
93
        if ($dec < 0x0800) {
94
            return chr(0xC0 + ($dec >> 6))
95
                .chr(0x80 + ($dec & 0x3f));
96
        }
97
98
        if ($dec < 0x010000) {
99
            return chr(0xE0 + ($dec >> 12))
100
                    .chr(0x80 + (($dec >> 6) & 0x3f))
101
                    .chr(0x80 + ($dec & 0x3f));
102
        }
103
104
        if ($dec < 0x200000) {
105
            return chr(0xF0 + ($dec >> 18))
106
                    .chr(0x80 + (($dec >> 12) & 0x3f))
107
                    .chr(0x80 + (($dec >> 6) & 0x3f))
108
                    .chr(0x80 + ($dec & 0x3f));
109
        }
110
    }
111
}
112