Skip to content

Commit 670bcc1

Browse files
phpstan-botstaabm
andauthored
Report invalid $flags constants for preg_replace_callback() and preg_replace_callback_array() (#6019)
Co-authored-by: staabm <120441+staabm@users.noreply.github.com>
1 parent 36c4f3d commit 670bcc1

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎resources/constantToFunctionParameterMap.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@
140140
],
141141
],
142142

143+
'preg_replace_callback' => [
144+
'flags' => [
145+
'type' => 'bitmask',
146+
'constants' => [
147+
'PREG_OFFSET_CAPTURE',
148+
'PREG_UNMATCHED_AS_NULL',
149+
],
150+
],
151+
],
152+
153+
'preg_replace_callback_array' => [
154+
'flags' => [
155+
'type' => 'bitmask',
156+
'constants' => [
157+
'PREG_OFFSET_CAPTURE',
158+
'PREG_UNMATCHED_AS_NULL',
159+
],
160+
],
161+
],
162+
143163
// ————————————————————————————————————————————
144164
// Sorting
145165
// ————————————————————————————————————————————

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,14 @@ public function testConstantParameterCheck(): void
28872887
'Constant JSON_THROW_ON_ERROR is not allowed for parameter #3 $depth of function json_decode.',
28882888
99,
28892889
],
2890+
[
2891+
'Constant PREG_SPLIT_NO_EMPTY is not allowed for parameter #6 $flags of function preg_replace_callback.',
2892+
110,
2893+
],
2894+
[
2895+
'Constant PREG_SPLIT_NO_EMPTY is not allowed for parameter #5 $flags of function preg_replace_callback_array.',
2896+
119,
2897+
],
28902898
]);
28912899
}
28922900

‎tests/PHPStan/Rules/Functions/data/constant-parameter-check.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,18 @@ public function hashPassword(string $password): string
105105

106106
// PHP_OS passed to $subject of preg_match - should not report
107107
preg_match('/foo/', PHP_OS);
108+
109+
// preg_replace_callback: wrong constant in $flags
110+
preg_replace_callback('/foo/', fn ($m) => $m[0], 'subject', -1, $count, PREG_SPLIT_NO_EMPTY);
111+
112+
// preg_replace_callback: correct constant in $flags
113+
preg_replace_callback('/foo/', fn ($m) => $m[0], 'subject', -1, $count, PREG_OFFSET_CAPTURE);
114+
115+
// preg_replace_callback: correct bitmask in $flags
116+
preg_replace_callback('/foo/', fn ($m) => $m[0], 'subject', -1, $count, PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL);
117+
118+
// preg_replace_callback_array: wrong constant in $flags
119+
preg_replace_callback_array(['/foo/' => fn ($m) => $m[0]], 'subject', -1, $count, PREG_SPLIT_NO_EMPTY);
120+
121+
// preg_replace_callback_array: correct constant in $flags
122+
preg_replace_callback_array(['/foo/' => fn ($m) => $m[0]], 'subject', -1, $count, PREG_UNMATCHED_AS_NULL);

0 commit comments

Comments
 (0)