servo-php / fluidxml

FluidXML, the PHP library for manipulating XML with a concise and fluent API.
BSD 2-Clause "Simplified" License
458 stars 45 forks source link

Improvent: Support PHP 8.0+, handle null parameter #62

Closed TechOverflow closed 11 months ago

TechOverflow commented 2 years ago

I get this deprecated warning using this library, which otherwise works flawlessly.

PHP Deprecated: DOMElement::__construct(): Passing null to parameter #3 ($namespace) of type string is deprecated in /var/www/html/***/***/***/assets/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php on line 202

https://stackoverflow.com/questions/71707325/migration-to-php-8-1-how-to-fix-deprecated-passing-null-to-parameter-error-r

Cheers!

daniele-orlando commented 2 years ago

Thanks for reporting and the hint on the solution. I'll try to resolve the warning in the next version.

ioweb-gr commented 2 years ago

I noticed I face php 8.1 related errors as well

PHP Fatal error:  During inheritance of ArrayAccess: Uncaught Exception: Deprecated Functionality: Return type of FluidXml\FluidContext::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /httpdocs/vendor/servo/fluidxml/source/FluidXml/FluidContext.php on line 53

After adjusting the function declarations to be compatible with the interfaces used, then I received the error mentioned in the OP.

Index: vendor/servo/fluidxml/source/FluidXml/FluidContext.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/servo/fluidxml/source/FluidXml/FluidContext.php b/vendor/servo/fluidxml/source/FluidXml/FluidContext.php
--- a/vendor/servo/fluidxml/source/FluidXml/FluidContext.php    
+++ b/vendor/servo/fluidxml/source/FluidXml/FluidContext.php    (date 1663106286213)
@@ -39,7 +39,7 @@
         }

         // \ArrayAccess interface.
-        public function offsetSet($offset, $value)
+        public function offsetSet($offset, $value): void
         {
                 // if (\is_null($offset)) {
                 //         $this->nodes[] = $value;
@@ -50,20 +50,20 @@
         }

         // \ArrayAccess interface.
-        public function offsetExists($offset)
+        public function offsetExists($offset): bool
         {
                 return isset($this->nodes[$offset]);
         }

         // \ArrayAccess interface.
-        public function offsetUnset($offset)
+        public function offsetUnset($offset): void
         {
                 // unset($this->nodes[$offset]);
                 \array_splice($this->nodes, $offset, 1);
         }

         // \ArrayAccess interface.
-        public function offsetGet($offset)
+        public function offsetGet($offset): mixed
         {
                 if (isset($this->nodes[$offset])) {
                         return $this->nodes[$offset];
@@ -73,31 +73,31 @@
         }

         // \Iterator interface.
-        public function rewind()
+        public function rewind(): void
         {
                 $this->seek = 0;
         }

         // \Iterator interface.
-        public function current()
+        public function current(): mixed
         {
                 return $this->nodes[$this->seek];
         }

         // \Iterator interface.
-        public function key()
+        public function key(): mixed
         {
                 return $this->seek;
         }

         // \Iterator interface.
-        public function next()
+        public function next(): void
         {
                 ++$this->seek;
         }

         // \Iterator interface.
-        public function valid()
+        public function valid(): bool
         {
                 return isset($this->nodes[$this->seek]);
         }
wucherpfennig commented 2 years ago

Thanks for reporting and the hint on the solution. I'll try to resolve the warning in the next version.

will there be actually a release? ;-) I would very much appreciate an updated release to cover php 8 / 8.1 as of now I get flooded with warnings ;-)

XIAOXIAOSIYU commented 1 year ago

Same deprecation warning here in PHP 8.1:

Deprecated: Return type of FluidXml\FluidContext::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in D:\PHP\webroot\coolerdepotusa\vendor\servo\fluidxml\source\FluidXml\FluidContext.php on line 53

Deprecated: Return type of FluidXml\FluidContext::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in D:\PHP\webroot\coolerdepotusa\vendor\servo\fluidxml\source\FluidXml\FluidContext.php on line 66

ioweb-gr commented 1 year ago

The project looks dead right?

wucherpfennig commented 1 year ago

I do think so. You have another library to work with?

ioweb-gr commented 1 year ago

Nope, I created a small patch for 8.1 for my needs so far and I use cweagans/composer-patches to apply it, but I'm looking for a long-term alternative. A lot of people submit PRs here but they don't get merged so it's sad to see it go :(

ioweb-gr commented 1 year ago

If you want it it's this one

Index: vendor/servo/fluidxml/source/FluidXml/FluidContext.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/servo/fluidxml/source/FluidXml/FluidContext.php b/vendor/servo/fluidxml/source/FluidXml/FluidContext.php
--- a/vendor/servo/fluidxml/source/FluidXml/FluidContext.php
+++ b/vendor/servo/fluidxml/source/FluidXml/FluidContext.php    (date 1663106286213)
@@ -39,7 +39,7 @@
         }

         // \ArrayAccess interface.
-        public function offsetSet($offset, $value)
+        public function offsetSet($offset, $value): void
         {
                 // if (\is_null($offset)) {
                 //         $this->nodes[] = $value;
@@ -50,20 +50,20 @@
         }

         // \ArrayAccess interface.
-        public function offsetExists($offset)
+        public function offsetExists($offset): bool
         {
                 return isset($this->nodes[$offset]);
         }

         // \ArrayAccess interface.
-        public function offsetUnset($offset)
+        public function offsetUnset($offset): void
         {
                 // unset($this->nodes[$offset]);
                 \array_splice($this->nodes, $offset, 1);
         }

         // \ArrayAccess interface.
-        public function offsetGet($offset)
+        public function offsetGet($offset): mixed
         {
                 if (isset($this->nodes[$offset])) {
                         return $this->nodes[$offset];
@@ -73,31 +73,31 @@
         }

         // \Iterator interface.
-        public function rewind()
+        public function rewind(): void
         {
                 $this->seek = 0;
         }

         // \Iterator interface.
-        public function current()
+        public function current(): mixed
         {
                 return $this->nodes[$this->seek];
         }

         // \Iterator interface.
-        public function key()
+        public function key(): mixed
         {
                 return $this->seek;
         }

         // \Iterator interface.
-        public function next()
+        public function next(): void
         {
                 ++$this->seek;
         }

         // \Iterator interface.
-        public function valid()
+        public function valid(): bool
         {
                 return isset($this->nodes[$this->seek]);
         }

======================================================
diff --git a/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php b/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php
--- a/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php
+++ b/vendor/servo/fluidxml/source/FluidXml/FluidInsertionHandler.php   (date 1663106563856)
@@ -199,7 +199,7 @@
                 }

                 // Algorithm 1:
-                $el = new \DOMElement($name, $value, $uri);
+                $el = new \DOMElement($name, $value, $uri ?? '');

                 // Algorithm 2:
                 // $el = $dom->createElement($name, $value);
daniele-orlando commented 11 months ago

PHP 8.1+ support is implemented with https://github.com/servo-php/fluidxml/pull/63.