Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preg_replace_callback doesn't account for flags in callback signature #10396

Open
Riimu opened this issue Jan 5, 2024 · 2 comments · May be fixed by phpstan/phpstan-src#3347
Open

preg_replace_callback doesn't account for flags in callback signature #10396

Riimu opened this issue Jan 5, 2024 · 2 comments · May be fixed by phpstan/phpstan-src#3347
Labels
Milestone

Comments

@Riimu
Copy link
Contributor

Riimu commented Jan 5, 2024

Bug report

PHP 7.4 added support for flags to preg_replace_callback() and preg_replace_callback_array(). It seems, however, that PHPStan doesn't account for the $flags parameter when determining the signature of the callback.

Currently, PHPStan expects the callback signature to be callable(array<int|string, string>): string, which is not true if PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL flags are used

Code snippet that reproduces the problem

https://phpstan.org/r/9b24636c-c960-45c8-9ef7-5ee47f34623f

Expected output

There should be no errors

Did PHPStan help you today? Did it make you happy in any way?

No response

@phpstan-bot
Copy link
Contributor

@Riimu After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
 5: First-class callables are supported only on PHP 8.1 and later.
 5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
 
-PHP 7.4 (3 errors)
+PHP 7.4 (6 errors)
 ==========
 
+3: Function test() has invalid return type null.
+3: Function test() never returns null so it can be removed from the return type.
 3: Function test() uses native union types but they're supported only on PHP 8.0 and later.
 5: First-class callables are supported only on PHP 8.1 and later.
+5: Function test() should return null|string but returns string|null.
 5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
 
-PHP 7.2 – 7.3 (4 errors)
+PHP 7.2 – 7.3 (7 errors)
 ==========
 
+3: Function test() has invalid return type null.
+3: Function test() never returns null so it can be removed from the return type.
 3: Function test() uses native union types but they're supported only on PHP 8.0 and later.
 5: First-class callables are supported only on PHP 8.1 and later.
 5: Function preg_replace_callback invoked with 6 parameters, 3-5 required.
+5: Function test() should return null|string but returns string|null.
 5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
Full report

PHP 8.1 – 8.3 (1 error)

Line Error
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array<int

PHP 8.0 (2 errors)

Line Error
5 First-class callables are supported only on PHP 8.1 and later.
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array<int

PHP 7.4 (6 errors)

Line Error
3 Function test() has invalid return type null.
3 Function test() never returns null so it can be removed from the return type.
3 Function test() uses native union types but they're supported only on PHP 8.0 and later.
5 First-class callables are supported only on PHP 8.1 and later.
5 `Function test() should return null
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array<int

PHP 7.2 – 7.3 (7 errors)

Line Error
3 Function test() has invalid return type null.
3 Function test() never returns null so it can be removed from the return type.
3 Function test() uses native union types but they're supported only on PHP 8.0 and later.
5 First-class callables are supported only on PHP 8.1 and later.
5 Function preg_replace_callback invoked with 6 parameters, 3-5 required.
5 `Function test() should return null
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array<int

@phpstan-bot
Copy link
Contributor

@Riimu After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
 PHP 8.1 – 8.3 (1 error)
 ==========
 
-5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
+5: Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(array<int|string, array{string|null, int}>): string given.
 
 PHP 8.0 (2 errors)
 ==========
 
 5: First-class callables are supported only on PHP 8.1 and later.
-5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
+5: Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(array<int|string, array{string|null, int}>): string given.
 
-PHP 7.4 (3 errors)
+PHP 7.4 (6 errors)
 ==========
 
+3: Function test() has invalid return type null.
+3: Function test() never returns null so it can be removed from the return type.
 3: Function test() uses native union types but they're supported only on PHP 8.0 and later.
 5: First-class callables are supported only on PHP 8.1 and later.
-5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
+5: Function test() should return null|string but returns string|null.
+5: Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(array<int|string, array{string|null, int}>): string given.
 
-PHP 7.2 – 7.3 (4 errors)
+PHP 7.2 – 7.3 (7 errors)
 ==========
 
+3: Function test() has invalid return type null.
+3: Function test() never returns null so it can be removed from the return type.
 3: Function test() uses native union types but they're supported only on PHP 8.0 and later.
 5: First-class callables are supported only on PHP 8.1 and later.
 5: Function preg_replace_callback invoked with 6 parameters, 3-5 required.
-5: Parameter #2 $callback of function preg_replace_callback expects callable(array<int|string, string>): string, Closure(array<int|string, array{string|null, int}>): string given.
+5: Function test() should return null|string but returns string|null.
+5: Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(array<int|string, array{string|null, int}>): string given.
Full report

PHP 8.1 – 8.3 (1 error)

Line Error
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(array<int

PHP 8.0 (2 errors)

Line Error
5 First-class callables are supported only on PHP 8.1 and later.
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(array<int

PHP 7.4 (6 errors)

Line Error
3 Function test() has invalid return type null.
3 Function test() never returns null so it can be removed from the return type.
3 Function test() uses native union types but they're supported only on PHP 8.0 and later.
5 First-class callables are supported only on PHP 8.1 and later.
5 `Function test() should return null
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(array<int

PHP 7.2 – 7.3 (7 errors)

Line Error
3 Function test() has invalid return type null.
3 Function test() never returns null so it can be removed from the return type.
3 Function test() uses native union types but they're supported only on PHP 8.0 and later.
5 First-class callables are supported only on PHP 8.1 and later.
5 Function preg_replace_callback invoked with 6 parameters, 3-5 required.
5 `Function test() should return null
5 `Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(array<int

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants