From ece1a963a04e5a3d7526f23648146163f2365e58 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Thu, 22 Aug 2019 12:18:12 +0200 Subject: [PATCH] Promote warnings to errors in str_word_count() --- ext/standard/string.c | 8 +-- .../tests/strings/str_word_count.phpt | 54 ++++++++++++------- .../tests/strings/str_word_count1.phpt | 30 +++++++---- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index c2f29baec7732..fd5c2a8882430 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -5916,13 +5916,13 @@ PHP_FUNCTION(str_shuffle) } /* }}} */ -/* {{{ proto mixed str_word_count(string str, [int format [, string charlist]]) + +/* {{{ proto array|int str_word_count(string str, [int format [, string charlist]]) Counts the number of words inside a string. If format of 1 is specified, then the function will return an array containing all the words found inside the string. If format of 2 is specified, then the function will return an associated array where the position of the word is the key and the word itself is the value. - For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters. @@ -5957,8 +5957,8 @@ PHP_FUNCTION(str_word_count) /* nothing to be done */ break; default: - php_error_docref(NULL, E_WARNING, "Invalid format value " ZEND_LONG_FMT, type); - RETURN_FALSE; + zend_throw_error(NULL, "Invalid format value " ZEND_LONG_FMT, type); + return; } if (char_list) { diff --git a/ext/standard/tests/strings/str_word_count.phpt b/ext/standard/tests/strings/str_word_count.phpt index e8fa12aca6d7c..375f26dee0340 100644 --- a/ext/standard/tests/strings/str_word_count.phpt +++ b/ext/standard/tests/strings/str_word_count.phpt @@ -2,17 +2,37 @@ str_word_count() --FILE-- getMessage() . "\n"; +} + +try { + var_dump(str_word_count($str, 123)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + var_dump(str_word_count($str, -1)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +try { + var_dump(str_word_count($str, 999999999)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + var_dump($str); $str2 = "F0o B4r 1s bar foo"; @@ -34,9 +54,10 @@ var_dump(str_word_count("'foo'", 2, "'")); var_dump(str_word_count("-foo-", 2)); var_dump(str_word_count("-foo-", 2, "-")); -echo "Done\n"; ?> ---EXPECTF-- + +DONE +--EXPECT-- array(6) { [0]=> string(5) "Hello" @@ -66,18 +87,10 @@ array(6) { string(5) "today" } int(6) - -Warning: str_word_count(): Invalid format value 3 in %s on line %d -bool(false) - -Warning: str_word_count(): Invalid format value 123 in %s on line %d -bool(false) - -Warning: str_word_count(): Invalid format value -1 in %s on line %d -bool(false) - -Warning: str_word_count(): Invalid format value 999999999 in %s on line %d -bool(false) +Invalid format value 3 +Invalid format value 123 +Invalid format value -1 +Invalid format value 999999999 string(55) "Hello friend, you're looking good today!" int(5) @@ -214,4 +227,5 @@ array(1) { [0]=> string(5) "-foo-" } -Done + +DONE diff --git a/ext/standard/tests/strings/str_word_count1.phpt b/ext/standard/tests/strings/str_word_count1.phpt index e942a17385886..18652699e232b 100644 --- a/ext/standard/tests/strings/str_word_count1.phpt +++ b/ext/standard/tests/strings/str_word_count1.phpt @@ -4,23 +4,31 @@ str_word_count() and invalid arguments getMessage() . "\n"; +} + +try { + var_dump(str_word_count("", -1, $a)); +} catch (\Error $e) { + echo $e->getMessage() . "\n"; +} + +var_dump($a); ?> + +DONE --EXPECTF-- int(0) - -Warning: str_word_count(): Invalid format value -1 in %s on line %d -bool(false) +Invalid format value -1 Notice: Undefined variable: a in %s on line %d - -Warning: str_word_count(): Invalid format value -1 in %s on line %d -bool(false) +Invalid format value -1 Notice: Undefined variable: a in %s on line %d NULL -Done + +DONE