Conditions | 25 |
Paths | 32 |
Total Lines | 132 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
90 | public function encodeEntities($data, $srcEncoding = '', $destEncoding = '') |
||
91 | { |
||
92 | if ($srcEncoding == '') { |
||
93 | // lame, but we know no better... |
||
94 | $srcEncoding = PhpXmlRpc::$xmlrpc_internalencoding; |
||
95 | } |
||
96 | |||
97 | $conversion = strtoupper($srcEncoding . '_' . $destEncoding); |
||
98 | switch ($conversion) { |
||
99 | case 'ISO-8859-1_': |
||
100 | case 'ISO-8859-1_US-ASCII': |
||
101 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
102 | $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData); |
||
103 | break; |
||
104 | |||
105 | case 'ISO-8859-1_UTF-8': |
||
106 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
107 | $escapedData = utf8_encode($escapedData); |
||
108 | break; |
||
109 | |||
110 | case 'ISO-8859-1_ISO-8859-1': |
||
111 | case 'US-ASCII_US-ASCII': |
||
112 | case 'US-ASCII_UTF-8': |
||
113 | case 'US-ASCII_': |
||
114 | case 'US-ASCII_ISO-8859-1': |
||
115 | case 'UTF-8_UTF-8': |
||
116 | //case 'CP1252_CP1252': |
||
117 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
118 | break; |
||
119 | |||
120 | case 'UTF-8_': |
||
121 | case 'UTF-8_US-ASCII': |
||
122 | case 'UTF-8_ISO-8859-1': |
||
123 | // NB: this will choke on invalid UTF-8, going most likely beyond EOF |
||
124 | $escapedData = ''; |
||
125 | // be kind to users creating string xmlrpc values out of different php types |
||
126 | $data = (string)$data; |
||
127 | $ns = strlen($data); |
||
128 | for ($nn = 0; $nn < $ns; $nn++) { |
||
129 | $ch = $data[$nn]; |
||
130 | $ii = ord($ch); |
||
131 | // 7 bits: 0bbbbbbb (127) |
||
132 | if ($ii < 128) { |
||
133 | /// @todo shall we replace this with a (supposedly) faster str_replace? |
||
134 | switch ($ii) { |
||
135 | case 34: |
||
136 | $escapedData .= '"'; |
||
137 | break; |
||
138 | case 38: |
||
139 | $escapedData .= '&'; |
||
140 | break; |
||
141 | case 39: |
||
142 | $escapedData .= '''; |
||
143 | break; |
||
144 | case 60: |
||
145 | $escapedData .= '<'; |
||
146 | break; |
||
147 | case 62: |
||
148 | $escapedData .= '>'; |
||
149 | break; |
||
150 | default: |
||
151 | $escapedData .= $ch; |
||
152 | } // switch |
||
153 | } // 11 bits: 110bbbbb 10bbbbbb (2047) |
||
154 | elseif ($ii >> 5 == 6) { |
||
155 | $b1 = ($ii & 31); |
||
156 | $ii = ord($data[$nn + 1]); |
||
157 | $b2 = ($ii & 63); |
||
158 | $ii = ($b1 * 64) + $b2; |
||
159 | $ent = sprintf('&#%d;', $ii); |
||
160 | $escapedData .= $ent; |
||
161 | $nn += 1; |
||
162 | } // 16 bits: 1110bbbb 10bbbbbb 10bbbbbb |
||
163 | elseif ($ii >> 4 == 14) { |
||
164 | $b1 = ($ii & 15); |
||
165 | $ii = ord($data[$nn + 1]); |
||
166 | $b2 = ($ii & 63); |
||
167 | $ii = ord($data[$nn + 2]); |
||
168 | $b3 = ($ii & 63); |
||
169 | $ii = ((($b1 * 64) + $b2) * 64) + $b3; |
||
170 | $ent = sprintf('&#%d;', $ii); |
||
171 | $escapedData .= $ent; |
||
172 | $nn += 2; |
||
173 | } // 21 bits: 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
||
174 | elseif ($ii >> 3 == 30) { |
||
175 | $b1 = ($ii & 7); |
||
176 | $ii = ord($data[$nn + 1]); |
||
177 | $b2 = ($ii & 63); |
||
178 | $ii = ord($data[$nn + 2]); |
||
179 | $b3 = ($ii & 63); |
||
180 | $ii = ord($data[$nn + 3]); |
||
181 | $b4 = ($ii & 63); |
||
182 | $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4; |
||
183 | $ent = sprintf('&#%d;', $ii); |
||
184 | $escapedData .= $ent; |
||
185 | $nn += 3; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | // when converting to latin-1, do not be so eager with using entities for characters 160-255 |
||
190 | if ($conversion == 'UTF-8_ISO-8859-1') { |
||
191 | $escapedData = str_replace(array_slice($this->xml_iso88591_Entities['out'], 32), array_slice($this->xml_iso88591_Entities['in'], 32), $escapedData); |
||
192 | } |
||
193 | break; |
||
194 | |||
195 | /* |
||
196 | case 'CP1252_': |
||
197 | case 'CP1252_US-ASCII': |
||
198 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
199 | $escapedData = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escapedData); |
||
200 | $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); |
||
201 | break; |
||
202 | case 'CP1252_UTF-8': |
||
203 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
204 | /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all allone will NOT convert them) |
||
205 | $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); |
||
206 | $escapedData = utf8_encode($escapedData); |
||
207 | break; |
||
208 | case 'CP1252_ISO-8859-1': |
||
209 | $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
||
210 | // we might as well replace all funky chars with a '?' here, but we are kind and leave it to the receiving application layer to decide what to do with these weird entities... |
||
211 | $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); |
||
212 | break; |
||
213 | */ |
||
214 | |||
215 | default: |
||
216 | $escapedData = ''; |
||
217 | error_log('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported..."); |
||
218 | } |
||
219 | |||
220 | return $escapedData; |
||
221 | } |
||
222 | |||
274 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.