Completed
Pull Request — master (#12)
by Márk
03:16
created

ZendSerializer::serialize()   C

Complexity

Conditions 12
Paths 15

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 32
cp 0
rs 5.1612
c 0
b 0
f 0
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
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
final class ZendSerializer implements Serializer
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