Skip to content

Commit

Permalink
Narrow the type for the mb_ereg_replace_callback() callable.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion authored and ondrejmirtes committed May 16, 2022
1 parent fc839cf commit 17f826f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6318,7 +6318,7 @@
'mb_ereg' => ['int|false', 'pattern'=>'string', 'string'=>'string', '&w_registers='=>'array'],
'mb_ereg_match' => ['bool', 'pattern'=>'string', 'string'=>'string', 'option='=>'string'],
'mb_ereg_replace' => ['string|false|null', 'pattern'=>'string', 'replacement'=>'string', 'string'=>'string', 'option='=>'string'],
'mb_ereg_replace_callback' => ['string|false|null', 'pattern'=>'string', 'callback'=>'callable', 'string'=>'string', 'option='=>'string'],
'mb_ereg_replace_callback' => ['string|false|null', 'pattern'=>'string', 'callback'=>'callable(array<int|string, string>):string', 'string'=>'string', 'option='=>'string'],
'mb_ereg_search' => ['bool', 'pattern='=>'string', 'option='=>'string'],
'mb_ereg_search_getpos' => ['int'],
'mb_ereg_search_getregs' => ['array|false'],
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,28 @@ public function testPregReplaceCallback(): void
]);
}

public function testMbEregReplaceCallback(): void
{
$this->analyse([__DIR__ . '/data/mb_ereg_replace_callback.php'], [
[
'Parameter #2 $callback of function mb_ereg_replace_callback expects callable(array<int|string, string>): string, Closure(string): string given.',
6,
],
[
'Parameter #2 $callback of function mb_ereg_replace_callback expects callable(array<int|string, string>): string, Closure(string): string given.',
13,
],
[
'Parameter #2 $callback of function mb_ereg_replace_callback expects callable(array<int|string, string>): string, Closure(array): void given.',
20,
],
[
'Parameter #2 $callback of function mb_ereg_replace_callback expects callable(array<int|string, string>): string, Closure(): void given.',
25,
],
]);
}

public function testUasortCallback(): void
{
$this->analyse([__DIR__ . '/data/uasort.php'], [
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Functions/data/mb_ereg_replace_callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

// Incorrect
$string = mb_ereg_replace_callback(
'pattern',
function(string $string): string {
return $string;
},
'subject'
);
$string = mb_ereg_replace_callback(
'pattern',
function(string $string) {
return $string;
},
'subject'
);
$string = mb_ereg_replace_callback(
'pattern',
function(array $matches) {},
'subject'
);
$string = mb_ereg_replace_callback(
'pattern',
function() {},
'subject'
);

// Correct
$string = mb_ereg_replace_callback(
'pattern',
function(array $matches): string {
return $matches[0];
},
'subject'
);
$string = mb_ereg_replace_callback(
'pattern',
function(array $matches) {
return $matches[0];
},
'subject'
);
$string = mb_ereg_replace_callback(
'pattern',
function() {
return 'Hello';
},
'subject'
);

0 comments on commit 17f826f

Please sign in to comment.