Skip to content

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.

@johnbillion johnbillion force-pushed the preg_replace_callback branch from a9d4e87 to c9e7fa7 Compare November 6, 2021 18:17
@ondrejmirtes ondrejmirtes merged commit 64d7ac8 into phpstan:master Jan 31, 2022
@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

Development

Successfully merging this pull request may close these issues.

2 participants