Skip to content

Promote count() warning to TypeError #4572

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: 5 additions & 3 deletions Zend/tests/generators/errors/count_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ try {
count($gen);
} catch (Exception $e) {
echo $e;
}
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
--EXPECT--
Parameter must be an array or an object that implements Countable
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8616,7 +8616,7 @@ ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED)
} else {
count = 1;
}
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
zend_type_error("Parameter must be an array or an object that implements Countable");
break;
}

Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -9558,7 +9558,7 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CONST_
} else {
count = 1;
}
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
zend_type_error("Parameter must be an array or an object that implements Countable");
break;
}

Expand Down Expand Up @@ -16687,7 +16687,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_TMPVAR_UNUSED_HANDL
} else {
count = 1;
}
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
zend_type_error("Parameter must be an array or an object that implements Countable");
break;
}

Expand Down Expand Up @@ -47323,7 +47323,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CV_UNUSED_HANDLER(Z
} else {
count = 1;
}
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
zend_type_error("Parameter must be an array or an object that implements Countable");
break;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/ffi/tests/008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ foreach ($a as $key => $val) {

$a = FFI::new("struct {int x,y;}");
try {
var_dump(@count($a));
var_dump(count($a));
} catch (Throwable $e) {
echo get_class($e) . ": " . $e->getMessage()."\n";
}
Expand All @@ -34,5 +34,5 @@ int(3)
0 => 0
1 => 10
2 => 20
FFI\Exception: Attempt to count() on non C array
TypeError: Parameter must be an array or an object that implements Countable
Copy link
Member

Choose a reason for hiding this comment

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

Hm, why does this change? That seems odd.

Copy link
Member

Choose a reason for hiding this comment

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

Should be fixed by d266ba4.

FFI\Exception: Attempt to iterate on non C array
8 changes: 4 additions & 4 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */

if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(ht)) {
php_error_docref(NULL, E_WARNING, "recursion detected");
zend_throw_error(NULL, "Recursion detected");
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't change this. Just skip and return null then.

Copy link
Member Author

Choose a reason for hiding this comment

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

ACK

return 0;
}
GC_PROTECT_RECURSION(ht);
Expand Down Expand Up @@ -773,7 +773,7 @@ PHP_FUNCTION(count)

switch (Z_TYPE_P(array)) {
case IS_NULL:
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
zend_type_error("Parameter must be an array or an object that implements Countable");
RETURN_LONG(0);
break;
case IS_ARRAY:
Expand Down Expand Up @@ -804,12 +804,12 @@ PHP_FUNCTION(count)
}

/* If There's no handler and it doesn't implement Countable then add a warning */
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
zend_type_error("Parameter must be an array or an object that implements Countable");
RETURN_LONG(1);
break;
}
default:
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
zend_type_error("Parameter must be an array or an object that implements Countable");
RETURN_LONG(1);
break;
}
Expand Down
74 changes: 74 additions & 0 deletions ext/standard/tests/array/count_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--TEST--
Test count() function error conditions
--FILE--
<?php
/* Prototype: int count ( mixed $var [, int $mode] );
Description: Count elements in an array, or properties in an object
*/

try {
count(NULL, COUNT_NORMAL);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count(NULL, COUNT_RECURSIVE);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count(NULL);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
count("string", COUNT_NORMAL);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count("string", COUNT_RECURSIVE);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count("string");
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count("");
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
count(100);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
count(-23.45);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
@count($a);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECT--
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
81 changes: 46 additions & 35 deletions ext/standard/tests/array/count_invalid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,51 @@ Only arrays and countable objects can be counted
--FILE--
<?php

$result = count(null);
var_dump($result);

$result = count("string");
var_dump($result);

$result = count(123);
var_dump($result);

$result = count(true);
var_dump($result);

$result = count(false);
var_dump($result);

$result = count((object) []);
var_dump($result);
try {
$result = count(null);
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
$result = count("string");
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
$result = count(123);
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

try {
$result = count(true);
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
$result = count(false);
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}
try {
$result = count((object) []);
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage() . "\n";
}

?>
--EXPECTF--
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(0)

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(1)

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(1)

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(1)

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(1)

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(1)
--EXPECT--
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Parameter must be an array or an object that implements Countable
Loading