Skip to content

Cast attr to string to prevent a PHP deprecation warning in 8.1 #365

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

Merged
merged 2 commits into from
Jun 17, 2025

Conversation

beryllium
Copy link
Contributor

I received this Deprecation notice when running unit tests for sculpin under PHP 8.1:

PHP Deprecated:  preg_match_all():
  Passing null to parameter #2 ($subject) of type string is deprecated
  in sculpin/vendor/michelf/php-markdown/Michelf/MarkdownExtra.php
  on line 252

I solved it in the leanest way possible, which was to cast $attr to string. Let me know if you would prefer a different solution.

And thanks for building this library! I've been quite happy with it.

@DivineDominion
Copy link

@michelf Would this be a reasonable fix? I get this warning all over the place, too 😅

@michelf
Copy link
Owner

michelf commented Jun 16, 2025

It works. But there's a fast path at the beginning of the function:

if (empty($attr) && !$defaultIdValue && empty($classes)) {
   return "";
}

I think it'd make more sense to check for null there, after all we're already checking for an empty value there.

@beryllium
Copy link
Contributor Author

So instead of

if (empty($attr) && !$defaultIdValue && empty($classes)) {

You're thinking of something like:

if (null === $attr || (empty($attr) && !$defaultIdValue && empty($classes))) {

... or is there a simpler implementation that would provide the desired logic?

@michelf
Copy link
Owner

michelf commented Jun 17, 2025

Ah, I was wrong here. empty already checks for null in that condition. The issue happens when this if evaluates to false because of the other two conditions. That's when preg_match is fed with a null value.

So I think the best fix would be to change $attr to an empty string when null. Casting works, but I'd favor $attr ?? "" in the call to preg_match: A cast would have the downside of not complaining if the function is fed with some nonsense value (an object, an array, etc.)

@michelf michelf merged commit dbb9957 into michelf:lib Jun 17, 2025
6 of 9 checks passed
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

Successfully merging this pull request may close these issues.

3 participants