• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / simple_html_dom / 24702136431

21 Apr 2026 03:15AM UTC coverage: 96.034% (-0.07%) from 96.106%
24702136431

push

github

voku
[+]: work on phpstan reported issues :)

... before a new release goes live.

208 of 218 new or added lines in 12 files covered. (95.41%)

6 existing lines in 2 files now uncovered.

2155 of 2244 relevant lines covered (96.03%)

286.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.45
/src/voku/helper/AbstractSimpleXmlDom.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace voku\helper;
6

7
abstract class AbstractSimpleXmlDom
8
{
9
    /**
10
     * @var array<string, string>
11
     */
12
    protected static $functionAliases = [
13
        'children'     => 'childNodes',
14
        'first_child'  => 'firstChild',
15
        'last_child'   => 'lastChild',
16
        'next_sibling' => 'nextSibling',
17
        'prev_sibling' => 'previousSibling',
18
        'parent'       => 'parentNode',
19
    ];
20

21
    /**
22
     * @var \DOMElement|\DOMNode|null
23
     */
24
    protected $node;
25

26
    /**
27
     * @param string       $name
28
     * @param array<mixed> $arguments
29
     *
30
     * @throws \BadMethodCallException
31
     *
32
     * @return SimpleXmlDomInterface|string|null
33
     */
34
    public function __call($name, $arguments)
35
    {
36
        $name = \strtolower($name);
×
37

38
        if (isset(self::$functionAliases[$name])) {
×
NEW
39
            $method = self::$functionAliases[$name];
×
40

NEW
41
            return $this->{$method}(...$arguments);
×
42
        }
43

44
        throw new \BadMethodCallException('Method does not exist');
×
45
    }
46

47
    /**
48
     * @param string $name
49
     *
50
     * @return array<int, string>|string|null
51
     */
52
    public function __get($name)
53
    {
54
        $nameOrig = $name;
84✔
55
        $name = \strtolower($name);
84✔
56

57
        switch ($name) {
58
            case 'xml':
84✔
59
                return $this->xml();
7✔
60
            case 'plaintext':
84✔
61
                return $this->text();
21✔
62
            case 'tag':
77✔
63
                return $this->node->nodeName ?? '';
63✔
64
            case 'attr':
49✔
65
                return $this->getAllAttributes();
21✔
66
            default:
67
                if ($this->node && \property_exists($this->node, $nameOrig)) {
35✔
UNCOV
68
                    return $this->node->{$nameOrig};
2✔
69
                }
70

71
                return $this->getAttribute($name);
35✔
72
        }
73
    }
74

75
    /**
76
     * @param string $selector
77
     * @param int|null    $idx
78
     *
79
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
80
     */
81
    public function __invoke($selector, $idx = null)
82
    {
83
        return $this->find($selector, $idx);
14✔
84
    }
85

86
    /**
87
     * @param string $name
88
     *
89
     * @return bool
90
     */
91
    public function __isset($name)
92
    {
93
        $nameOrig = $name;
14✔
94
        $name = \strtolower($name);
14✔
95

96
        switch ($name) {
97
            case 'outertext':
14✔
98
            case 'outerhtml':
14✔
99
            case 'innertext':
14✔
100
            case 'innerhtml':
14✔
101
            case 'innerhtmlkeep':
14✔
102
            case 'plaintext':
14✔
103
            case 'text':
14✔
104
            case 'tag':
14✔
105
                return true;
14✔
106
            default:
107
                if ($this->node && \property_exists($this->node, $nameOrig)) {
14✔
108
                    return isset($this->node->{$nameOrig});
×
109
                }
110

111
                return $this->hasAttribute($name);
14✔
112
        }
113
    }
114

115
    /**
116
     * @param string $name
117
     * @param mixed  $value
118
     *
119
     * @return void
120
     */
121
    public function __set($name, $value): void
122
    {
123
        $nameOrig = $name;
35✔
124
        $name = \strtolower($name);
35✔
125

126
        switch ($name) {
127
            case 'outerhtml':
35✔
128
            case 'outertext':
35✔
129
                $this->replaceNodeWithString($value);
35✔
130
                return;
35✔
131
            case 'innertext':
35✔
132
            case 'innerhtml':
35✔
133
                $this->replaceChildWithString($value);
35✔
134
                return;
35✔
135
            case 'innerhtmlkeep':
35✔
136
                $this->replaceChildWithString($value, false);
7✔
137
                return;
7✔
138
            case 'plaintext':
35✔
139
                $this->replaceTextWithString($value);
35✔
140
                return;
35✔
141
            default:
142
                if ($this->node && \property_exists($this->node, $nameOrig)) {
7✔
NEW
143
                    $this->node->{$nameOrig} = $value;
×
NEW
144
                    return;
×
145
                }
146

147
                $this->setAttribute($name, $value);
7✔
148
        }
149
    }
150

151
    /**
152
     * @return string
153
     */
154
    public function __toString()
155
    {
156
        return $this->xml();
14✔
157
    }
158

159
    /**
160
     * @param string $name
161
     *
162
     * @return void
163
     */
164
    public function __unset($name)
165
    {
166
        /** @noinspection UnusedFunctionResultInspection */
167
        $this->removeAttribute($name);
14✔
168
    }
169

170
    /**
171
     * @param string $selector
172
     * @param int|null   $idx
173
     *
174
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
175
     */
176
    abstract public function find(string $selector, $idx = null);
177

178
    /**
179
     * @return string[]|null
180
     */
181
    abstract public function getAllAttributes();
182

183
    /**
184
     * @param string $name
185
     *
186
     * @return string
187
     */
188
    abstract public function getAttribute(string $name): string;
189

190
    /**
191
     * @param string $name
192
     *
193
     * @return bool
194
     */
195
    abstract public function hasAttribute(string $name): bool;
196

197
    abstract public function innerXml(bool $multiDecodeNewHtmlEntity = false): string;
198

199
    abstract public function removeAttribute(string $name): SimpleXmlDomInterface;
200

201
    abstract protected function replaceChildWithString(string $string, bool $putBrokenReplacedBack = true): SimpleXmlDomInterface;
202

203
    abstract protected function replaceNodeWithString(string $string): SimpleXmlDomInterface;
204

205
    /**
206
     * @param string $string
207
     *
208
     * @return SimpleXmlDomInterface
209
     */
210
    abstract protected function replaceTextWithString($string): SimpleXmlDomInterface;
211

212
    /**
213
     * @param string $name
214
     * @param string|null   $value
215
     * @param bool   $strictEmptyValueCheck
216
     *
217
     * @return SimpleXmlDomInterface
218
     */
219
    abstract public function setAttribute(string $name, $value = null, bool $strictEmptyValueCheck = false): SimpleXmlDomInterface;
220

221
    abstract public function text(): string;
222

223
    abstract public function xml(bool $multiDecodeNewHtmlEntity = false): string;
224
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc