Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidearl opened this issue Jan 6, 2023 · 0 comments

Comments

@davidearl
Copy link

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);
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant