Skip to content

Commit fdff6cf

Browse files
committed
Promote warnings to errors in str_word_count()
1 parent 754d9f3 commit fdff6cf

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

ext/standard/string.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5916,13 +5916,13 @@ PHP_FUNCTION(str_shuffle)
59165916
}
59175917
/* }}} */
59185918

5919-
/* {{{ proto mixed str_word_count(string str, [int format [, string charlist]])
5919+
5920+
/* {{{ proto array|int str_word_count(string str, [int format [, string charlist]])
59205921
Counts the number of words inside a string. If format of 1 is specified,
59215922
then the function will return an array containing all the words
59225923
found inside the string. If format of 2 is specified, then the function
59235924
will return an associated array where the position of the word is the key
59245925
and the word itself is the value.
5925-
59265926
For the purpose of this function, 'word' is defined as a locale dependent
59275927
string containing alphabetic characters, which also may contain, but not start
59285928
with "'" and "-" characters.
@@ -5957,8 +5957,8 @@ PHP_FUNCTION(str_word_count)
59575957
/* nothing to be done */
59585958
break;
59595959
default:
5960-
php_error_docref(NULL, E_WARNING, "Invalid format value " ZEND_LONG_FMT, type);
5961-
RETURN_FALSE;
5960+
zend_throw_error(NULL, "Invalid format value " ZEND_LONG_FMT, type);
5961+
return;
59625962
}
59635963

59645964
if (char_list) {

ext/standard/tests/strings/str_word_count.phpt

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
str_word_count()
33
--FILE--
44
<?php
5-
error_reporting(E_ALL);
65
$str = "Hello friend, you're
76
looking good today!";
87
$b =& $str;
98
var_dump(str_word_count($str, 1));
109
var_dump(str_word_count($str, 2));
1110
var_dump(str_word_count($str));
12-
var_dump(str_word_count($str, 3));
13-
var_dump(str_word_count($str, 123));
14-
var_dump(str_word_count($str, -1));
15-
var_dump(str_word_count($str, 999999999));
11+
12+
try {
13+
var_dump(str_word_count($str, 3));
14+
} catch (\Error $e) {
15+
echo $e->getMessage() . "\n";
16+
}
17+
18+
try {
19+
var_dump(str_word_count($str, 123));
20+
} catch (\Error $e) {
21+
echo $e->getMessage() . "\n";
22+
}
23+
24+
try {
25+
var_dump(str_word_count($str, -1));
26+
} catch (\Error $e) {
27+
echo $e->getMessage() . "\n";
28+
}
29+
30+
try {
31+
var_dump(str_word_count($str, 999999999));
32+
} catch (\Error $e) {
33+
echo $e->getMessage() . "\n";
34+
}
35+
1636
var_dump($str);
1737

1838
$str2 = "F0o B4r 1s bar foo";
@@ -34,9 +54,10 @@ var_dump(str_word_count("'foo'", 2, "'"));
3454
var_dump(str_word_count("-foo-", 2));
3555
var_dump(str_word_count("-foo-", 2, "-"));
3656

37-
echo "Done\n";
3857
?>
39-
--EXPECTF--
58+
59+
DONE
60+
--EXPECT--
4061
array(6) {
4162
[0]=>
4263
string(5) "Hello"
@@ -66,18 +87,10 @@ array(6) {
6687
string(5) "today"
6788
}
6889
int(6)
69-
70-
Warning: str_word_count(): Invalid format value 3 in %s on line %d
71-
bool(false)
72-
73-
Warning: str_word_count(): Invalid format value 123 in %s on line %d
74-
bool(false)
75-
76-
Warning: str_word_count(): Invalid format value -1 in %s on line %d
77-
bool(false)
78-
79-
Warning: str_word_count(): Invalid format value 999999999 in %s on line %d
80-
bool(false)
90+
Invalid format value 3
91+
Invalid format value 123
92+
Invalid format value -1
93+
Invalid format value 999999999
8194
string(55) "Hello friend, you're
8295
looking good today!"
8396
int(5)
@@ -214,4 +227,5 @@ array(1) {
214227
[0]=>
215228
string(5) "-foo-"
216229
}
217-
Done
230+
231+
DONE

ext/standard/tests/strings/str_word_count1.phpt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@ str_word_count() and invalid arguments
44
<?php
55

66
var_dump(str_word_count(""));
7-
var_dump(str_word_count("", -1));
8-
var_dump(str_word_count("", -1, $a));
9-
var_dump($a);
107

11-
echo "Done\n";
8+
try {
9+
var_dump(str_word_count("", -1));
10+
} catch (\Error $e) {
11+
echo $e->getMessage() . "\n";
12+
}
13+
14+
try {
15+
var_dump(str_word_count("", -1, $a));
16+
} catch (\Error $e) {
17+
echo $e->getMessage() . "\n";
18+
}
19+
20+
var_dump($a);
1221
?>
22+
23+
DONE
1324
--EXPECTF--
1425
int(0)
15-
16-
Warning: str_word_count(): Invalid format value -1 in %s on line %d
17-
bool(false)
26+
Invalid format value -1
1827

1928
Notice: Undefined variable: a in %s on line %d
20-
21-
Warning: str_word_count(): Invalid format value -1 in %s on line %d
22-
bool(false)
29+
Invalid format value -1
2330

2431
Notice: Undefined variable: a in %s on line %d
2532
NULL
26-
Done
33+
34+
DONE

0 commit comments

Comments
 (0)