XMLStreamObject::toString()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
nc 6
nop 1
1
<?php
2
namespace PHPDaemon\XMLStream;
3
4
class XMLStreamObject
5
{
6
    use \PHPDaemon\Traits\ClassWatchdog;
7
    use \PHPDaemon\Traits\StaticObjectWatchdog;
8
9
    /**
10
     * Tag name
11
     *
12
     * @var string
13
     */
14
    public $name;
15
16
    /**
17
     * Namespace
18
     *
19
     * @var string
20
     */
21
    public $ns;
22
23
    /**
24
     * Attributes
25
     *
26
     * @var array
27
     */
28
    public $attrs = [];
29
30
    /**
31
     * Subs?
32
     *
33
     * @var array
34
     */
35
    public $subs = [];
36
37
    /**
38
     * Node data
39
     *
40
     * @var string
41
     */
42
    public $data = '';
43
44
    /**
45
     * Constructor
46
     *
47
     * @param string $name
48
     * @param string $ns
49
     * @param array $attrs
50
     * @param string $data
51
     */
52
    public function __construct($name, $ns = '', $attrs = [], $data = '')
53
    {
54
        $this->name = strtolower($name);
55
        $this->ns = $ns;
56
        if (is_array($attrs) && count($attrs)) {
57
            foreach ($attrs as $key => $value) {
58
                $this->attrs[strtolower($key)] = $value;
59
            }
60
        }
61
        $this->data = $data;
62
    }
63
64
    /**
65
     * Dump this XML Object to output.
66
     *
67
     * @param integer $depth
68
     */
69
    public function printObj($depth = 0)
70
    {
71
        $s = str_repeat("\t", $depth) . $this->name . " " . $this->ns . ' ' . $this->data . "\n";
72
        foreach ($this->subs as $sub) {
73
            $s .= $sub->printObj($depth + 1);
74
        }
75
        return $s;
76
    }
77
78
    /**
79
     * Return this XML Object in xml notation
80
     *
81
     * @param string $str
82
     */
83
    public function toString($str = '')
84
    {
85
        $str .= "<{$this->name} xmlns='{$this->ns}' ";
86
        foreach ($this->attrs as $key => $value) {
87
            if ($key !== 'xmlns') {
88
                $value = htmlspecialchars($value);
89
                $str .= "$key='$value' ";
90
            }
91
        }
92
        $str .= ">";
93
        foreach ($this->subs as $sub) {
94
            $str .= $sub->toString();
95
        }
96
        $body = htmlspecialchars($this->data);
97
        $str .= "$body</{$this->name}>";
98
        return $str;
99
    }
100
101
    /**
102
     * Has this XML Object the given sub?
103
     *
104
     * @param string $name
105
     * @return boolean
106
     */
107 View Code Duplication
    public function hasSub($name, $ns = null)
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...
108
    {
109
        foreach ($this->subs as $sub) {
110
            if (($name === "*" or $sub->name === $name) and ($ns === null or $sub->ns === $ns)) {
111
                return true;
112
            }
113
        }
114
        return false;
115
    }
116
117
    /**
118
     * Return a sub
119
     *
120
     * @param string $name
121
     * @param string $attrs
0 ignored issues
show
Documentation introduced by
Should the type for parameter $attrs not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
122
     * @param string $ns
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ns not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
123
     */
124 View Code Duplication
    public function sub($name, $attrs = null, $ns = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
The parameter $attrs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
125
    {
126
        //@TODO: attrs is ignored
127
        foreach ($this->subs as $sub) {
128
            if ($sub->name === $name and ($ns === null or $sub->ns === $ns)) {
129
                return $sub;
130
            }
131
        }
132
        return null;
133
    }
134
}
135