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 |
||
43 | class Escaper |
||
44 | { |
||
45 | /** @type string REGEX_CHARACTER_TO_ESCAPE Characters that would cause a dumped string to require double quoting */ |
||
46 | const REGEX_CHARACTER_TO_ESCAPE = "[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9"; |
||
47 | /** @type string REGEX_ESCAPED_CHARACTER Regex fragment that matches an escaped char in a double quoted string */ |
||
48 | const REGEX_ESCAPED_CHARACTER = |
||
49 | "\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)"; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @type array $escapees Mapping arrays for escaping a double quoted string. The backslash is first to ensure |
||
54 | * proper escaping because str_replace operates iteratively on the input arrays. This ordering of the characters |
||
55 | * avoids the use of strtr, which performs more slowly |
||
56 | */ |
||
57 | protected static $escapees = ["\\", "\\\\", "\\\"", "\"", |
||
58 | "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", |
||
59 | "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", |
||
60 | "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", |
||
61 | "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f", |
||
62 | "\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9"]; |
||
63 | /** |
||
64 | * @type array $escaped Mapping arrays for escaping a double quoted string. The backslash is first to ensure |
||
65 | * proper escaping because str_replace operates iteratively on the input arrays. This ordering of the characters |
||
66 | * avoids the use of strtr, which performs more slowly |
||
67 | */ |
||
68 | protected static $escaped = ["\\\\", "\\\"", "\\\\", "\\\"", |
||
69 | "\\0", "\\x01", "\\x02", "\\x03", "\\x04", "\\x05", "\\x06", "\\a", |
||
70 | "\\b", "\\t", "\\n", "\\v", "\\f", "\\r", "\\x0e", "\\x0f", |
||
71 | "\\x10", "\\x11", "\\x12", "\\x13", "\\x14", "\\x15", "\\x16", "\\x17", |
||
72 | "\\x18", "\\x19", "\\x1a", "\\e", "\\x1c", "\\x1d", "\\x1e", "\\x1f", |
||
73 | "\\N", "\\_", "\\L", "\\P"]; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Determines if a PHP value would require double quoting in YAML |
||
78 | * |
||
79 | * @param string $value A PHP value |
||
80 | * |
||
81 | * @return bool True if the value would require double quotes. |
||
82 | */ |
||
83 | public static function requiresDoubleQuoting($value) |
||
87 | |||
88 | /** |
||
89 | * Escapes and surrounds a PHP value with double quotes |
||
90 | * |
||
91 | * @param string $value A PHP value |
||
92 | * |
||
93 | * @return string The quoted, escaped string |
||
94 | */ |
||
95 | public static function escapeWithDoubleQuotes($value) |
||
99 | |||
100 | /** |
||
101 | * Determines if a PHP value would require single quoting in YAML |
||
102 | * |
||
103 | * @param string $value A PHP value |
||
104 | * |
||
105 | * @return bool True if the value would require single quotes. |
||
106 | */ |
||
107 | public static function requiresSingleQuoting($value) |
||
108 | { |
||
109 | // Determines if a PHP value is entirely composed of a value that would |
||
110 | // require single quoting in YAML. |
||
111 | if (in_array(strtolower($value), ["null", "~", "true", "false", "y", "n", "yes", "no", "on", "off"])) { |
||
112 | return true; |
||
113 | } |
||
114 | |||
115 | // Determines if the PHP value contains any single characters that would |
||
116 | // cause it to require single quoting in YAML. |
||
117 | return preg_match("/[ \\s ' \" \\: \\{ \\} \\[ \\] , & \\* \\# \\?] | \\A[ \\- ? | < > = ! % @ ` ]/x", $value); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Escapes and surrounds a PHP value with single quotes |
||
122 | * |
||
123 | * @param string $value A PHP value |
||
124 | * |
||
125 | * @return string The quoted, escaped string |
||
126 | */ |
||
127 | public static function escapeWithSingleQuotes($value) |
||
131 | |||
132 | /** |
||
133 | * Unescapes a single quoted string |
||
134 | * |
||
135 | * @param string $value A single quoted string |
||
136 | * |
||
137 | * @return string The unescaped string |
||
138 | */ |
||
139 | public function unescapeSingleQuotedString($value) |
||
143 | |||
144 | /** |
||
145 | * Unescapes a double quoted string |
||
146 | * |
||
147 | * @param string $value A double quoted string |
||
148 | * |
||
149 | * @return string The unescaped string |
||
150 | */ |
||
151 | public function unescapeDoubleQuotedString($value) |
||
160 | |||
161 | /** |
||
162 | * Unescapes a character that was found in a double-quoted string |
||
163 | * |
||
164 | * @param string $value An escaped character |
||
165 | * |
||
166 | * @return string The unescaped character |
||
167 | */ |
||
168 | protected function unescapeCharacter($value) |
||
229 | } |
||
230 |
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.