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

Zend1Serializer::serialize()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 39
Code Lines 24

Duplication

Lines 39
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 39
loc 39
ccs 0
cts 32
cp 0
rs 5.1612
cc 12
eloc 24
nc 15
nop 2
crap 156

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