mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
701 stars 206 forks source link

PHP8.2: PHP Deprecation: trim(): Passing null to parameter #1 ($string) of type string is deprecated in .../Spyc.php:353 #89

Open davidearl opened 1 year ago

davidearl commented 1 year ago

Thank you for your library.

I've been running development with PHP8.2 to iron out any problems well in advance of deploying it, and hit this one. I know it's only a deprecation message, but it will become a real error over time, and is presumably not intended.

It's from a NULL value in a field in the object being passed to Spyc::YAMLDump, e.g.

echo Spyc::YAMLDump((object)['test'=>NULL]);

The code clearly intends to replace NULL with the string 'null', but prior to that it passes it to trim() as if it were already a string.

If the value is a bool, it also passes that to trim() but that doesn't seem to result in a deprecation message, nevertheless is not intended.

Personally, I'd move the bool and null transforms before the trim; use if else and is_null, as in...

 if ($value === array()) {
   $value = '[ ]';
 } else if (is_bool($value)) {
   $value = $value ? "true" : "false";
 } else if (is_null($value)) {
   $value = 'null';
 } else if  ($value === "") {
   $value = '""';
 } else if (self::isTranslationWord($value)) {
   $value = $this->_doLiteralBlock($value, $indent);
 } else if (trim ($value) != $value) {
   $value = $this->_doLiteralBlock($value,$indent);
 }