Skip to content

Commit def94db

Browse files
committed
Add PHP 8.4 support. Remove PHP 7.* support.
1 parent 0df5316 commit def94db

File tree

7 files changed

+28
-28
lines changed

7 files changed

+28
-28
lines changed

autoload.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'IvoPetkov\HTML5DOMTokenList' => __DIR__ . '/src/HTML5DOMTokenList.php'
1616
);
1717

18-
spl_autoload_register(function ($class) use ($classes) {
18+
spl_autoload_register(function ($class) use ($classes): void {
1919
if (isset($classes[$class])) {
2020
require $classes[$class];
2121
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*",
13+
"php": "8.0.*|8.1.*|8.2.*|8.3.*|8.4.*",
1414
"ext-dom": "*"
1515
},
1616
"require-dev": {

src/HTML5DOMDocument.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function loadHTML($source, $options = 0)
200200
preg_match_all('/\sid[\s]*=[\s]*(["\'])(.*?)\1/', $source, $matches);
201201
if (!empty($matches[2]) && max(array_count_values($matches[2])) > 1) {
202202
$elementIDs = [];
203-
$walkChildren = function ($element) use (&$walkChildren, &$elementIDs) {
203+
$walkChildren = function ($element) use (&$walkChildren, &$elementIDs): void {
204204
foreach ($element->childNodes as $child) {
205205
if ($child instanceof \DOMElement) {
206206
if ($child->attributes->length > 0) { // Performance optimization
@@ -301,7 +301,7 @@ private function addBodyElementIfMissing(): bool
301301
* @param \DOMNode $node Optional parameter to output a subset of the document.
302302
* @return string The document (or node) HTML code as string.
303303
*/
304-
public function saveHTML(\DOMNode $node = null): string
304+
public function saveHTML(?\DOMNode $node = null): string
305305
{
306306
$nodeMode = $node !== null;
307307
if ($nodeMode && $node instanceof \DOMDocument) {
@@ -489,7 +489,7 @@ public function insertHTMLMulti(array $sources)
489489

490490
$currentDomDocument = &$this;
491491

492-
$copyAttributes = function ($sourceNode, $targetNode) {
492+
$copyAttributes = function ($sourceNode, $targetNode): void {
493493
foreach ($sourceNode->attributes as $attributeName => $attribute) {
494494
$targetNode->setAttribute($attributeName, $attribute->value);
495495
}
@@ -500,7 +500,7 @@ public function insertHTMLMulti(array $sources)
500500
$currentDomBodyElement = null;
501501

502502
$insertTargetsList = null;
503-
$prepareInsertTargetsList = function () use (&$insertTargetsList) {
503+
$prepareInsertTargetsList = function () use (&$insertTargetsList): void {
504504
if ($insertTargetsList === null) {
505505
$insertTargetsList = [];
506506
$targetElements = $this->getElementsByTagName('html5-dom-document-insert-target');

src/HTML5DOMDocument/Internal/QuerySelectors.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
4444
} else {
4545
$getChildren = function () use ($context) {
4646
$result = [];
47-
$process = function (\DOMNode $node) use (&$process, &$result) {
47+
$process = function (\DOMNode $node) use (&$process, &$result): void {
4848
foreach ($node->childNodes as $child) {
4949
if ($child instanceof \DOMElement) {
5050
$result[] = $child;
@@ -94,7 +94,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
9494
$simpleSelectors = [];
9595

9696
// all
97-
$simpleSelectors['\*'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($walkChildren) {
97+
$simpleSelectors['\*'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($walkChildren) {
9898
if ($mode === 'validate') {
9999
return true;
100100
} else {
@@ -107,7 +107,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
107107
};
108108

109109
// tagname
110-
$simpleSelectors['[a-zA-Z0-9\-]+'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($walkChildren) {
110+
$simpleSelectors['[a-zA-Z0-9\-]+'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($walkChildren) {
111111
$tagNames = [];
112112
foreach ($matches as $match) {
113113
$tagNames[] = strtolower($match[0]);
@@ -123,7 +123,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
123123
};
124124

125125
// tagname[target] or [target] // Available values for targets: attr, attr="value", attr~="value", attr|="value", attr^="value", attr$="value", attr*="value"
126-
$simpleSelectors['(?:[a-zA-Z0-9\-]*)(?:\[.+?\])'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($walkChildren) {
126+
$simpleSelectors['(?:[a-zA-Z0-9\-]*)(?:\[.+?\])'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($walkChildren) {
127127
$run = function ($match) use ($mode, $context, $add, $walkChildren) {
128128
$attributeSelectors = explode('][', substr($match[2], 1, -1));
129129
foreach ($attributeSelectors as $i => $attributeSelector) {
@@ -229,7 +229,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
229229
};
230230

231231
// tagname#id or #id
232-
$simpleSelectors['(?:[a-zA-Z0-9\-]*)#(?:[a-zA-Z0-9\-\_]+?)'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($getElementById) {
232+
$simpleSelectors['(?:[a-zA-Z0-9\-]*)#(?:[a-zA-Z0-9\-\_]+?)'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($getElementById) {
233233
$run = function ($match) use ($mode, $context, $add, $getElementById) {
234234
$tagName = strlen($match[1]) > 0 ? strtolower($match[1]) : null;
235235
$id = $match[2];
@@ -258,7 +258,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
258258
};
259259

260260
// tagname.classname, .classname, tagname.classname.classname2, .classname.classname2
261-
$simpleSelectors['(?:[a-zA-Z0-9\-]*)\.(?:[a-zA-Z0-9\-\_\.]+?)'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($walkChildren) {
261+
$simpleSelectors['(?:[a-zA-Z0-9\-]*)\.(?:[a-zA-Z0-9\-\_\.]+?)'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($walkChildren) {
262262
$rawData = []; // Array containing [tag, classnames]
263263
$tagNames = [];
264264
foreach ($matches as $match) {
@@ -350,7 +350,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
350350
}
351351
if (!$found) {
352352
$result[] = $element;
353-
if ($preferredLimit !== null && sizeof($result) >= $preferredLimit) {
353+
if ($preferredLimit !== null && count($result) >= $preferredLimit) {
354354
return true;
355355
}
356356
}
@@ -359,8 +359,8 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
359359
}
360360

361361
$selectorsToCall = [];
362-
$addSelectorToCall = function ($type, $selector, $argument) use (&$selectorsToCall) {
363-
$previousIndex = sizeof($selectorsToCall) - 1;
362+
$addSelectorToCall = function ($type, $selector, $argument) use (&$selectorsToCall): void {
363+
$previousIndex = count($selectorsToCall) - 1;
364364
// todo optimize complex too
365365
if ($type === 1 && isset($selectorsToCall[$previousIndex]) && $selectorsToCall[$previousIndex][0] === $type && $selectorsToCall[$previousIndex][1] === $selector) {
366366
$selectorsToCall[$previousIndex][2][] = $argument;
@@ -421,7 +421,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
421421
};
422422

423423
// div p (space between) - all <p> elements inside <div> elements
424-
$complexSelectors[' '] = function (array $parts, \DOMNode $context, callable $add = null) use (&$getMatchingElements) {
424+
$complexSelectors[' '] = function (array $parts, \DOMNode $context, ?callable $add = null) use (&$getMatchingElements): void {
425425
$elements = null;
426426
foreach ($parts as $part) {
427427
if ($elements === null) {
@@ -440,7 +440,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
440440
};
441441

442442
// div > p - all <p> elements where the parent is a <div> element
443-
$complexSelectors['>'] = function (array $parts, \DOMNode $context, callable $add = null) use (&$getMatchingElements, &$isMatchingElement) {
443+
$complexSelectors['>'] = function (array $parts, \DOMNode $context, ?callable $add = null) use (&$getMatchingElements, &$isMatchingElement): void {
444444
$elements = null;
445445
foreach ($parts as $part) {
446446
if ($elements === null) {
@@ -463,7 +463,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
463463
};
464464

465465
// div + p - all <p> elements that are placed immediately after <div> elements
466-
$complexSelectors['+'] = function (array $parts, \DOMNode $context, callable $add = null) use (&$getMatchingElements, &$isMatchingElement) {
466+
$complexSelectors['+'] = function (array $parts, \DOMNode $context, ?callable $add = null) use (&$getMatchingElements, &$isMatchingElement): void {
467467
$elements = null;
468468
foreach ($parts as $part) {
469469
if ($elements === null) {
@@ -484,7 +484,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
484484
};
485485

486486
// p ~ ul - all <ul> elements that are preceded by a <p> element
487-
$complexSelectors['~'] = function (array $parts, \DOMNode $context, callable $add = null) use (&$getMatchingElements, &$isMatchingElement) {
487+
$complexSelectors['~'] = function (array $parts, \DOMNode $context, ?callable $add = null) use (&$getMatchingElements, &$isMatchingElement): void {
488488
$elements = null;
489489
foreach ($parts as $part) {
490490
if ($elements === null) {

src/HTML5DOMNodeList.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function item(int $index)
3838
public function __get(string $name)
3939
{
4040
if ($name === 'length') {
41-
return sizeof($this);
41+
return count($this);
4242
}
4343
throw new \Exception('Undefined property: \IvoPetkov\HTML5DOMNodeList::$' . $name);
4444
}

src/HTML5DOMTokenList.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function item(int $index)
121121
* @param bool $force A Boolean that, if included, turns the toggle into a one way-only operation. If set to false, the token will only be removed but not added again. If set to true, the token will only be added but not removed again.
122122
* @return bool false if the token is not in the list after the call, or true if the token is in the list after the call.
123123
*/
124-
public function toggle(string $token, bool $force = null): bool
124+
public function toggle(string $token, ?bool $force = null): bool
125125
{
126126
$this->tokenize();
127127
$isThereAfter = false;

tests/Test.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Test extends PHPUnit\Framework\TestCase
2222
public function testSaveHTML()
2323
{
2424

25-
$testSource = function ($source, $expectedSource) {
25+
$testSource = function ($source, $expectedSource): void {
2626
$dom = new HTML5DOMDocument();
2727
$dom->loadHTML($source);
2828
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
@@ -50,7 +50,7 @@ public function testSaveHTML()
5050
*/
5151
public function testOmitedElements()
5252
{
53-
$testSource = function ($source, $expectedSource) {
53+
$testSource = function ($source, $expectedSource): void {
5454
$dom = new HTML5DOMDocument();
5555
$dom->loadHTML($source);
5656
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
@@ -312,7 +312,7 @@ public function testInsertHTML()
312312
public function testEmpty()
313313
{
314314

315-
$testSource = function ($source, $expectedSource) {
315+
$testSource = function ($source, $expectedSource): void {
316316
$dom = new HTML5DOMDocument();
317317
$dom->loadHTML($source);
318318
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
@@ -631,7 +631,7 @@ public function testGetAttributes()
631631
$this->assertTrue($dom->querySelector('div')->getAttribute('unknown') === '');
632632
$this->assertTrue($dom->querySelector('div')->getAttribute('data-value') === $expectedDataAttributeValue);
633633
$attributes = $dom->querySelector('div')->getAttributes();
634-
$this->assertTrue(sizeof($attributes) === 2);
634+
$this->assertTrue(count($attributes) === 2);
635635
$this->assertTrue($attributes['class'] === 'text1');
636636
}
637637

@@ -1295,12 +1295,12 @@ public function testLIBXML_HTML_NOIMPLIED()
12951295
public function testCompatibilityWithDOMDocument()
12961296
{
12971297

1298-
$compareDOMs = function (HTML5DOMDocument $dom1, DOMDocument $dom2) {
1298+
$compareDOMs = function (HTML5DOMDocument $dom1, DOMDocument $dom2): void {
12991299
$this->assertEquals($dom1->getElementsByTagName('html')->length, $dom2->getElementsByTagName('html')->length);
13001300
$this->assertEquals($dom1->getElementsByTagName('head')->length, $dom2->getElementsByTagName('head')->length);
13011301
$this->assertEquals($dom1->getElementsByTagName('body')->length, $dom2->getElementsByTagName('body')->length);
13021302

1303-
$updateNewLines = function (&$content) {
1303+
$updateNewLines = function (&$content): void {
13041304
$content = str_replace("\n<head>", '<head>', $content);
13051305
$content = str_replace("\n<body>", '<body>', $content);
13061306
$content = str_replace("\n</html>", '</html>', $content);
@@ -1335,7 +1335,7 @@ public function testCompatibilityWithDOMDocument()
13351335
}
13361336
};
13371337

1338-
$compareContent = function ($content) use ($compareDOMs) {
1338+
$compareContent = function ($content) use ($compareDOMs): void {
13391339
$dom = new HTML5DOMDocument();
13401340
$dom->loadHTML($content);
13411341
$dom2 = new DOMDocument();

0 commit comments

Comments
 (0)