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

voku / simple_html_dom / 8870003987

28 Apr 2024 07:52PM UTC coverage: 66.051% (-0.04%) from 66.091%
8870003987

Pull #110

github

web-flow
Merge 0c27c54be into e2ced0848
Pull Request #110: fix: Cannot assign null to property DOMNode::* of type string

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

1109 of 1679 relevant lines covered (66.05%)

25.7 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 \DOMElement|\DOMNode|null
28
     */
29
    protected $node;
30

31
    /**
32
     * @var SimpleHtmlAttributes|null
33
     */
34
    private $classListCache;
35

36
    /**
37
     * @param string $name
38
     * @param array  $arguments
39
     *
40
     * @throws \BadMethodCallException
41
     *
42
     * @return SimpleHtmlDomInterface|string|null
43
     */
44
    public function __call($name, $arguments)
45
    {
46
        $name = \strtolower($name);
×
47

48
        if (isset(self::$functionAliases[$name])) {
×
49
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
×
50
        }
51

52
        throw new \BadMethodCallException('Method does not exist');
×
53
    }
54

55
    /**
56
     * @param string $name
57
     *
58
     * @return SimpleHtmlAttributes|string|string[]|null
59
     */
60
    public function __get($name)
61
    {
62
        $nameOrig = $name;
90✔
63
        $name = \strtolower($name);
90✔
64

65
        switch ($name) {
66
            case 'outerhtml':
90✔
67
            case 'outertext':
82✔
68
            case 'html':
70✔
69
                return $this->html();
30✔
70
            case 'innerhtml':
70✔
71
            case 'innertext':
58✔
72
                return $this->innerHtml();
21✔
73
            case 'innerhtmlkeep':
52✔
74
                return $this->innerHtml(false, false);
1✔
75
            case 'text':
52✔
76
            case 'plaintext':
46✔
77
                return $this->text();
26✔
78
            case 'tag':
30✔
79
                return $this->node->nodeName ?? '';
6✔
80
            case 'attr':
27✔
81
                return $this->getAllAttributes();
×
82
            case 'classlist':
27✔
83
                if ($this->classListCache === null) {
12✔
84
                    $this->classListCache = new SimpleHtmlAttributes($this->node ?? null, 'class');
12✔
85
                }
86

87
                return $this->classListCache;
12✔
88
            default:
89
                if ($this->node && \property_exists($this->node, $nameOrig)) {
15✔
90
                    if (\is_string($this->node->{$nameOrig})) {
2✔
91
                        return HtmlDomParser::putReplacedBackToPreserveHtmlEntities($this->node->{$nameOrig});
2✔
92
                    }
93

94
                    return $this->node->{$nameOrig};
×
95
                }
96

97
                return $this->getAttribute($name);
15✔
98
        }
99
    }
100

101
    /**
102
     * @param string $selector
103
     * @param int    $idx
104
     *
105
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
106
     */
107
    public function __invoke($selector, $idx = null)
108
    {
109
        return $this->find($selector, $idx);
12✔
110
    }
111

112
    /**
113
     * @param string $name
114
     *
115
     * @return bool
116
     */
117
    public function __isset($name)
118
    {
119
        $nameOrig = $name;
1✔
120
        $name = \strtolower($name);
1✔
121

122
        switch ($name) {
123
            case 'outertext':
1✔
124
            case 'outerhtml':
1✔
125
            case 'innertext':
1✔
126
            case 'innerhtml':
1✔
127
            case 'innerhtmlkeep':
1✔
128
            case 'plaintext':
1✔
129
            case 'text':
1✔
130
            case 'tag':
1✔
131
                return true;
×
132
            default:
133
                if ($this->node && \property_exists($this->node, $nameOrig)) {
1✔
134
                    return isset($this->node->{$nameOrig});
×
135
                }
136

137
                return $this->hasAttribute($name);
1✔
138
        }
139
    }
140

141
    /**
142
     * @param string $name
143
     * @param mixed  $value
144
     *
145
     * @return SimpleHtmlDomInterface|null
146
     */
147
    public function __set($name, $value)
148
    {
149
        $nameOrig = $name;
28✔
150
        $name = \strtolower($name);
28✔
151

152
        switch ($name) {
153
            case 'outerhtml':
28✔
154
            case 'outertext':
24✔
155
                return $this->replaceNodeWithString($value);
9✔
156
            case 'innertext':
19✔
157
            case 'innerhtml':
17✔
158
                return $this->replaceChildWithString($value);
8✔
159
            case 'innerhtmlkeep':
15✔
160
                return $this->replaceChildWithString($value, false);
1✔
161
            case 'plaintext':
14✔
162
                return $this->replaceTextWithString($value);
1✔
163
            case 'classlist':
13✔
164
                $name = 'class';
1✔
165
                $nameOrig = 'class';
1✔
166
                // no break
167
            default:
168
                if ($this->node && \property_exists($this->node, $nameOrig)) {
13✔
169
                    // INFO: Cannot assign null to property DOMNode::* of type string
170
                    if ($nameOrig === 'prefix' || $nameOrig === 'textContent') {
×
171
                        $value = (string)$value;
×
172
                    }
173

NEW
174
                    if (!is_null($value)) {
×
NEW
175
                        return $this->node->{$nameOrig} = $value;
×
176
                    }
177
                }
178

179
                return $this->setAttribute($name, $value);
13✔
180
        }
181
    }
182

183
    /**
184
     * @return string
185
     */
186
    public function __toString()
187
    {
188
        return $this->html();
3✔
189
    }
190

191
    /**
192
     * @param string $name
193
     *
194
     * @return void
195
     */
196
    public function __unset($name)
197
    {
198
        /** @noinspection UnusedFunctionResultInspection */
199
        $this->removeAttribute($name);
×
200
    }
201

202
    /**
203
     * @param string $selector
204
     * @param int|null   $idx
205
     *
206
     * @return mixed
207
     */
208
    abstract public function find(string $selector, $idx = null);
209

210
    /**
211
     * @return string[]|null
212
     */
213
    abstract public function getAllAttributes();
214

215
    abstract public function getAttribute(string $name): string;
216

217
    abstract public function hasAttribute(string $name): bool;
218

219
    abstract public function html(bool $multiDecodeNewHtmlEntity = false): string;
220

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

223
    abstract public function removeAttribute(string $name): SimpleHtmlDomInterface;
224

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

227
    abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface;
228

229
    /**
230
     * @param string $string
231
     *
232
     * @return SimpleHtmlDomInterface
233
     */
234
    abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface;
235

236
    /**
237
     * @param string      $name
238
     * @param string|null $value
239
     * @param bool        $strictEmptyValueCheck
240
     *
241
     * @return SimpleHtmlDomInterface
242
     */
243
    abstract public function setAttribute(string $name, $value = null, bool $strictEmptyValueCheck = false): SimpleHtmlDomInterface;
244

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