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

voku / simple_html_dom / 9233676186

25 May 2024 06:56AM UTC coverage: 66.032% (-0.04%) from 66.071%
9233676186

push

github

web-flow
Merge pull request #110 from frugan-dev/master

fix: Cannot assign null to property DOMNode::* of type string

0 of 2 new or added lines in 1 file covered. (0.0%)

1110 of 1681 relevant lines covered (66.03%)

102.76 hits per line

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

81.16
/src/voku/helper/AbstractSimpleHtmlDom.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace voku\helper;
6

7
abstract class AbstractSimpleHtmlDom
8
{
9
    /**
10
     * @var array
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
        'outertext'    => 'html',
20
        'outerhtml'    => 'html',
21
        'innertext'    => 'innerHtml',
22
        'innerhtml'    => 'innerHtml',
23
        'innerhtmlkeep'    => 'innerHtmlKeep',
24
    ];
25

26
    /**
27
     * @var string[]
28
     */
29
    protected static $stringDomNodes = [
30
        'id',
31
        'prefix',
32
        'content'
33
    ];
34

35
    /**
36
     * @var \DOMElement|\DOMNode|null
37
     */
38
    protected $node;
39

40
    /**
41
     * @var SimpleHtmlAttributes|null
42
     */
43
    private $classListCache;
44

45
    /**
46
     * @param string $name
47
     * @param array  $arguments
48
     *
49
     * @throws \BadMethodCallException
50
     *
51
     * @return SimpleHtmlDomInterface|string|null
52
     */
53
    public function __call($name, $arguments)
54
    {
55
        $name = \strtolower($name);
×
56

57
        if (isset(self::$functionAliases[$name])) {
×
58
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
×
59
        }
60

61
        throw new \BadMethodCallException('Method does not exist');
×
62
    }
63

64
    /**
65
     * @param string $name
66
     *
67
     * @return SimpleHtmlAttributes|string|string[]|null
68
     */
69
    public function __get($name)
70
    {
71
        $nameOrig = $name;
360✔
72
        $name = \strtolower($name);
360✔
73

74
        switch ($name) {
75
            case 'outerhtml':
360✔
76
            case 'outertext':
328✔
77
            case 'html':
280✔
78
                return $this->html();
120✔
79
            case 'innerhtml':
280✔
80
            case 'innertext':
232✔
81
                return $this->innerHtml();
84✔
82
            case 'innerhtmlkeep':
208✔
83
                return $this->innerHtml(false, false);
4✔
84
            case 'text':
208✔
85
            case 'plaintext':
184✔
86
                return $this->text();
104✔
87
            case 'tag':
120✔
88
                return $this->node->nodeName ?? '';
24✔
89
            case 'attr':
108✔
90
                return $this->getAllAttributes();
×
91
            case 'classlist':
108✔
92
                if ($this->classListCache === null) {
48✔
93
                    $this->classListCache = new SimpleHtmlAttributes($this->node ?? null, 'class');
48✔
94
                }
95

96
                return $this->classListCache;
48✔
97
            default:
98
                if ($this->node && \property_exists($this->node, $nameOrig)) {
60✔
99
                    if (\is_string($this->node->{$nameOrig})) {
8✔
100
                        return HtmlDomParser::putReplacedBackToPreserveHtmlEntities($this->node->{$nameOrig});
8✔
101
                    }
102

103
                    return $this->node->{$nameOrig};
×
104
                }
105

106
                return $this->getAttribute($name);
60✔
107
        }
108
    }
109

110
    /**
111
     * @param string $selector
112
     * @param int    $idx
113
     *
114
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
115
     */
116
    public function __invoke($selector, $idx = null)
117
    {
118
        return $this->find($selector, $idx);
48✔
119
    }
120

121
    /**
122
     * @param string $name
123
     *
124
     * @return bool
125
     */
126
    public function __isset($name)
127
    {
128
        $nameOrig = $name;
4✔
129
        $name = \strtolower($name);
4✔
130

131
        switch ($name) {
132
            case 'outertext':
4✔
133
            case 'outerhtml':
4✔
134
            case 'innertext':
4✔
135
            case 'innerhtml':
4✔
136
            case 'innerhtmlkeep':
4✔
137
            case 'plaintext':
4✔
138
            case 'text':
4✔
139
            case 'tag':
4✔
140
                return true;
×
141
            default:
142
                if ($this->node && \property_exists($this->node, $nameOrig)) {
4✔
143
                    return isset($this->node->{$nameOrig});
×
144
                }
145

146
                return $this->hasAttribute($name);
4✔
147
        }
148
    }
149

150
    /**
151
     * @param string $name
152
     * @param mixed  $value
153
     *
154
     * @return SimpleHtmlDomInterface|null
155
     */
156
    public function __set($name, $value)
157
    {
158
        $nameOrig = $name;
115✔
159
        $name = \strtolower($name);
115✔
160

161
        switch ($name) {
162
            case 'outerhtml':
115✔
163
            case 'outertext':
99✔
164
                return $this->replaceNodeWithString($value);
36✔
165
            case 'innertext':
79✔
166
            case 'innerhtml':
68✔
167
                return $this->replaceChildWithString($value);
35✔
168
            case 'innerhtmlkeep':
60✔
169
                return $this->replaceChildWithString($value, false);
4✔
170
            case 'plaintext':
56✔
171
                return $this->replaceTextWithString($value);
4✔
172
            case 'classlist':
52✔
173
                $name = 'class';
4✔
174
                $nameOrig = 'class';
4✔
175
                // no break
176
            default:
177
                if ($this->node && \property_exists($this->node, $nameOrig)) {
52✔
178
                    // INFO: Cannot assign null to property DOMNode::* of type string
179
                    if (in_array($nameOrig, self::$stringDomNodes)) {
×
180
                        $value = (string)$value;
×
181
                    }
182

NEW
183
                    if (!is_null($value)) {
×
NEW
184
                        return $this->node->{$nameOrig} = $value;
×
185
                    }
186
                }
187

188
                return $this->setAttribute($name, $value);
52✔
189
        }
190
    }
191

192
    /**
193
     * @return string
194
     */
195
    public function __toString()
196
    {
197
        return $this->html();
12✔
198
    }
199

200
    /**
201
     * @param string $name
202
     *
203
     * @return void
204
     */
205
    public function __unset($name)
206
    {
207
        /** @noinspection UnusedFunctionResultInspection */
208
        $this->removeAttribute($name);
×
209
    }
210

211
    /**
212
     * @param string $selector
213
     * @param int|null   $idx
214
     *
215
     * @return mixed
216
     */
217
    abstract public function find(string $selector, $idx = null);
218

219
    /**
220
     * @return string[]|null
221
     */
222
    abstract public function getAllAttributes();
223

224
    abstract public function getAttribute(string $name): string;
225

226
    abstract public function hasAttribute(string $name): bool;
227

228
    abstract public function html(bool $multiDecodeNewHtmlEntity = false): string;
229

230
    abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false, bool $putBrokenReplacedBack = true): string;
231

232
    abstract public function removeAttribute(string $name): SimpleHtmlDomInterface;
233

234
    abstract protected function replaceChildWithString(string $string, bool $putBrokenReplacedBack = true): SimpleHtmlDomInterface;
235

236
    abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface;
237

238
    /**
239
     * @param string $string
240
     *
241
     * @return SimpleHtmlDomInterface
242
     */
243
    abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface;
244

245
    /**
246
     * @param string      $name
247
     * @param string|null $value
248
     * @param bool        $strictEmptyValueCheck
249
     *
250
     * @return SimpleHtmlDomInterface
251
     */
252
    abstract public function setAttribute(string $name, $value = null, bool $strictEmptyValueCheck = false): SimpleHtmlDomInterface;
253

254
    abstract public function text(): string;
255
}
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

© 2025 Coveralls, Inc