Skip to content

Commit

Permalink
Static analysis compatibility with PHP 8.1 and PHP 8.4
Browse files Browse the repository at this point in the history
* tweak: php 8.1 support

* tweak: php 8.4 support

* tweak: inherit integers from parent node class

* tweak: inherit integers from parent node class

* tweak: class definition

* tweak: fix node extension issue

* tweak: php 8.4 changes

* tweak: phpcs ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files

* tweak: phpstan ignore node files
  • Loading branch information
g105b authored Jan 2, 2025
1 parent 6548066 commit 2de9031
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 44 deletions.
11 changes: 6 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, 8.3 ]
php: [ 8.1, 8.2, 8.3, 8.4 ]

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

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, 8.3 ]
php: [ 8.1, 8.2, 8.3, 8.4 ]

steps:
- uses: actions/download-artifact@v3
Expand All @@ -106,13 +106,14 @@ jobs:
with:
php_version: ${{ matrix.php }}
path: src/
configuration: phpstan.neon

phpmd:
runs-on: ubuntu-latest
needs: [ composer ]
strategy:
matrix:
php: [ 8.1, 8.2, 8.3 ]
php: [ 8.1, 8.2, 8.3, 8.4 ]

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

steps:
- uses: actions/download-artifact@v3
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 6
paths:
- src/
excludePaths:
- src/Node83.php
- src/Node84.php
43 changes: 8 additions & 35 deletions src/Node.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
<?php
// phpcs:ignoreFile
namespace Gt\Dom;

use DOMNode;

class Node extends DOMNode {
use NonDocumentTypeChildNode;
use ChildNode;
use ParentNode;
use RegisteredNodeClass;

const TYPE_ELEMENT = XML_ELEMENT_NODE;
const TYPE_ATTR = XML_ATTRIBUTE_NODE;
const TYPE_TEXT = XML_TEXT_NODE;
const TYPE_CDATA = XML_CDATA_SECTION_NODE;
const TYPE_ENTITY_REF = XML_ENTITY_REF_NODE;
const TYPE_ENTITY = XML_ENTITY_NODE;
const TYPE_PI = XML_PI_NODE;
const TYPE_COMMENT = XML_COMMENT_NODE;
const TYPE_DOCUMENT = XML_DOCUMENT_NODE;
const TYPE_DOCUMENT_TYPE = XML_DOCUMENT_TYPE_NODE;
const TYPE_DOCUMENT_FRAGMENT = XML_DOCUMENT_FRAG_NODE;
const TYPE_NOTATION = XML_NOTATION_NODE;
const TYPE_HTML_DOCUMENT = XML_HTML_DOCUMENT_NODE;
const TYPE_DTD = XML_DTD_NODE;
const TYPE_ELEMENT_DECL = XML_ELEMENT_DECL_NODE;
const TYPE_ATTRIBUTE_DECL = XML_ATTRIBUTE_DECL_NODE;
const TYPE_ENTITY_DECL = XML_ENTITY_DECL_NODE;
const TYPE_NAMESPACE_DECL = XML_NAMESPACE_DECL_NODE;

public const DOCUMENT_POSITION_DISCONNECTED = 0b000001;
public const DOCUMENT_POSITION_PRECEDING = 0b000010;
public const DOCUMENT_POSITION_FOLLOWING = 0b000100;
public const DOCUMENT_POSITION_CONTAINS = 0b001000;
public const DOCUMENT_POSITION_CONTAINED_BY = 0b010000;
public const DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0b100000;

private function __construct() {}
if (version_compare(PHP_VERSION, '8.4', '>=')) {
class Node extends Node84 {
}
}
else {
class Node extends Node83 {
}
}
40 changes: 40 additions & 0 deletions src/Node83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// phpcs:ignoreFile
namespace Gt\Dom;

use DOMNode;

class Node83 extends DOMNode {
use NonDocumentTypeChildNode;
use ChildNode;
use ParentNode;
use RegisteredNodeClass;

const TYPE_ELEMENT = XML_ELEMENT_NODE;
const TYPE_ATTR = XML_ATTRIBUTE_NODE;
const TYPE_TEXT = XML_TEXT_NODE;
const TYPE_CDATA = XML_CDATA_SECTION_NODE;
const TYPE_ENTITY_REF = XML_ENTITY_REF_NODE;
const TYPE_ENTITY = XML_ENTITY_NODE;
const TYPE_PI = XML_PI_NODE;
const TYPE_COMMENT = XML_COMMENT_NODE;
const TYPE_DOCUMENT = XML_DOCUMENT_NODE;
const TYPE_DOCUMENT_TYPE = XML_DOCUMENT_TYPE_NODE;
const TYPE_DOCUMENT_FRAGMENT = XML_DOCUMENT_FRAG_NODE;
const TYPE_NOTATION = XML_NOTATION_NODE;
const TYPE_HTML_DOCUMENT = XML_HTML_DOCUMENT_NODE;
const TYPE_DTD = XML_DTD_NODE;
const TYPE_ELEMENT_DECL = XML_ELEMENT_DECL_NODE;
const TYPE_ATTRIBUTE_DECL = XML_ATTRIBUTE_DECL_NODE;
const TYPE_ENTITY_DECL = XML_ENTITY_DECL_NODE;
const TYPE_NAMESPACE_DECL = XML_NAMESPACE_DECL_NODE;

public const DOCUMENT_POSITION_DISCONNECTED = 0b000001;
public const DOCUMENT_POSITION_PRECEDING = 0b000010;
public const DOCUMENT_POSITION_FOLLOWING = 0b000100;
public const DOCUMENT_POSITION_CONTAINS = 0b001000;
public const DOCUMENT_POSITION_CONTAINED_BY = 0b010000;
public const DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0b100000;

private function __construct() {}
}
40 changes: 40 additions & 0 deletions src/Node84.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// phpcs:ignoreFile
namespace Gt\Dom;

use DOMNode;

class Node84 extends DOMNode {
use NonDocumentTypeChildNode;
use ChildNode;
use ParentNode;
use RegisteredNodeClass;

const TYPE_ELEMENT = XML_ELEMENT_NODE;
const TYPE_ATTR = XML_ATTRIBUTE_NODE;
const TYPE_TEXT = XML_TEXT_NODE;
const TYPE_CDATA = XML_CDATA_SECTION_NODE;
const TYPE_ENTITY_REF = XML_ENTITY_REF_NODE;
const TYPE_ENTITY = XML_ENTITY_NODE;
const TYPE_PI = XML_PI_NODE;
const TYPE_COMMENT = XML_COMMENT_NODE;
const TYPE_DOCUMENT = XML_DOCUMENT_NODE;
const TYPE_DOCUMENT_TYPE = XML_DOCUMENT_TYPE_NODE;
const TYPE_DOCUMENT_FRAGMENT = XML_DOCUMENT_FRAG_NODE;
const TYPE_NOTATION = XML_NOTATION_NODE;
const TYPE_HTML_DOCUMENT = XML_HTML_DOCUMENT_NODE;
const TYPE_DTD = XML_DTD_NODE;
const TYPE_ELEMENT_DECL = XML_ELEMENT_DECL_NODE;
const TYPE_ATTRIBUTE_DECL = XML_ATTRIBUTE_DECL_NODE;
const TYPE_ENTITY_DECL = XML_ENTITY_DECL_NODE;
const TYPE_NAMESPACE_DECL = XML_NAMESPACE_DECL_NODE;

public const int DOCUMENT_POSITION_DISCONNECTED = 0b000001;
public const int DOCUMENT_POSITION_PRECEDING = 0b000010;
public const int DOCUMENT_POSITION_FOLLOWING = 0b000100;
public const int DOCUMENT_POSITION_CONTAINS = 0b001000;
public const int DOCUMENT_POSITION_CONTAINED_BY = 0b010000;
public const int DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0b100000;

private function __construct() {}
}
11 changes: 8 additions & 3 deletions test/phpunit/NodeTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Gt\Dom\Test;

use Gt\Dom\Exception\ClientSideOnlyFunctionalityException;
use Gt\Dom\Exception\NotFoundErrorException;
use Gt\Dom\HTMLDocument;
use Gt\Dom\Node;
Expand All @@ -13,7 +12,7 @@
class NodeTest extends TestCase {
public function testCanNotConstruct():void {
self::expectException(\Error::class);
self::expectExceptionMessage("Call to private Gt\Dom\Node::__construct()");
self::expectExceptionMessageMatches("/Call to private Gt.Dom.Node[0-9]*::__construct()/");
$className = Node::class;
/** @phpstan-ignore-next-line */
new $className();
Expand All @@ -22,7 +21,13 @@ public function testCanNotConstruct():void {
public function testBaseURIClientSideOnly():void {
$document = new XMLDocument();
$sut = $document->createElement("example");
self::assertNull($sut->baseURI);

if(version_compare(PHP_VERSION, "8.4", ">=")) {
self::assertEquals(getcwd() . "/", $sut->baseURI);
}
else {
self::assertNull($sut->baseURI);
}
}

public function testChildNodesEmpty():void {
Expand Down
7 changes: 6 additions & 1 deletion test/phpunit/XMLDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public function testBodyNullOnXML():void {

public function testToStringEmptyXML():void {
$sut = new XMLDocument();
self::assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml/>\n", (string)$sut);
if(version_compare(PHP_VERSION, "8.4", ">=")) {
self::assertEquals("<?xml version=\"1.0\"?>\n<xml/>\n", (string)$sut);
}
else {
self::assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xml/>\n", (string)$sut);
}
}

public function testPropContentTypeEmpty():void {
Expand Down

0 comments on commit 2de9031

Please sign in to comment.