Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOMStringMap improvements with PHP 8.3 support #450

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

outputs:
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
Expand Down Expand Up @@ -90,7 +90,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -112,7 +112,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -136,7 +136,7 @@ jobs:
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]
php: [ 8.1, 8.2, 8.3 ]

steps:
- uses: actions/download-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phpgt/dom",
"description": "The modern DOM API for PHP projects.",
"description": "Modern DOM API.",
"type": "library",

"require": {
Expand Down
2 changes: 1 addition & 1 deletion src/DOMStringMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function count():int {

private function correctCamelCase(string $name):string {
preg_match_all(
'/((?:^|[A-Z])[a-z\-]+)/',
'/((?:^|[A-Z])[0-9a-z\-]+)/',
$name,
$matches
);
Expand Down
6 changes: 3 additions & 3 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ public function getElementById(string $elementId):?Element {

/**
* @see Node::isEqualNode()
* @param Node|Element $otherNode
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
public function isEqualNode(Node|Element|DOMNode $otherNode):bool {
public function isEqualNode(
null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
):bool {
return $this->documentElement->isEqualNode($otherNode);
}

Expand Down
6 changes: 2 additions & 4 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Countable;
use DOMElement;
use DOMNamedNodeMap;
use DOMNode;
use Gt\Dom\Exception\InvalidAdjacentPositionException;
use Gt\Dom\Exception\XPathQueryException;
use Gt\PropFunc\MagicProp;
Expand Down Expand Up @@ -329,14 +330,11 @@ public function getAttributeNames():array {
* 'beforeend': Just inside the targetElement, after its last child.
* 'afterend': After the targetElement itself.
*
* @param Node|Element $element The element to be inserted into the tree.
* @return ?Element The element that was inserted, or null, if the
* insertion failed.
* @link https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
*/
public function insertAdjacentElement(
string $position,
Node|Element|DocumentFragment|Text $element
DOMElement|DOMNode|Element|Node|DocumentFragment|Text $element
):?Element {
switch($position) {
case "beforebegin":
Expand Down
12 changes: 6 additions & 6 deletions src/RegisteredNodeClass.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Gt\Dom;

use DOMNameSpaceNode;
use DOMNode;

/**
Expand All @@ -22,13 +23,14 @@ trait RegisteredNodeClass {
* Returns a Boolean which indicates whether or not two nodes are of
* the same type and all their defining data points match.
*
* @param Node|Element|Document|DocumentType|Attr|ProcessingInstruction $otherNode
* @param null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
* The Node to compare equality with.
* @return bool
* @link https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode
*/
// phpcs:ignore
public function isEqualNode(Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode):bool {
public function isEqualNode(
null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
):bool {
if($otherNode instanceof Document) {
$otherNode = $otherNode->documentElement;
}
Expand Down Expand Up @@ -163,13 +165,11 @@ public function compareDocumentPosition(DOMNode|Node|Element $otherNode):int {
* Returns a Boolean value indicating whether or not a node is a
* descendant of the calling node.
*
* @param Node $otherNode
* @return bool
* @link https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
*/
public function contains(
Node|Element|Text|ProcessingInstruction|DocumentType|DocumentFragment
|Document|Comment|CdataSection|Attr $otherNode
|Document|Comment|CdataSection|Attr|DOMNode|DOMNameSpaceNode|null $otherNode
):bool {
$context = $otherNode;

Expand Down
60 changes: 58 additions & 2 deletions test/phpunit/DOMStringMapTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpUndefinedFieldInspection */
namespace Gt\Dom\Test;

use Gt\Dom\DOMStringMap;
Expand All @@ -19,7 +19,7 @@ public function testGetterSetter():void {
self::assertSame($keyValuePairs["example"], $sut->example);
}

public function testGetterCamelCaseConversion():void {
public function testGetter_fromCamelCase():void {
$keyValuePairs = [
"this-is-camel-case" => uniqid("example-"),
];
Expand All @@ -34,6 +34,44 @@ public function testGetterCamelCaseConversion():void {
self::assertSame($keyValuePairs["this-is-camel-case"], $sut->thisIsCamelCase);
}

public function testGetter_fromHyphenated():void {
$keyValuePairs = [
"this-is-camel-case" => uniqid("example-"),
];
$getter = function() use (&$keyValuePairs) {
return $keyValuePairs;
};
$setter = function(array $kvp) use (&$keyValuePairs) {
$keyValuePairs = $kvp;
};
$sut = new DOMStringMap($getter, $setter);

self::assertSame($keyValuePairs["this-is-camel-case"], $sut->get("this-is-camel-case"));
self::assertSame($keyValuePairs["this-is-camel-case"], $sut->get("thisIsCamelCase"));
self::assertArrayNotHasKey("thisIsCamelCase", $keyValuePairs);
}

public function testSetter_fromHyphenated():void {
$keyValuePairs = [
"this-is-camel-case" => uniqid("example-"),
];
$getter = function() use (&$keyValuePairs) {
return $keyValuePairs;
};
$setter = function(array $kvp) use (&$keyValuePairs) {
$keyValuePairs = $kvp;
};
$sut = new DOMStringMap($getter, $setter);

$sut->set("this-is-camel-case", "update1");
$sut->set("thisIsCamelCase", "update2");
$sut->set("other-key", "other-update");

self::assertCount(2, $keyValuePairs);
self::assertSame("update2", $sut->thisIsCamelCase);
self::assertSame("other-update", $sut->otherKey);
}

public function testSetterCamelCaseConversion():void {
$keyValuePairs = [];
$getter = function() use (&$keyValuePairs) {
Expand All @@ -47,4 +85,22 @@ public function testSetterCamelCaseConversion():void {
self::assertSame("example123", $sut->get("thisIsCamelCase"));
self::assertSame("example123", $sut->get("this-is-camel-case"));
}

public function testSetter_withNumbers():void {
$keyValuePairs = [];
$getter = function() use (&$keyValuePairs) {
return $keyValuePairs;
};
$setter = function(array $kvp) use (&$keyValuePairs) {
$keyValuePairs = $kvp;
};
$sut = new DOMStringMap($getter, $setter);
$sut->example1 = "one";
$sut->example2 = "two";
$sut->example3 = "three";

self::assertSame("one", $sut->example1);
self::assertSame("two", $sut->example2);
self::assertSame("three", $sut->example3);
}
}