Completed
Push — master ( 312590...670503 )
by Marco
03:19
created

XmlrpcDecoder::decodeValue()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 19.7662

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 59
ccs 25
cts 38
cp 0.6579
rs 6.4268
cc 13
eloc 39
nc 13
nop 1
crap 19.7662

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php namespace Comodojo\Xmlrpc;
2
3
use \Comodojo\Exception\XmlrpcException;
4
use \Exception;
5
6
/** 
7
 * XML-RPC decoder
8
 *
9
 * @package     Comodojo Spare Parts
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 * 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class XmlrpcDecoder {
25
26
    private $is_fault = false;
27
28 21
    public function __construct() {
29
30 21
        libxml_use_internal_errors(true);
31
32 21
    }
33
34
    /**
35
     * Decode an xmlrpc response
36
     *
37
     * @param   string  $response
38
     *
39
     * @return  array
40
     *
41
     * @throws  \Comodojo\Exception\XmlrpcException
42
     */
43 12
    public function decodeResponse($response) {
44
45 12
        $xml_data = simplexml_load_string($response);
46
47 12
        if ( $xml_data === false ) throw new XmlrpcException("Not a valid XMLRPC response");
48
49 9
        $data = array();
50
51
        try {
52
53 9
            if ( isset($xml_data->fault) ) {
54
55 3
                $this->is_fault = true;
56
57 3
                array_push($data, $this->decodeValue($xml_data->fault->value));
58
59 7
            } else if ( isset($xml_data->params) ) {
60
61 6
                foreach ( $xml_data->params->param as $param ) array_push($data, $this->decodeValue($param->value));
62
63 8
            } else throw new XmlrpcException("Uncomprensible response");
64
65 3
        } catch (XmlrpcException $xe) {
66
            
67
            throw $xe;
68
            
69
        }
70
71 9
        return isset($data[0]) ? $data[0] : $data;
72
73
    }
74
75 3
    public function isFault() {
76
77 3
        return $this->is_fault;
78
79
    }
80
81
    /**
82
     * Decode an xmlrpc request.
83
     *
84
     * Can handle single or multicall requests and return an array of: [method], [data]
85
     *
86
     * WARNING: in case of multicall, it will not throw any exception for an invalid
87
     * boxcarred request; a null value will be placed instead of array(method,params).
88
     *
89
     * @param   string  $request
90
     *
91
     * @return  array
92
     *
93
     * @throws  \Comodojo\Exception\XmlrpcException
94
     */
95 9
    public function decodeCall($request) {
96
97 9
        $xml_data = simplexml_load_string($request);
98
99 9
        if ( $xml_data === false ) throw new XmlrpcException("Not a valid XMLRPC call");
100
101 9
        if ( !isset($xml_data->methodName) ) throw new XmlrpcException("Uncomprensible request");
102
            
103 9
        $method_name = $this->decodeString($xml_data->methodName[0]);
104
105 9
        if ( $method_name == "system.multicall" ) {
106
107
            try {
108
        
109 6
                $data = $this->multicallDecode($xml_data);
110
111 2
            } catch (XmlrpcException $xe) {
112
                
113 4
                throw $xe;
114
115
            }
116
117 2
        } else {
118
119 3
            $parsed = array();
120
121
            try {
122
            
123 3
                foreach ( $xml_data->params->param as $param ) $parsed[] = $this->decodeValue($param->value);
124
125 1
            } catch (XmlrpcException $xe) {
126
                
127
                throw $xe;
128
129
            }
130
131 3
            $data = array($method_name, $parsed);
132
133
        }
134
135 9
        return $data;
136
137
    }
138
139
    /**
140
     * Decode an xmlrpc multicall
141
     *
142
     * @param   string  $request
143
     *
144
     * @return  array
145
     *
146
     * @throws  \Comodojo\Exception\XmlrpcException
147
     */
148
    public function decodeMulticall($request) {
149
150
        $xml_data = simplexml_load_string($request);
151
152
        if ( $xml_data === false ) throw new XmlrpcException("Not a valid XMLRPC multicall");
153
154
        if ( !isset($xml_data->methodName) ) throw new XmlrpcException("Uncomprensible multicall request");
155
156
        if ( $this->decodeString($xml_data->methodName[0]) != "system.multicall" ) throw new XmlrpcException("Invalid multicall request");
157
158
        try {
159
        
160
            $data = $this->multicallDecode($xml_data);
161
162
        } catch (XmlrpcException $xe) {
163
            
164
            throw $xe;
165
166
        }
167
168
        return $data;
169
170
    }
171
172
    /**
173
     * Decode a value from xmlrpc data
174
     *
175
     * @param   mixed   $value
176
     *
177
     * @return  mixed
178
     *
179
     * @throws  \Comodojo\Exception\XmlrpcException
180
     */
