Skip to content

make preg_replace() use generics#705

Open
alexchexes wants to merge 1 commit into
thecodingmachine:masterfrom
alexchexes:patch-1
Open

make preg_replace() use generics#705
alexchexes wants to merge 1 commit into
thecodingmachine:masterfrom
alexchexes:patch-1

Conversation

@alexchexes

Copy link
Copy Markdown

It's a pain to cast the result of \Safe\preg_replace() to string every time. This PR makes the preg_replace return type conditional on the $subject parameter type, so if you pass a string, the return type is also string.

@shish

shish commented Jan 5, 2026

Copy link
Copy Markdown
Collaborator

I wonder how this interacts with https://github.com/thecodingmachine/phpstan-safe-rule which IIRC does the same thing and more 🤔

It's a pain to cast the result of `\Safe\preg_replace()` to `string` every time. This PR makes the `preg_replace` return type conditional on the `$subject` parameter type, so if you pass a string, the return type is also `string`.
@alexchexes

Copy link
Copy Markdown
Author

@shish
Other tools, including IDE extensions like Intelephense cannot use PHPStan extensions.

Some users intentionally do not use phpstan-safe-rule.

This doesn't conflict with it, PHPStan will keep using the rule's dynamic return type extension. This PR just makes the base safe PHPDoc more accurate for tools/users that rely on the package types directly.

Please clarify if anything else is needed on my end for this PR to be merged.

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