Completed
Push — master ( 6aef24...286712 )
by Márk
06:17
created

Zend2Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 46
loc 46
ccs 0
cts 30
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 40 40 7

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 Fxmlrpc\Serialization\Parser;
4
5
use Fxmlrpc\Serialization\Exception\ParserException;
6
use Fxmlrpc\Serialization\Parser;
7
use Fxmlrpc\Serialization\Value\Base64Value;
8
use Zend\XmlRpc\Response;
9
10
/**
11
 * Parser to parse XML responses into its PHP representation using XML RPC extension.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15 View Code Duplication
final class Zend2Parser implements Parser
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function parse($xmlString, &$isFault)
21
    {
22
        $response = new Response();
23
24
        try {
25
            $response->loadXml($xmlString);
26
        } catch (\Exception $e) {
27
            throw new ParserException($e->getMessage(), $e->getCode(), $e);
28
        }
29
30
        $isFault = $response->isFault();
31
32
        if ($isFault) {
33
            $fault = $response->getFault();
34
35
            return [
36
                'faultCode'   => $fault->getCode(),
37
                'faultString' => $fault->getMessage(),
38
            ];
39
        }
40
41
        $result = $response->getReturnValue();
42
43
        $toBeVisited = [&$result];
44
45
        while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {
46
            $type = gettype($value);
47
            if ($type === 'array') {
48
                foreach ($value as &$element) {
49
                    $toBeVisited[] = &$element;
50
                }
51
52
                reset($value); // Reset all array pointers
53
            }
54
55
            array_shift($toBeVisited);
56
        }
57
58
        return $result;
59
    }
60
}
61