HtmlElement   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 0
dl 0
loc 132
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getTagName() 0 4 1
A getAllAttributes() 0 4 1
A getAttribute() 0 8 2
A setAttribute() 0 6 1
A getContents() 0 12 4
A setContents() 0 6 2
A __toString() 0 18 4
1
<?php
2
3
namespace League\CommonMark;
4
5
class HtmlElement
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $tagName;
11
12
    /**
13
     * @var string[]
14
     */
15
    protected $attributes = [];
16
17
    /**
18
     * @var HtmlElement|HtmlElement[]|string
19
     */
20
    protected $contents;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $selfClosing = false;
26
27
    /**
28
     * @param string                           $tagName
29
     * @param string[]                         $attributes
30
     * @param HtmlElement|HtmlElement[]|string $contents
31
     * @param bool                             $selfClosing
32
     */
33 1983
    public function __construct($tagName, $attributes = [], $contents = '', $selfClosing = false)
34
    {
35 1983
        $this->tagName = $tagName;
36 1983
        $this->attributes = $attributes;
37 1983
        $this->selfClosing = $selfClosing;
38
39 1983
        $this->setContents($contents);
40 1983
    }
41
42
    /**
43
     * @return string
44
     */
45 93
    public function getTagName()
46
    {
47 93
        return $this->tagName;
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53 57
    public function getAllAttributes()
54
    {
55 57
        return $this->attributes;
56
    }
57
58
    /**
59
     * @param string $key
60
     *
61
     * @return string|null
62
     */
63 69
    public function getAttribute($key)
64
    {
65 69
        if (!isset($this->attributes[$key])) {
66 24
            return;
67
        }
68
69 60
        return $this->attributes[$key];
70
    }
71
72
    /**
73
     * @param string $key
74
     * @param string $value
75
     *
76
     * @return $this
77
     */
78 6
    public function setAttribute($key, $value)
79
    {
80 6
        $this->attributes[$key] = $value;
81
82 6
        return $this;
83
    }
84
85
    /**
86
     * @param bool $asString
87
     *
88
     * @return HtmlElement|HtmlElement[]|string
89
     */
90 1908
    public function getContents($asString = true)
91
    {
92 1908
        if (!$asString || is_string($this->contents)) {
93 1896
            return $this->contents;
94
        }
95
96 246
        if (is_array($this->contents)) {
97 3
            return implode('', $this->contents);
98
        }
99
100 243
        return (string) $this->contents;
101
    }
102
103
    /**
104
     * @param HtmlElement|HtmlElement[]|string $contents
105
     *
106
     * @return $this
107
     */
108 1983
    public function setContents($contents)
109
    {
110 1983
        $this->contents = $contents !== null ? $contents : '';
111
112 1983
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 1863
    public function __toString()
119
    {
120 1863
        $result = '<' . $this->tagName;
121
122 1863
        foreach ($this->attributes as $key => $value) {
123 426
            $result .= ' ' . $key . '="' . $value . '"';
124 621
        }
125
126 1863
        if ($this->contents !== '') {
127 1824
            $result .= '>' . $this->getContents() . '</' . $this->tagName . '>';
128 779
        } elseif ($this->selfClosing) {
129 198
            $result .= ' />';
130 66
        } else {
131 42
            $result .= '></' . $this->tagName . '>';
132
        }
133
134 1863
        return $result;
135
    }
136
}
137