XML::_serialize()   C
last analyzed

Complexity

Conditions 14
Paths 14

Size

Total Lines 46
Code Lines 37

Duplication

Lines 10
Ratio 21.74 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 46
rs 5.0744
cc 14
eloc 37
nc 14
nop 1

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 PhpParser\Serializer;
4
5
use XMLWriter;
6
use PhpParser\Node;
7
use PhpParser\Comment;
8
use PhpParser\Serializer;
9
10
class XML implements Serializer
11
{
12
    protected $writer;
13
14
    /**
15
     * Constructs a XML serializer.
16
     */
17
    public function __construct() {
18
        $this->writer = new XMLWriter;
19
        $this->writer->openMemory();
20
        $this->writer->setIndent(true);
21
    }
22
23
    public function serialize(array $nodes) {
24
        $this->writer->flush();
25
        $this->writer->startDocument('1.0', 'UTF-8');
26
27
        $this->writer->startElement('AST');
28
        $this->writer->writeAttribute('xmlns:node',      'http://nikic.github.com/PHPParser/XML/node');
29
        $this->writer->writeAttribute('xmlns:subNode',   'http://nikic.github.com/PHPParser/XML/subNode');
30
        $this->writer->writeAttribute('xmlns:attribute', 'http://nikic.github.com/PHPParser/XML/attribute');
31
        $this->writer->writeAttribute('xmlns:scalar',    'http://nikic.github.com/PHPParser/XML/scalar');
32
33
        $this->_serialize($nodes);
34
35
        $this->writer->endElement();
36
37
        return $this->writer->outputMemory();
38
    }
39
40
    protected function _serialize($node) {
41
        if ($node instanceof Node) {
42
            $this->writer->startElement('node:' . $node->getType());
43
44 View Code Duplication
            foreach ($node->getAttributes() as $name => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
                $this->writer->startElement('attribute:' . $name);
46
                $this->_serialize($value);
47
                $this->writer->endElement();
48
            }
49
50 View Code Duplication
            foreach ($node as $name => $subNode) {
0 ignored issues
show
Bug introduced by
The expression $node of type object<PhpParser\Node> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
51
                $this->writer->startElement('subNode:' . $name);
52
                $this->_serialize($subNode);
53
                $this->writer->endElement();
54
            }
55
56
            $this->writer->endElement();
57
        } elseif ($node instanceof Comment) {
58
            $this->writer->startElement('comment');
59
            $this->writer->writeAttribute('isDocComment', $node instanceof Comment\Doc ? 'true' : 'false');
60
            $this->writer->writeAttribute('line', (string) $node->getLine());
61
            $this->writer->text($node->getText());
62
            $this->writer->endElement();
63
        } elseif (is_array($node)) {
64
            $this->writer->startElement('scalar:array');
65
            foreach ($node as $subNode) {
66
                $this->_serialize($subNode);
67
            }
68
            $this->writer->endElement();
69
        } elseif (is_string($node)) {
70
            $this->writer->writeElement('scalar:string', $node);
71
        } elseif (is_int($node)) {
72
            $this->writer->writeElement('scalar:int', (string) $node);
73
        } elseif (is_float($node)) {
74
            // TODO Higher precision conversion?
75
            $this->writer->writeElement('scalar:float', (string) $node);
76
        } elseif (true === $node) {
77
            $this->writer->writeElement('scalar:true');
78
        } elseif (false === $node) {
79
            $this->writer->writeElement('scalar:false');
80
        } elseif (null === $node) {
81
            $this->writer->writeElement('scalar:null');
82
        } else {
83
            throw new \InvalidArgumentException('Unexpected node type');
84
        }
85
    }
86
}
87