String_   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 2.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 19
c 3
b 0
f 1
lcom 1
cbo 2
dl 3
loc 140
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubNodeNames() 0 3 1
A parse() 0 18 3
C parseEscapeSequences() 3 28 7
B codePointToUtf8() 0 16 5
A parseDocString() 0 11 2

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 PhpParser\Node\Scalar;
4
5
use PhpParser\Error;
6
use PhpParser\Node\Scalar;
7
8
class String_ extends Scalar
9
{
10
    /** @var string String value */
11
    public $value;
12
13
    protected static $replacements = array(
14
        '\\' => '\\',
15
        '$'  =>  '$',
16
        'n'  => "\n",
17
        'r'  => "\r",
18
        't'  => "\t",
19
        'f'  => "\f",
20
        'v'  => "\v",
21
        'e'  => "\x1B",
22
    );
23
24
    /**
25
     * Constructs a string scalar node.
26
     *
27
     * @param string $value      Value of the string
28
     * @param array  $attributes Additional attributes
29
     */
30
    public function __construct($value, array $attributes = array()) {
31
        parent::__construct($attributes);
32
        $this->value = $value;
33
    }
34
35
    public function getSubNodeNames() {
36
        return array('value');
37
    }
38
39
    /**
40
     * @internal
41
     *
42
     * Parses a string token.
43
     *
44
     * @param string $str String token content
45
     * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
46
     *
47
     * @return string The parsed string
48
     */
49
    public static function parse($str, $parseUnicodeEscape = true) {
50
        $bLength = 0;
51
        if ('b' === $str[0]) {
52
            $bLength = 1;
53
        }
54
55
        if ('\'' === $str[$bLength]) {
56
            return str_replace(
57
                array('\\\\', '\\\''),
58
                array(  '\\',   '\''),
59
                substr($str, $bLength + 1, -1)
60
            );
61
        } else {
62
            return self::parseEscapeSequences(
63
                substr($str, $bLength + 1, -1), '"', $parseUnicodeEscape
64
            );
65
        }
66
    }
67
68
    /**
69
     * @internal
70
     *
71
     * Parses escape sequences in strings (all string types apart from single quoted).
72
     *
73
     * @param string      $str   String without quotes
74
     * @param null|string $quote Quote type
75
     * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
76
     *
77
     * @return string String with escape sequences parsed
78
     */
79
    public static function parseEscapeSequences($str, $quote, $parseUnicodeEscape = true) {
80
        if (null !== $quote) {
81
            $str = str_replace('\\' . $quote, $quote, $str);
82
        }
83
84
        $extra = '';
85
        if ($parseUnicodeEscape) {
86
            $extra = '|u\{([0-9a-fA-F]+)\}';
87
        }
88
89
        return preg_replace_callback(
90
            '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~',
91
            function($matches) {
92
                $str = $matches[1];
93
94
                if (isset(self::$replacements[$str])) {
95
                    return self::$replacements[$str];
96 View Code Duplication
                } elseif ('x' === $str[0] || 'X' === $str[0]) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
                    return chr(hexdec($str));
98
                } elseif ('u' === $str[0]) {
99
                    return self::codePointToUtf8(hexdec($matches[2]));
100
                } else {
101
                    return chr(octdec($str));
102
                }
103
            },
104
            $str
105
        );
106
    }
107
108
    private static function codePointToUtf8($num) {
109
        if ($num <= 0x7F) {
110
            return chr($num);
111
        }
112
        if ($num <= 0x7FF) {
113
            return chr(($num>>6) + 0xC0) . chr(($num&0x3F) + 0x80);
114
        }
115
        if ($num <= 0xFFFF) {
116
            return chr(($num>>12) + 0xE0) . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
117
        }
118
        if ($num <= 0x1FFFFF) {
119
            return chr(($num>>18) + 0xF0) . chr((($num>>12)&0x3F) + 0x80)
120
                 . chr((($num>>6)&0x3F) + 0x80) . chr(($num&0x3F) + 0x80);
121
        }
122
        throw new Error('Invalid UTF-8 codepoint escape sequence: Codepoint too large');
123
    }
124
125
    /**
126
     * @internal
127
     *
128
     * Parses a constant doc string.
129
     *
130
     * @param string $startToken Doc string start token content (<<<SMTHG)
131
     * @param string $str        String token content
132
     * @param bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes
133
     *
134
     * @return string Parsed string
135
     */
136
    public static function parseDocString($startToken, $str, $parseUnicodeEscape = true) {
137
        // strip last newline (thanks tokenizer for sticking it into the string!)
138
        $str = preg_replace('~(\r\n|\n|\r)\z~', '', $str);
139
140
        // nowdoc string
141
        if (false !== strpos($startToken, '\'')) {
142
            return $str;
143
        }
144
145
        return self::parseEscapeSequences($str, null, $parseUnicodeEscape);
146
    }
147
}
148