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

Zend2Serializer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 5
dl 45
loc 45
ccs 0
cts 32
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C serialize() 39 39 12

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\Serializer;
4
5
use Fxmlrpc\Serialization\Exception\InvalidTypeException;
6
use Fxmlrpc\Serialization\Exception\SerializerException;
7
use Fxmlrpc\Serialization\Serializer;
8
use Fxmlrpc\Serialization\Value\Base64;
9
use Zend\XmlRpc\AbstractValue;
10
use Zend\XmlRpc\Request;
11
12
/**
13
 * Serializer creates XML from native PHP types using XML RPC extension.
14
 *
15
 * @author Márk Sági-Kazár <[email protected]>
16
 */
17 View Code Duplication
final class Zend2Serializer implements Serializer
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...
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function serialize($method, array $params = [])
23
    {
24
        $toBeVisited = [&$params];
25
26
        while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {
27
            $type = gettype($value);
28
29
            if ($type === 'array') {
30
                // Zend converts non-zero-indexed arrays to structs
31
                if ((array_keys($value) !== range(0, count($value) - 1)) && (array_keys($value) == range(1, count($value)))) {
32
                    $value = array_values($value);
33
                }
34
35
                foreach ($value as &$child) {
36
                    $toBeVisited[] = &$child;
37
                }
38
            } elseif ($type === 'object') {
39
                if ($value instanceof \DateTime) {
40
                    $value = AbstractValue::getXmlRpcValue($value->format('Ymd\TH:i:s'), AbstractValue::XMLRPC_TYPE_DATETIME);
41
                } elseif ($value instanceof Base64) {
42
                    $value = AbstractValue::getXmlRpcValue($value->getDecoded(), AbstractValue::XMLRPC_TYPE_BASE64);
43
                } else {
44
                    $value = get_object_vars($value);
45
                }
46
            } elseif ($type === 'resource') {
47
                throw new InvalidTypeException($value);
48
            }
49
50
            array_shift($toBeVisited);
51
        }
52
53
        $request = new Request($method, $params);
54
55
        try {
56
            return $request->saveXml();
57
        } catch (\Exception $e) {
58
            throw new SerializerException($e->getMessage(), $e->getCode(), $e);
59
        }
60
    }
61
}
62