Skip to content

feat: infer preg_replace_callback return type #60

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;

/**
* This file has been copy-pasted from PHPStan's source code but with isFunctionSupported changed.
* For now, only preg_* functions are supported.
* @See https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
*/
class ReplaceSafeFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

/** @var array<string, int> */
/** @var array<string, int> The function name associated with the typed argument position used to type the return */
private array $functions = [
'Safe\preg_replace' => 2,
'Safe\preg_replace_callback' => 2,
'Safe\preg_replace_callback_array' => 1,
];

public function isFunctionSupported(FunctionReflection $functionReflection): bool
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/Php/data/preg_match_checked.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

// @phpstan-ignore-next-line - use of unsafe is intentional
if(\preg_match($pattern, $string, $matches)) {
\PHPStan\Testing\assertType($type, $matches);
\PHPStan\Testing\assertSuperType($type, $matches);
}

if(\Safe\preg_match($pattern, $string, $matches)) {
\PHPStan\Testing\assertType($type, $matches);
\PHPStan\Testing\assertSuperType($type, $matches);
}
4 changes: 2 additions & 2 deletions tests/Type/Php/data/preg_match_unchecked.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// @phpstan-ignore-next-line - use of unsafe is intentional
\preg_match($pattern, $string, $matches);
\PHPStan\Testing\assertType($type, $matches);
\PHPStan\Testing\assertSuperType($type, $matches);

\Safe\preg_match($pattern, $string, $matches);
\PHPStan\Testing\assertType($type, $matches);
\PHPStan\Testing\assertSuperType($type, $matches);
23 changes: 23 additions & 0 deletions tests/Type/Php/data/preg_replace_return.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@

namespace TheCodingMachine\Safe\PHPStan\Type\Php\data;

// preg_replace

// preg_replace with a string pattern should return a string
$x = \Safe\preg_replace('/foo/', 'bar', 'baz');
\PHPStan\Testing\assertType("string", $x);

// preg_replace with an array pattern should return an array
$x = \Safe\preg_replace(['/foo/'], ['bar'], ['baz']);
\PHPStan\Testing\assertType("array", $x);

// preg_replace_callback

// preg_replace_callback with a string pattern should return a string
$x = \Safe\preg_replace_callback('/foo/', fn (array $matches) => 'bar', 'baz');
\PHPStan\Testing\assertType("string", $x);

// preg_replace_callback with an array pattern should return an array
$x = \Safe\preg_replace_callback(['/foo/'], fn (array $matches) => 'bar', ['baz']);
\PHPStan\Testing\assertType("array", $x);


// preg_replace_callback_array

// preg_replace_callback_array with a string pattern should return a string
$x = \Safe\preg_replace_callback_array(['/foo/' => fn (array $matches) => 'bar'], 'baz');
\PHPStan\Testing\assertType("string", $x);

// preg_replace_callback with an array pattern should return an array
$x = \Safe\preg_replace_callback_array(['/foo/' => fn (array $matches) => 'bar'], ['baz']);
\PHPStan\Testing\assertType("array", $x);