Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 5cddd4f

Browse files
committed
Fix/Avoid deprecation notices on PHP 8.1
1 parent 6e8203e commit 5cddd4f

13 files changed

+70
-63
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ The classes contained within this repository extend the standard DOM to use exce
55
all occasions of errors instead of PHP warnings or notices. They also add various custom methods
66
and shortcuts for convenience and to simplify the usage of DOM.
77

8-
[![Build Status](https://travis-ci.org/theseer/fDOMDocument.png)](https://travis-ci.org/theseer/fDOMDocument)
9-
108
Requirements
119
------------
1210

1311
PHP: 5.3.3 (5.3.0-5.3.2 had serious issues with spl stacked autoloaders)
1412
Extensions: dom, libxml
1513

14+
Aus of 1.6.7, tests will require PHPUnit 8.5 and PHP 7.3+
1615

1716
Installation
1817
------------
@@ -61,6 +60,10 @@ Usage Samples
6160

6261
Changelog
6362
---------
63+
##### Release 1.6.7
64+
* Add `#[\ReturnTypeWillChange]` attribute to shut up PHP 8.1 notices on return types
65+
* Fix deprecation notice for passing `NULL` where `int` is expected
66+
6467
##### Release 1.6.6
6568
* Merge PRs 33+34: Add support for parameter "asTextNode" to fDOMElement::appendElement().
6669
fDOMElement::appendElementNS() and fDOMElement::appendElementPrefix

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"ext-dom": "*",
1919
"lib-libxml": "*"
2020
},
21+
"require-dev": {
22+
"php": ">=7.3"
23+
},
2124
"autoload": {
2225
"classmap": [
2326
"src/"

phive.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phive xmlns="https://phar.io/phive">
3+
<phar name="phpunit" version="^8.5" installed="8.5.23" location="./tools/phpunit" copy="false"/>
4+
</phive>

phpunit.xml.dist

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
4+
backupGlobals="false"
35
backupStaticAttributes="false"
46
verbose="true"
5-
bootstrap="src/autoload.php">
7+
bootstrap="src/autoload.php"
8+
convertErrorsToExceptions="true"
9+
convertDeprecationsToExceptions="true"
10+
>
611

712
<testsuites>
813
<testsuite name="fDOMDocument">
@@ -11,11 +16,9 @@
1116
</testsuites>
1217

1318
<logging>
14-
<log type="coverage-html" target="build/coverage" title="fDOMDocument"
15-
charset="UTF-8" yui="true" highlight="true"
16-
lowUpperBound="35" highLowerBound="70"/>
19+
<log type="coverage-html" target="build/coverage" lowUpperBound="35" highLowerBound="70"/>
1720
<log type="coverage-clover" target="build/logs/clover.xml"/>
18-
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
21+
<log type="junit" target="build/logs/junit.xml" />
1922
</logging>
2023

2124
<filter>

src/fDOMDocument.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function loadXML($source, $options = LIBXML_NONET) {
168168
$this->xp = NULL;
169169
$tmp = parent :: loadXML($source, $options);
170170
if (!$tmp || libxml_get_last_error()) {
171-
throw new fDOMException('parsing string failed', fDOMException::ParseError);
171+
throw new fDOMException('parsing string failed:' . (libxml_get_last_error())->message, fDOMException::ParseError);
172172
}
173173
$this->registerNodeClasses();
174174
return TRUE;
@@ -246,6 +246,7 @@ public function loadHTML($source, $options = NULL) {
246246
*
247247
* @return integer bytes saved
248248
*/
249+
#[\ReturnTypeWillChange]
249250
public function save($filename, $options = NULL) {
250251
$tmp = parent::save($filename, $options);
251252
if (!$tmp) {
@@ -263,6 +264,7 @@ public function save($filename, $options = NULL) {
263264
*
264265
* @return string html content
265266
*/
267+
#[\ReturnTypeWillChange]
266268
public function saveHTML(\DOMNode $node = NULL) {
267269
if (version_compare(PHP_VERSION, '5.3.6', '<') && $node !== NULL) {
268270
throw new fDOMException('Passing a context node requires PHP 5.3.6+', fDOMException::SaveError);
@@ -284,6 +286,7 @@ public function saveHTML(\DOMNode $node = NULL) {
284286
*
285287
* @return integer bytes saved
286288
*/
289+
#[\ReturnTypeWillChange]
287290
public function saveHTMLFile($filename, $options = NULL) {
288291
$tmp = parent::saveHTMLFile($filename, $options);
289292
if (!$tmp) {
@@ -302,9 +305,15 @@ public function saveHTMLFile($filename, $options = NULL) {
302305
*
303306
* @return string serialized XML
304307
*/
308+
#[\ReturnTypeWillChange]
305309
public function saveXML(\DOMNode $node = NULL, $options = NULL) {
306310
try {
307-
$tmp = parent::saveXML($node, $options);
311+
if ($options !== null) {
312+
$tmp = parent::saveXML($node, $options);
313+
} else {
314+
$tmp = parent::saveXML($node);
315+
}
316+
308317
if (!$tmp) {
309318
throw new fDOMException('Serializing to XML failed', fDOMException::SaveError);
310319
}
@@ -541,7 +550,9 @@ public function createElementNS($namespace, $name, $content = NULL, $asTextNode
541550

542551
/**
543552
* @return fDOMDocumentFragment
553+
*
544554
*/
555+
#[\ReturnTypeWillChange]
545556
public function createDocumentFragment() {
546557
return $this->ensureIntance(parent::createDocumentFragment());
547558
}

src/fDOMDocumentFragment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function __toString() {
6969
*
7070
* @return bool true on success
7171
*/
72+
#[\ReturnTypeWillChange]
7273
public function appendXML($str) {
7374
if (!parent::appendXML($str)) {
7475
throw new fDOMException('Appending xml string failed', fDOMException::ParseError);

src/fDOMElement.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ public function createElementNS($namespace, $name, $content = NULL, $asTextNode
231231
*
232232
* @return string
233233
*/
234+
#[\ReturnTypeWillChange]
234235
public function getAttribute($attr, $default='') {
235236
return $this->hasAttribute($attr) ? parent::getAttribute($attr) : $default;
236237
}
@@ -246,6 +247,7 @@ public function getAttribute($attr, $default='') {
246247
*
247248
* @return string
248249
*/
250+
#[\ReturnTypeWillChange]
249251
public function getAttributeNS($ns, $attr, $default='') {
250252
return $this->hasAttributeNS($ns, $attr) ? parent::getAttributeNS($ns, $attr) : $default;
251253
}
@@ -263,6 +265,7 @@ public function getAttributeNS($ns, $attr, $default='') {
263265
*
264266
* @see DOMElement::setAttribute()
265267
*/
268+
#[\ReturnTypeWillChange]
266269
public function setAttribute($attr, $value, $keepEntities=false) {
267270
if ($keepEntities === true) {
268271
$attrNode = $this->ownerDocument->createAttribute($attr);
@@ -289,6 +292,7 @@ public function setAttribute($attr, $value, $keepEntities=false) {
289292
* @return \DOMAttr|null
290293
* @see DOMElement::setAttribute()
291294
*/
295+
#[\ReturnTypeWillChange]
292296
public function setAttributeNS($ns, $attr, $value, $keepEntities=false) {
293297
if ($keepEntities === true) {
294298
$attrNode = $this->ownerDocument->createAttributeNS($ns, $attr);

src/fDOMXPath.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function prepare($xpath, array $valueMap) {
9090
*
9191
* @return \DOMNodeList
9292
*/
93+
#[\ReturnTypeWillChange]
9394
public function query($q, \DOMNode $ctx = null, $registerNodeNS = true) {
9495
libxml_clear_errors();
9596
if (version_compare(PHP_VERSION, '5.3.3', '<') || strpos(PHP_VERSION, 'hiphop') || strpos(PHP_VERSION, 'hhvm')) {
@@ -113,6 +114,7 @@ public function query($q, \DOMNode $ctx = null, $registerNodeNS = true) {
113114
*
114115
* @return mixed
115116
*/
117+
#[\ReturnTypeWillChange]
116118
public function evaluate($q, \DOMNode $ctx = null, $registerNodeNS = true) {
117119
libxml_clear_errors();
118120
if (version_compare(PHP_VERSION, '5.3.3', '<') || strpos(PHP_VERSION, 'hiphop') || strpos(PHP_VERSION, 'hhvm')) {

tests/XPathQuery.test.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@
4343

4444
use TheSeer\fDOM\XPathQuery;
4545
use TheSeer\fDOM\fDOMDocument;
46+
use TheSeer\fDOM\XPathQueryException;
4647

4748
class XPathQueryTest extends \PHPUnit\Framework\TestCase {
4849

4950
private $dom;
5051

51-
protected function setUp() {
52+
protected function setUp(): void {
5253
$this->dom = new fDOMDocument();
5354
$this->dom->loadXML('<?xml version="1.0" ?><root attr="value" />');
5455
}
@@ -59,11 +60,9 @@ public function testFindingKeysInQueryWorks() {
5960
$this->assertEquals(array('key'), $xp->getKeys());
6061
}
6162

62-
/**
63-
* @expectedException TheSeer\fDOM\XPathQueryException
64-
*/
6563
public function testTryingToBindNonExistingKeyThrowsException() {
6664
$xp = new XPathQuery(':key');
65+
$this->expectException(XPathQueryException::class);
6766
$xp->bind('other', 123);
6867
}
6968

tests/fDOMDocument.test.php

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
namespace TheSeer\fDOM\Tests {
4343

4444
use TheSeer\fDOM\fDOMDocument;
45+
use TheSeer\fDOM\fDOMException;
4546

4647
/**
4748
*
@@ -55,7 +56,7 @@ class fDOMDocumentTest extends \PHPUnit\Framework\TestCase {
5556
*/
5657
private $dom;
5758

58-
public function setUp() {
59+
public function setUp(): void {
5960
$this->dom = new fDOMDocument();
6061
}
6162

@@ -74,66 +75,48 @@ public function testGetDomXPathReturnsXPathObject() {
7475
$this->assertInstanceOf('TheSeer\fDOM\fDOMXpath', $this->dom->getDomXPath());
7576
}
7677

77-
/**
78-
* @expectedException \TheSeer\fDOM\fDOMException
79-
*/
8078
public function testAttemptingToLoadAnXMLStringWithAnUndefinedEntityThrowsException() {
79+
$this->expectException(fDOMException::class);
8180
$this->dom->loadXML('<?xml version="1.0" ?><root>&undefined;</root>');
8281
}
8382

84-
/**
85-
* @expectedException \TheSeer\fDOM\fDOMException
86-
*/
8783
public function testAttemptingToLoadAnEmptyXMLStringThrowsException() {
84+
$this->expectException(fDOMException::class);
8885
$this->dom->loadXML('');
8986
}
9087

91-
/**
92-
* @expectedException \TheSeer\fDOM\fDOMException
93-
*/
9488
public function testAttemptingToLoadWithEmptyFilenameThrowsException() {
89+
$this->expectException(fDOMException::class);
9590
$this->dom->load('');
9691
}
9792

98-
/**
99-
* @expectedException \TheSeer\fDOM\fDOMException
100-
*/
10193
public function testAttemptingToLoadHTMLWithAnEmptyFilenameThrowsException() {
94+
$this->expectException(fDOMException::class);
10295
$this->dom->loadHTMLFile('');
10396
}
10497

105-
/**
106-
* @expectedException \TheSeer\fDOM\fDOMException
107-
*/
10898
public function testAttemptingToLoadHMLWithAnEmptyStringThrowsException() {
99+
$this->expectException(fDOMException::class);
109100
$this->dom->loadHTML('');
110101
}
111102

112-
/**
113-
* @expectedException \TheSeer\fDOM\fDOMException
114-
*/
115103
public function testloadingInvalidXMLStringThrowsException() {
104+
$this->expectException(fDOMException::class);
116105
$this->dom->loadXML('<?xml version="1.0" ?><broken>');
117106
}
118107

119-
/**
120-
* @expectedException \TheSeer\fDOM\fDOMException
121-
*/
122108
public function testTryingToLoadNonExistingFileThrowsException() {
109+
$this->expectException(fDOMException::class);
123110
$this->dom->load('_does_not_exist.xml');
124111
}
125112

126-
/**
127-
* @expectedException \TheSeer\fDOM\fDOMException
128-
*/
129113
public function testloadingBrokenXMLFileThrowsException() {
114+
$this->expectException(fDOMException::class);
130115
$this->dom->load(__DIR__ . '/_data/broken.xml');
131116
}
132117

133-
/**
134-
* @expectedException \TheSeer\fDOM\fDOMException
135-
*/
136118
public function testAttemptingToLoadAnXMLFileWithAnUndefinedEntityThrowsException() {
119+
$this->expectException(fDOMException::class);
137120
$this->dom->load(__DIR__ . '/_data/undefentity.xml');
138121
}
139122

@@ -162,11 +145,9 @@ public function testSaveXMLReturnsCorrectXMLString() {
162145
$this->assertEquals($xml, $this->dom->saveXML());
163146
}
164147

165-
/**
166-
* @expectedException \TheSeer\fDOM\fDOMException
167-
*/
168148
public function testSaveXMLThrowsExceptionWithReferenceToNodeFromOtherDocument() {
169149
$dom = new fDOMDocument();
150+
$this->expectException(fDOMException::class);
170151
$this->dom->saveXML($dom->createElement('foo'));
171152
}
172153

@@ -192,10 +173,8 @@ public function testRegisteringANamespaceWithPrefixWorks() {
192173
$this->assertAttributeEquals(array('test' => 'test:uri'), 'prefixes', $this->dom);
193174
}
194175

195-
/**
196-
* @expectedException \TheSeer\fDOM\fDOMException
197-
*/
198176
public function testCreatingElementWithInvalidNameThrowsException() {
177+
$this->expectException(fDOMException::class);
199178
$node = $this->dom->createElement('in valid');
200179
}
201180

@@ -216,9 +195,9 @@ public function testCreatingNewElementByprefix() {
216195

217196
/**
218197
* @covers \TheSeer\fDOM\fDOMDocument::createElementPrefix
219-
* @expectedException \TheSeer\fDOM\fDOMException
220198
*/
221199
public function testTryingToCreateNewElementByprefixWithUndefinedPrefixThrowsException() {
200+
$this->expectException(fDOMException::class);
222201
$this->dom->createElementPrefix('test', 'node');
223202
}
224203

@@ -228,10 +207,8 @@ public function testSettingContentUnescapedForNewElementRemainsIntact() {
228207
$this->assertEquals('test & demo', $node->nodeValue);
229208
}
230209

231-
/**
232-
* @expectedException \TheSeer\fDOM\fDOMException
233-
*/
234210
public function testSettingContentUnescapedForNewElementThrowsExceptionOnInvalidEntity() {
211+
$this->expectException(fDOMException::class);
235212
$node = $this->dom->createElement('test', "test & demo");
236213
}
237214

@@ -246,10 +223,8 @@ public function testSettingContentUnescapedForNewElementWithNamespaceRemainsInta
246223
$this->assertEquals('test & demo', $node->nodeValue);
247224
}
248225

249-
/**
250-
* @expectedException \TheSeer\fDOM\fDOMException
251-
*/
252226
public function testSettingContentUnescapedForNewElementWithNamespaceThrowsExceptionOnInvalidEntity() {
227+
$this->expectException(fDOMException::class);
253228
$node = $this->dom->createElementNS('test:uri', 'test', "test & demo");
254229
}
255230

0 commit comments

Comments
 (0)