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

Narrow the type for the $callback parameter of preg_replace_callback() #758

Merged
merged 1 commit into from Jan 31, 2022

Conversation

johnbillion
Copy link
Contributor

@johnbillion johnbillion commented Nov 6, 2021

The $callback parameter of preg_replace_callback() should be a callable which accepts an array of strings and returns a string. Its signature can be updated to reflect this.

From https://www.php.net/manual/en/function.preg-replace-callback.php:

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

handler(array $matches): string

The $matches parameter is of type array<int|string, string> because using a named capturing group results in an array element keyed by the capturing group name. Example: https://3v4l.org/bDNbd

Interestingly the callback function can also return an empty value such as null, false, or void and PHP doesn't complain about it, but I would consider it a bug if the callback returns anything other than a string. This might warrant further discussion in case there's a common use case I've not considered.

@ondrejmirtes
Copy link
Member

Thank you.

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