safe icon indicating copy to clipboard operation
safe copied to clipboard

Added PHPStan-generics to preg_replace

Open tjveldhuizen opened this issue 2 years ago • 4 comments

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[]

tjveldhuizen avatar Oct 17 '21 11:10 tjveldhuizen

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.

ZebulanStanphill avatar Oct 20 '21 00:10 ZebulanStanphill

Ah, so that's the reason... should we leave the PR open, or close it for now?

tjveldhuizen avatar Oct 20 '21 14:10 tjveldhuizen

@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.

ZebulanStanphill avatar Nov 02 '21 00:11 ZebulanStanphill

Sorry I completly missed this PR. I will update the phpstan recs this week

Kharhamel avatar Mar 22 '22 08:03 Kharhamel