Skip to content

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