181 15
    private function decodeValue($value) {
182
183 15
        $children = $value->children();
184
185 15
        if ( count($children) != 1 ) throw new XmlrpcException("Cannot decode value: invalid value element");
186
187 15
        $child = $children[0];
188
189 15
        $child_type = $child->getName();
190
191
        switch ( $child_type ) {
192
193 15
            case "i4":
194 15
            case "int":
195 6
                $return_value = $this->decodeInt($child);
196 6
            break;
197
198 15
            case "double":
199
                $return_value = $this->decodeDouble($child);
200
            break;
201
202 15
            case "boolean":
203 3
                $return_value = $this->decodeBool($child);
204 3
            break;
205
206 15
            case "base64":
207
                $return_value = $this->decodeBase($child);
208
            break;
209
            
210 15
            case "dateTime.iso8601":
211
                $return_value = $this->decodeIso8601Datetime($child);
212
            break;
213
214 15
            case "string":
215 15
                $return_value = $this->decodeString($child);
216 15
            break;
217
218 15
            case "array":
219 12
                $return_value = $this->decodeArray($child);
220 12
            break;
221
            
222 12
            case "struct":
223 12
                $return_value = $this->decodeStruct($child);
224 12
            break;
225
            
226
            case "nil":
227
            case "ex:nil":
228
                $return_value = $this->decodeNil();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $return_value is correct as $this->decodeNil() (which targets Comodojo\Xmlrpc\XmlrpcDecoder::decodeNil()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
229
            break;
230
            
231
            default:
232
                throw new XmlrpcException("Cannot decode value: invalid value type");
233
            break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
234
235
        }
236
237 15
        return $return_value;
238
239
    }
240
241
    /**
242
     * Decode an XML-RPC <base64> element
243
     */
244
    private function decodeBase($base64) {
245
246
        return base64_decode($this->decodeString($base64));
247
248
    }
249
250
    /**
251
     * Decode an XML-RPC <boolean> element
252
     */
253 3
    private function decodeBool($boolean) {
254
255 3
        return filter_var($boolean, FILTER_VALIDATE_BOOLEAN);
256
257
    }
258
259
    /**
260
     * Decode an XML-RPC <dateTime.iso8601> element
261
     */
262
    private function decodeIso8601Datetime($date_time) {
263
        
264
        return strtotime($date_time);
265
266
    }
267
268
    /**
269
     * Decode an XML-RPC <double> element
270
     */
271
    private function decodeDouble($double) {
272
273
        return (double) ($this->decodeString($double));
274
275
    }
276
277
    /**
278
     * Decode an XML-RPC <int> or <i4> element
279
     */
280 6
    private function decodeInt($int) {
281
282 6
        return filter_var($int, FILTER_VALIDATE_INT);
283
284
    }
285
286
    /**
287
     * Decode an XML-RPC <string>
288
     */
289 18
    private function decodeString($string) {
290
291 18
        return (string) $string;
292
293
    }
294
295
    /**
296
     * Decode an XML-RPC <nil/>
297
     */
298
    private function decodeNil() {
299
300
        return null;
301
302
    }
303
304
    /**
305
     * Decode an XML-RPC <struct>
306
     */
307 12
    private function decodeStruct($struct) {
308
309 12
        $return_value = array();
310
311 12
        foreach ( $struct->member as $member ) {
312
313 12
            $name = $member->name."";
314 12
            $value = $this->decodeValue($member->value);
315 12
            $return_value[$name] = $value;
316
317 4
        }
318
319 12
        return $return_value;
320
321
    }
322
323
    /** 
324
     * Decode an XML-RPC <array> element
325
     */
326 12
    private function decodeArray($array) {
327
328 12
        $return_value = array();
329
330 12
        foreach ( $array->data->value as $value ) {
331
332 12
            $return_value[] = $this->decodeValue($value);
333
334 4
        }
335
336 12
        return $return_value;
337
338
    }
339
340
    /** 
341
     * Decode an XML-RPC multicall request (internal)
342
     * @param \SimpleXMLElement $xml_data
343
     */
344 6
    private function multicallDecode($xml_data) {
345
346 6
        $data = array();
347
348
        try {
349
350 6
            $calls = $xml_data->params->param->value->children();
351
352 6
            $calls_array = $this->decodeArray($calls[0]);
353
354 6
            foreach ( $calls_array as $call ) {
355
                
356 6
                $data[] = (!isset($call['methodName']) || !isset($call['params'])) ? null : array($call['methodName'], $call['params']);
357
358 2
            }
359
            
360 2
        } catch (XmlrpcException $xe) {
361
            
362
            throw $xe;
363
364
        }
365
        
366 6
        return $data;
367
368
    }
369
370
}
371