Skip to content

Promote warnings to errors in str_word_count() #4604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
54 changes: 34 additions & 20 deletions ext/standard/tests/strings/str_word_count.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@
str_word_count()
--FILE--
<?php
error_reporting(E_ALL);
$str = "Hello friend, you're
looking good today!";
$b =& $str;
var_dump(str_word_count($str, 1));
var_dump(str_word_count($str, 2));
var_dump(str_word_count($str));
var_dump(str_word_count($str, 3));
var_dump(str_word_count($str, 123));
var_dump(str_word_count($str, -1));
var_dump(str_word_count($str, 999999999));

try {
var_dump(str_word_count($str, 3));
} catch (\Error $e) {
echo $e->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";
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: Why are you making this change everywhere? It looks unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen a bunch of test do that in other extensions and else where so I just decided to mimic it for "consistency", I can stop doing that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I've ever seen this. Possibly you saw ===DONE===?

In any case, unless the test output would be empty without it, I don't think there's any point to adding a final "done" in any form.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible yes, Okay will know this for next time :)

--EXPECT--
array(6) {
[0]=>
string(5) "Hello"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -214,4 +227,5 @@ array(1) {
[0]=>
string(5) "-foo-"
}
Done

DONE
30 changes: 19 additions & 11 deletions ext/standard/tests/strings/str_word_count1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ str_word_count() and invalid arguments
<?php

var_dump(str_word_count(""));
var_dump(str_word_count("", -1));
var_dump(str_word_count("", -1, $a));
var_dump($a);

echo "Done\n";
try {
var_dump(str_word_count("", -1));
} catch (\Error $e) {
echo $e->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