Skip to content

Commit 228bae7

Browse files
committed
Use TypeError for preg_replace type check
This is a type violation warning, and as such should use TypeError in PHP 8.
1 parent 4067519 commit 228bae7

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

ext/pcre/php_pcre.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,8 +2239,8 @@ static void preg_replace_common(INTERNAL_FUNCTION_PARAMETERS, int is_filter)
22392239
}
22402240
} else {
22412241
if (Z_TYPE_P(regex) != IS_ARRAY) {
2242-
php_error_docref(NULL, E_WARNING, "Parameter mismatch, pattern is a string while replacement is an array");
2243-
RETURN_FALSE;
2242+
zend_type_error("Parameter mismatch, pattern is a string while replacement is an array");
2243+
return;
22442244
}
22452245
}
22462246

ext/pcre/php_pcre.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ function preg_match_all(string $pattern, string $subject, &$subpatterns = null,
1010
* @param string|array $regex
1111
* @param string|array $replace
1212
* @param string|array $subject
13-
* @return string|array|null|false
13+
* @return string|array|null
1414
*/
1515
function preg_replace($regex, $replace, $subject, int $limit = -1, &$count = null) {}
1616

1717
/**
1818
* @param string|array $regex
1919
* @param string|array $replace
2020
* @param string|array $subject
21-
* @return string|array|null|false
21+
* @return string|array|null
2222
*/
2323
function preg_filter($regex, $replace, $subject, int $limit = -1, &$count = null) {}
2424

ext/pcre/tests/bug21732.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
--TEST--
22
Bug #21732 (preg_replace() segfaults with invalid parameters)
3-
--INI--
4-
error_reporting=0
53
--FILE--
64
<?php
75
class foo {
@@ -11,11 +9,15 @@ class foo {
119
}
1210
}
1311

14-
var_dump(preg_replace('', array(), ''));
12+
try {
13+
var_dump(preg_replace('', array(), ''));
14+
} catch (TypeError $e) {
15+
echo $e->getMessage(), "\n";
16+
}
1517
var_dump(preg_replace_callback("/(ab)(cd)(e)/", array(new foo(), "cb"), 'abcde'));
1618
?>
1719
--EXPECT--
18-
bool(false)
20+
Parameter mismatch, pattern is a string while replacement is an array
1921
array(4) {
2022
[0]=>
2123
string(5) "abcde"

ext/pcre/tests/preg_replace2.phpt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ if (@preg_match('/./u', '') === false) {
99
--FILE--
1010
<?php
1111

12-
var_dump(preg_replace('', array(), ''));
13-
1412
var_dump(preg_replace(array('/\da(.)/ui', '@..@'), '$1', '12Abc'));
1513
var_dump(preg_replace(array('/\da(.)/ui', '@(.)@'), '$1', array('x','a2aA', '1av2Ab')));
1614

@@ -21,9 +19,7 @@ var_dump(preg_replace(array('/\s+/', '~[b-d]~'), array('$'), array('x y', 'bd bc
2119
echo "==done==\n";
2220

2321
?>
24-
--EXPECTF--
25-
Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in %spreg_replace2.php on line 3
26-
bool(false)
22+
--EXPECT--
2723
string(1) "c"
2824
array(3) {
2925
[0]=>

ext/pcre/tests/preg_replace_error2.phpt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ $replace = array('this is a string', array('this is', 'a subarray'),);
1616
$subject = 'test';
1717
foreach($replace as $value) {
1818
print "\nArg value is: $value\n";
19-
var_dump(preg_replace($regex, $value, $subject));
19+
try {
20+
var_dump(preg_replace($regex, $value, $subject));
21+
} catch (TypeError $e) {
22+
echo $e->getMessage(), "\n";
23+
}
2024
}
2125
$value = new stdclass(); //Object
2226
try {
@@ -33,8 +37,6 @@ Arg value is: this is a string
3337
string(64) "this is a stringthis is a stringthis is a stringthis is a string"
3438

3539
Arg value is: Array
36-
37-
Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array in %spreg_replace_error2.php on line %d
38-
bool(false)
40+
Parameter mismatch, pattern is a string while replacement is an array
3941
Object of class stdClass could not be converted to string
4042
Done

0 commit comments

Comments
 (0)