Skip to content

Commit

Permalink
Add PHP 8.4 support. Remove PHP 7.* support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Jan 4, 2025
1 parent 0df5316 commit def94db
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'IvoPetkov\HTML5DOMTokenList' => __DIR__ . '/src/HTML5DOMTokenList.php'
);

spl_autoload_register(function ($class) use ($classes) {
spl_autoload_register(function ($class) use ($classes): void {
if (isset($classes[$class])) {
require $classes[$class];
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*",
"php": "8.0.*|8.1.*|8.2.*|8.3.*|8.4.*",
"ext-dom": "*"
},
"require-dev": {
Expand Down
8 changes: 4 additions & 4 deletions src/HTML5DOMDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function loadHTML($source, $options = 0)
preg_match_all('/\sid[\s]*=[\s]*(["\'])(.*?)\1/', $source, $matches);
if (!empty($matches[2]) && max(array_count_values($matches[2])) > 1) {
$elementIDs = [];
$walkChildren = function ($element) use (&$walkChildren, &$elementIDs) {
$walkChildren = function ($element) use (&$walkChildren, &$elementIDs): void {
foreach ($element->childNodes as $child) {
if ($child instanceof \DOMElement) {
if ($child->attributes->length > 0) { // Performance optimization
Expand Down Expand Up @@ -301,7 +301,7 @@ private function addBodyElementIfMissing(): bool
* @param \DOMNode $node Optional parameter to output a subset of the document.
* @return string The document (or node) HTML code as string.
*/
public function saveHTML(\DOMNode $node = null): string
public function saveHTML(?\DOMNode $node = null): string
{
$nodeMode = $node !== null;
if ($nodeMode && $node instanceof \DOMDocument) {
Expand Down Expand Up @@ -489,7 +489,7 @@ public function insertHTMLMulti(array $sources)

$currentDomDocument = &$this;

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

$insertTargetsList = null;
$prepareInsertTargetsList = function () use (&$insertTargetsList) {
$prepareInsertTargetsList = function () use (&$insertTargetsList): void {
if ($insertTargetsList === null) {
$insertTargetsList = [];
$targetElements = $this->getElementsByTagName('html5-dom-document-insert-target');
Expand Down
26 changes: 13 additions & 13 deletions src/HTML5DOMDocument/Internal/QuerySelectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
} else {
$getChildren = function () use ($context) {
$result = [];
$process = function (\DOMNode $node) use (&$process, &$result) {
$process = function (\DOMNode $node) use (&$process, &$result): void {
foreach ($node->childNodes as $child) {
if ($child instanceof \DOMElement) {
$result[] = $child;
Expand Down Expand Up @@ -94,7 +94,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
$simpleSelectors = [];

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

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

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

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

// tagname.classname, .classname, tagname.classname.classname2, .classname.classname2
$simpleSelectors['(?:[a-zA-Z0-9\-]*)\.(?:[a-zA-Z0-9\-\_\.]+?)'] = function (string $mode, array $matches, \DOMNode $context, callable $add = null) use ($walkChildren) {
$simpleSelectors['(?:[a-zA-Z0-9\-]*)\.(?:[a-zA-Z0-9\-\_\.]+?)'] = function (string $mode, array $matches, \DOMNode $context, ?callable $add = null) use ($walkChildren) {
$rawData = []; // Array containing [tag, classnames]
$tagNames = [];
foreach ($matches as $match) {
Expand Down Expand Up @@ -350,7 +350,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
}
if (!$found) {
$result[] = $element;
if ($preferredLimit !== null && sizeof($result) >= $preferredLimit) {
if ($preferredLimit !== null && count($result) >= $preferredLimit) {
return true;
}
}
Expand All @@ -359,8 +359,8 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
}

$selectorsToCall = [];
$addSelectorToCall = function ($type, $selector, $argument) use (&$selectorsToCall) {
$previousIndex = sizeof($selectorsToCall) - 1;
$addSelectorToCall = function ($type, $selector, $argument) use (&$selectorsToCall): void {
$previousIndex = count($selectorsToCall) - 1;
// todo optimize complex too
if ($type === 1 && isset($selectorsToCall[$previousIndex]) && $selectorsToCall[$previousIndex][0] === $type && $selectorsToCall[$previousIndex][1] === $selector) {
$selectorsToCall[$previousIndex][2][] = $argument;
Expand Down Expand Up @@ -421,7 +421,7 @@ private function internalQuerySelectorAll(string $selector, $preferredLimit = nu
};

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

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

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

// p ~ ul - all <ul> elements that are preceded by a <p> element
$complexSelectors['~'] = function (array $parts, \DOMNode $context, callable $add = null) use (&$getMatchingElements, &$isMatchingElement) {
$complexSelectors['~'] = function (array $parts, \DOMNode $context, ?callable $add = null) use (&$getMatchingElements, &$isMatchingElement): void {
$elements = null;
foreach ($parts as $part) {
if ($elements === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/HTML5DOMNodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function item(int $index)
public function __get(string $name)
{
if ($name === 'length') {
return sizeof($this);
return count($this);
}
throw new \Exception('Undefined property: \IvoPetkov\HTML5DOMNodeList::$' . $name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/HTML5DOMTokenList.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function item(int $index)
* @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.
* @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.
*/
public function toggle(string $token, bool $force = null): bool
public function toggle(string $token, ?bool $force = null): bool
{
$this->tokenize();
$isThereAfter = false;
Expand Down
14 changes: 7 additions & 7 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Test extends PHPUnit\Framework\TestCase
public function testSaveHTML()
{

$testSource = function ($source, $expectedSource) {
$testSource = function ($source, $expectedSource): void {
$dom = new HTML5DOMDocument();
$dom->loadHTML($source);
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testSaveHTML()
*/
public function testOmitedElements()
{
$testSource = function ($source, $expectedSource) {
$testSource = function ($source, $expectedSource): void {
$dom = new HTML5DOMDocument();
$dom->loadHTML($source);
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
Expand Down Expand Up @@ -312,7 +312,7 @@ public function testInsertHTML()
public function testEmpty()
{

$testSource = function ($source, $expectedSource) {
$testSource = function ($source, $expectedSource): void {
$dom = new HTML5DOMDocument();
$dom->loadHTML($source);
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
Expand Down Expand Up @@ -631,7 +631,7 @@ public function testGetAttributes()
$this->assertTrue($dom->querySelector('div')->getAttribute('unknown') === '');
$this->assertTrue($dom->querySelector('div')->getAttribute('data-value') === $expectedDataAttributeValue);
$attributes = $dom->querySelector('div')->getAttributes();
$this->assertTrue(sizeof($attributes) === 2);
$this->assertTrue(count($attributes) === 2);
$this->assertTrue($attributes['class'] === 'text1');
}

Expand Down Expand Up @@ -1295,12 +1295,12 @@ public function testLIBXML_HTML_NOIMPLIED()
public function testCompatibilityWithDOMDocument()
{

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

$updateNewLines = function (&$content) {
$updateNewLines = function (&$content): void {
$content = str_replace("\n<head>", '<head>', $content);
$content = str_replace("\n<body>", '<body>', $content);
$content = str_replace("\n</html>", '</html>', $content);
Expand Down Expand Up @@ -1335,7 +1335,7 @@ public function testCompatibilityWithDOMDocument()
}
};

$compareContent = function ($content) use ($compareDOMs) {
$compareContent = function ($content) use ($compareDOMs): void {
$dom = new HTML5DOMDocument();
$dom->loadHTML($content);
$dom2 = new DOMDocument();
Expand Down

0 comments on commit def94db

Please sign in to comment.