SimpleXml   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 45.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 32
loc 70
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 13 1
B arrayToSimpleXml() 32 32 5
A specialChars() 0 4 1

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
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Output\Xml;
14
15
use Apix\Output\Xml;
16
17
class SimpleXml extends Xml
18
{
19
20
    /**
21
     * SimpleXML handler
22
     * {@inheritdoc}
23
     */
24
    public function encode(array $data, $rootNode='root')
25
    {
26
        $str = sprintf('<?xml version="%s" encoding="%s"?><%s />',
27
            $this->version,
28
            $this->encoding,
29
            $rootNode
30
        );
31
32
        $x = new \SimpleXMLElement($str);
33
        $this->arrayToSimpleXml($x, $data);
34
35
        return $x->asXML();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $x->asXML(); of type string|false adds false to the return on line 35 which is incompatible with the return type declared by the abstract method Apix\Output\AbstractOutput::encode of type string. It seems like you forgot to handle an error condition.
Loading history...
36
    }
37
38
    /**
39
     * Array to Simple XML
40
     *
41
     * @param \SimpleXMLElement $xml
0 ignored issues
show
Bug introduced by
There is no parameter named $xml. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     * @param array             $array
43
     */
44 View Code Duplication
    protected function arrayToSimpleXml(\SimpleXMLElement $x, array $array)
0 ignored issues
show
Duplication introduced by
This method 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...
45
    {
46
        foreach ($array as $k => $v) {
47
            if (is_array($v)) {
48
                if (is_int($k)) {
49
                    $k = $this->items_key;
50
                }
51
52
                // @codeCoverageIgnoreStart
53
                // Attributes needs to be reviewd!!!
54
                #if ($k == '@attributes') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
                #    foreach ($v as $k => $v) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
                #        $x->addAttribute($k, $v);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
                #    }
58
                // @codeCoverageIgnoreEnd
59
                #} else {
60
                    $child = $x->addChild($k);
61
                    $this->arrayToSimpleXml($child, $v);
62
                #}
63
            } else {
64
                if (is_int($k)) {
65
                    $k = $this->item_key;
66
                }
67
68
                // $xml->addAttribute(
69
                //     $k,
70
                //     htmlspecialchars($v, ENT_QUOTES, $this->encoding)
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
                // )
72
                $x->addChild($k, $this->specialChars($v));
73
            }
74
        }
75
    }
76
77
    /**
78
     * Decodes any special characters entities.
79
     * XmlWriter does this automatically.
80
     */
81
    public function specialChars($var)
82
    {
83
        return htmlspecialchars($var, ENT_COMPAT, $this->encoding);
84
    }
85
86
}
87