Skip to content

Commit

Permalink
CI type safety (#427)
Browse files Browse the repository at this point in the history
* build: upgrade to phpunit 10

* ci: upgrade test runners

* maintenance: improve static analysis everywhere

* ci: add phpstan tests

* ci: php 8.1

* ci: matrix php versions

* ci: fix dynamic property usage for php 8.2
for #397

* ci: use level 6 phpstan

* CI upgrade (#422)

* build: upgrade to phpunit 10

* ci: upgrade test runners

* maintenance: improve static analysis everywhere

* ci: add phpstan tests

* ci: php 8.1

* ci: use level 6 phpstan

* maintenance: hide phpstan errors where type is definitely inferred

* tweak: better use of callable

* tweak: type safety
  • Loading branch information
g105b authored Feb 18, 2023
1 parent 4b12b6d commit 307dba0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on: [push]
jobs:
composer:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 8.1, 8.2 ]

steps:
- uses: actions/checkout@v3
Expand All @@ -18,7 +21,7 @@ jobs:
- name: Composer install
uses: php-actions/composer@v6
with:
php_version: 8.1
php_version: ${{ matrix.php }}

- name: Archive build
run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./
Expand All @@ -32,6 +35,9 @@ jobs:
phpunit:
runs-on: ubuntu-latest
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]

outputs:
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
Expand All @@ -50,7 +56,7 @@ jobs:
env:
XDEBUG_MODE: cover
with:
php_version: 8.1
php_version: ${{ matrix.php }}
php_extensions: xdebug
configuration: test/phpunit/phpunit.xml
bootstrap: vendor/autoload.php
Expand Down Expand Up @@ -84,6 +90,9 @@ jobs:
phpstan:
runs-on: ubuntu-latest
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -97,9 +106,8 @@ jobs:
- name: PHP Static Analysis
uses: php-actions/phpstan@v3
with:
php_version: 8.1
php_version: ${{ matrix.php }}
path: src/
level: 4

remove_old_artifacts:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions src/ChildNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function remove():void {
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before
*/
public function before(...$nodes):void {
$parent = $this->parentElement;
$parent = $this->parentNode;
if(!$parent) {
return;
}
Expand All @@ -67,7 +67,7 @@ public function before(...$nodes):void {
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after
*/
public function after(...$nodes):void {
$parent = $this->parentElement;
$parent = $this->parentNode;
$nextSibling = $this->nextSibling;
if(!$parent) {
return;
Expand All @@ -90,7 +90,7 @@ public function after(...$nodes):void {
* @link https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith
*/
public function replaceWith(...$nodes):void {
$parent = $this->parentElement;
$parent = $this->parentNode;
if(!$parent) {
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/HTMLDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public function __construct(
}

if($nonElementChildNodes) {
$this->documentElement->prepend(...$nonElementChildNodes);
call_user_func(
$this->documentElement->prepend(...),
...$nonElementChildNodes,
);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/ParentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ public function getElementsByTagName(string $qualifiedName):HTMLCollection {
/**
* The removeChild() method of the Node interface removes a child node
* from the DOM and returns the removed node.
*
* @param Node|Element|Text|Comment $child
*/
public function removeChild(Node|Element|Text|Comment|DOMNode|ProcessingInstruction $child):Node|Element|Text|Comment|CdataSection|ProcessingInstruction {
try {
Expand Down
9 changes: 5 additions & 4 deletions src/RegisteredNodeClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public function isEqualNode(Node|Element|Document|DocumentType|Attr|ProcessingIn
}

if($this instanceof Element
&& $otherNode instanceof Element) {
&& $otherNode instanceof Element) {
$similar = $this->namespaceURI === $otherNode->namespaceURI
&& $this->localName === $otherNode->localName
&& $this->attributes->length === $otherNode->attributes->length;
&& count($this->attributes) === count($otherNode->attributes);
if(!$similar) {
return false;
}

for($i = 0, $len = $this->attributes->length; $i < $len; $i++) {
for($i = 0, $len = count($this->attributes); $i < $len; $i++) {
$attr = $this->attributes->item($i);
$otherAttr = $otherNode->attributes->item($i);
if(!$attr->isEqualNode($otherAttr)) {
Expand Down Expand Up @@ -136,6 +136,7 @@ public function compareDocumentPosition(DOMNode|Node|Element $otherNode):int {
$unionPath = "$thisNodePath | $otherNodePath";
$xpathResult = $this->ownerDocument->evaluate($unionPath);

/** @var Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $node */
foreach($xpathResult as $node) {
if($node === $this) {
$bits |= Node::DOCUMENT_POSITION_FOLLOWING;
Expand Down Expand Up @@ -165,7 +166,7 @@ public function compareDocumentPosition(DOMNode|Node|Element $otherNode):int {
* @return bool
* @link https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
*/
public function contains(Node|Element $otherNode):bool {
public function contains(Node|Element|Text|ProcessingInstruction|DocumentType|DocumentFragment|Document|Comment|CdataSection|Attr $otherNode):bool {
$context = $otherNode;

while($context = $context->parentNode) {
Expand Down
11 changes: 10 additions & 1 deletion src/Traversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public function parentNode():null|Node|Element|Text|Attr|ProcessingInstruction|C
/** @var Element $node */
$node = $node->parentNode;

/** @phpstan-ignore-next-line */
if($node && $this->filter->acceptNode($node) === NodeFilter::FILTER_ACCEPT) {
/** @phpstan-ignore-next-line */
$this->pCurrentNode = $this->hintNullableNodeType($node);
return $this->pCurrentNode;
}
Expand Down Expand Up @@ -182,14 +184,17 @@ public function previousNode():null|Node|Element|Text|Attr|ProcessingInstruction
while(!is_null($sibling)) {
/** @var null|Node|Element|Text|Attr|ProcessingInstruction|Comment|Document|DocumentType|DocumentFragment $node */
$node = $sibling;
/** @phpstan-ignore-next-line */
$result = $this->filter->acceptNode($node);

while($result !== NodeFilter::FILTER_REJECT
&& !is_null($node->lastChild)) {
&& !is_null($node->lastChild)) {
$node = $node->lastChild;
/** @phpstan-ignore-next-line */
$result = $this->filter->acceptNode($node);
}
if($result === NodeFilter::FILTER_ACCEPT) {
/** @phpstan-ignore-next-line */
$this->pCurrentNode = $this->hintNullableNodeType($node);
return $this->pCurrentNode;
}
Expand All @@ -203,7 +208,10 @@ public function previousNode():null|Node|Element|Text|Attr|ProcessingInstruction

/** @var null|Element|Node|Text $node */
$node = $node->parentNode;

/** @phpstan-ignore-next-line */
if($this->filter->acceptNode($node) === NodeFilter::FILTER_ACCEPT) {
/** @phpstan-ignore-next-line */
$this->pCurrentNode = $this->hintNullableNodeType($node);
return $this->pCurrentNode;
}
Expand Down Expand Up @@ -388,6 +396,7 @@ private function nextSkippingChildren(
break;
}
if(!is_null($node->nextSibling)) {
/** @phpstan-ignore-next-line */
return $this->hintNodeType($node->nextSibling);
}
}
Expand Down

0 comments on commit 307dba0

Please sign in to comment.