safe
safe copied to clipboard
Added PHPStan-generics to preg_replace
The subject in preg_replace can be a string or an arrays of strings. The return value is the same type as the provided subject. By adding the generics PHPStan 'knows' the correct return type in a situation like this:
use function Safe\preg_replace;
$foo = 'some string 123';
$bar = preg_replace('/[0-9]/', '', $foo);
$baz = trim($bar); // PHPStan complains that trim does not accept string|string[]
Support for array
as an @template
type bound was only very recently added to PHPStan in https://github.com/phpstan/phpstan-src/pull/673, and the current PHPStan release (0.12.99), was made a few days before that PR was merged.
So the changes in this PR will work with the next PHPStan release... but until then, this PR will have to wait.
Ah, so that's the reason... should we leave the PR open, or close it for now?
@tjveldhuizen PHPStan 1.0 just came out, and it includes support for @template T of array
, so once this project updates its PHPStan requirement, this PR can be rebased and should start working as intended.
Sorry I completly missed this PR. I will update the phpstan recs this week