Completed
Pull Request — master (#546)
by Richard
09:47
created

Kint_Object_Blob   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 162
Duplicated Lines 13.58 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 22
loc 162
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 3
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 10 3
A getValueShort() 0 6 2
A transplant() 0 7 1
B strlen() 9 14 5
B substr() 9 14 6
C detectEncoding() 0 26 8
C escape() 4 40 11

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
class Kint_Object_Blob extends Kint_Object
4
{
5
    /**
6
     * @var array Character encodings to detect
7
     *
8
     * @see http://php.net/manual/en/function.mb-detect-order.php
9
     *
10
     * In practice, mb_detect_encoding can only successfully determine the
11
     * difference between the following common charsets at once without
12
     * breaking things for one of the other charsets:
13
     * - ASCII
14
     * - UTF-8
15
     * - SJIS
16
     * - EUC-JP
17
     *
18
     * If the array contains 'Windows-1252' special checking will be done
19
     * *after* all other encodings have failed. (Since it's likely to match
20
     * almost anything)
21
     *
22
     * The order of the charsets is significant. If you put UTF-8 before ASCII
23
     * it will never match ASCII, because UTF-8 is a superset of ASCII.
24
     * Similarly, SJIS and EUC-JP frequently match UTF-8 strings, so you should
25
     * check UTF-8 first. SJIS and EUC-JP seem to work either way, but SJIS is
26
     * more common so it should probably be first.
27
     *
28
     * Keep this behavior in mind when setting up your char_encodings array.
29
     *
30
     * Note that HHVM doesn't support SJIS or EUC-JP making them moot.
31
     */
32
    public static $char_encodings = array(
33
        'ASCII',
34
        'UTF-8',
35
    );
36
37
    public $type = 'string';
38
    public $encoding = false;
39
    public $hints = array('string');
40
41
    public function getType()
42
    {
43
        if ($this->encoding === false) {
44
            return 'binary '.$this->type;
45
        } elseif ($this->encoding === 'ASCII') {
46
            return $this->type;
47
        } else {
48
            return $this->encoding.' '.$this->type;
49
        }
50
    }
51
52
    public function getValueShort()
53
    {
54
        if ($rep = $this->value) {
55
            return '"'.$rep->contents.'"';
56
        }
57
    }
58
59
    public function transplant(Kint_Object $new)
60
    {
61
        $new = parent::transplant($new);
62
        $new->encoding = $this->encoding;
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist in Kint_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
64
        return $new;
65
    }
66
67
    public static function strlen($string, $encoding = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69 View Code Duplication
        if (extension_loaded('mbstring')) {
70
            if ($encoding === false) {
71
                $encoding = self::detectEncoding($string);
72
            }
73
74
            if ($encoding && $encoding !== 'ASCII') {
75
                return mb_strlen($string, $encoding);
76
            }
77
        }
78
79
        return strlen($string);
80
    }
81
82
    public static function substr($string, $start, $length = null, $encoding = false)
83
    {
84 View Code Duplication
        if (extension_loaded('mbstring')) {
85
            if ($encoding === false) {
86
                $encoding = self::detectEncoding($string);
87
            }
88
89
            if ($encoding && $encoding !== 'ASCII') {
90
                return mb_substr($string, $start, $length, $encoding);
91
            }
92
        }
93
94
        return substr($string, $start, isset($length) ? $length : PHP_INT_MAX);
95
    }
96
97
    public static function detectEncoding($string)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
98
    {
99
        if (extension_loaded('mbstring')) {
100
            if ($ret = mb_detect_encoding($string, array_diff(self::$char_encodings, array('Windows-1252')), true)) {
101
                return $ret;
102
            } elseif (!in_array('Windows-1252', self::$char_encodings) || preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x81\x8D\x8F\x90\x9D]/', $string)) {
103
                return false;
104
            } else {
105
                return 'Windows-1252';
106
            }
107
        }
108
109
        if (!extension_loaded('iconv')) {
110
            return 'UTF-8';
111
        }
112
113
        $md5 = md5($string);
114
        foreach (self::$char_encodings as $encoding) {
115
            // fuck knows why, //IGNORE and //TRANSLIT still throw notice
116
            if (md5(@iconv($encoding, $encoding, $string)) === $md5) {
117
                return $encoding;
118
            }
119
        }
120
121
        return false;
122
    }
123
124
    public static function escape($string, $encoding = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
125
    {
126
        static $show_dep = true;
127
128 View Code Duplication
        if ($show_dep) {
129
            trigger_error('Kint_Object_Blob::escape() is deprecated and will be removed in Kint 3.0. Use renderer-specific escape methods instead.', KINT_PHP53 ? E_USER_DEPRECATED : E_USER_NOTICE);
130
            $show_dep = false;
131
        }
132
133
        if (empty($string)) {
134
            return $string;
135
        }
136
137
        if (Kint::$enabled_mode === Kint::MODE_TEXT) {
138
            return $string;
139
        }
140
141
        if (Kint::$enabled_mode === Kint::MODE_CLI) {
142
            return str_replace("\x1b", '\\x1b', $string);
143
        }
144
145
        if ($encoding === false) {
146
            $encoding = self::detectEncoding($string);
147
        }
148
149
        $original_encoding = $encoding;
150
151
        if ($encoding === false || $encoding === 'ASCII') {
152
            $encoding = 'UTF-8';
153
        }
154
155
        $string = htmlspecialchars($string, ENT_NOQUOTES, $encoding);
156
157
        // this call converts all non-ASCII characters into numeirc htmlentities
158
        if ($original_encoding !== 'ASCII' && function_exists('mb_encode_numericentity')) {
159
            $string = mb_encode_numericentity($string, array(0x80, 0xffff, 0, 0xffff), $encoding);
160
        }
161
162
        return $string;
163
    }
164
}
165