rector icon indicating copy to clipboard operation
rector copied to clipboard

[php 8.1] preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated

Open codebymikey opened this issue 9 months ago • 2 comments

Feature Request

There's an unhandled PHP 8.1 deprecation scenario with preg_split that's currently not handled.

Example https://3v4l.org/cVPim

I guess we need something similar to NullToStrictStringFuncCallArgRector but for integers.

Diff

Proposed change should be to convert the NULL to a -1.

$output = "a  b\n\nc";
-var_dump(preg_split('/\s/', $output, NULL, PREG_SPLIT_NO_EMPTY));
+var_dump(preg_split('/\s/', $output, -1, PREG_SPLIT_NO_EMPTY));
$limit = null;
-var_dump(preg_split('/\s/', $output, $limit, PREG_SPLIT_NO_EMPTY));
+var_dump(preg_split('/\s/', $output, (int) $limit, PREG_SPLIT_NO_EMPTY));

codebymikey avatar May 09 '25 10:05 codebymikey

what the different between pass -1 and 0?

using cast (int) NULL result 0 if we are going to using cast for similar transform

samsonasik avatar May 09 '25 10:05 samsonasik

Reading documentation, it seems pass 0 or -1 is equal:

limit
----

    If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1 or 0 means "no limit".

so, it seems similar logic can be used for the new rule to use cast int for defined functions.

could you create PR? Thank you

samsonasik avatar May 09 '25 13:05 samsonasik

@samsonasik Could you add a rule for this one as well? 👍 Should be part of PHP 8.1 set

TomasVotruba avatar Sep 10 '25 15:09 TomasVotruba

I will try

samsonasik avatar Sep 10 '25 15:09 samsonasik

I created PR:

  • https://github.com/rectorphp/rector-src/pull/7240

for it.

samsonasik avatar Sep 11 '25 03:09 samsonasik