Skip to content

Commit f885924

Browse files
committed
Promote warning to type error in count()
1 parent 5b8e12a commit f885924

15 files changed

+450
-618
lines changed

Zend/tests/generators/errors/count_error.phpt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ try {
1111
count($gen);
1212
} catch (Exception $e) {
1313
echo $e;
14-
}
14+
} catch (\TypeError $e) {
15+
echo $e->getMessage() . "\n";
16+
}
1517

1618
?>
17-
--EXPECTF--
18-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
19+
--EXPECT--
20+
Parameter must be an array or an object that implements Countable

Zend/zend_vm_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8616,7 +8616,7 @@ ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED)
86168616
} else {
86178617
count = 1;
86188618
}
8619-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
8619+
zend_type_error("Parameter must be an array or an object that implements Countable");
86208620
break;
86218621
}
86228622

Zend/zend_vm_execute.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9558,7 +9558,7 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CONST_
95589558
} else {
95599559
count = 1;
95609560
}
9561-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
9561+
zend_type_error("Parameter must be an array or an object that implements Countable");
95629562
break;
95639563
}
95649564

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

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

ext/ffi/tests/008.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ foreach ($a as $key => $val) {
1616

1717
$a = FFI::new("struct {int x,y;}");
1818
try {
19-
var_dump(@count($a));
19+
var_dump(count($a));
2020
} catch (Throwable $e) {
2121
echo get_class($e) . ": " . $e->getMessage()."\n";
2222
}
@@ -34,5 +34,5 @@ int(3)
3434
0 => 0
3535
1 => 10
3636
2 => 20
37-
FFI\Exception: Attempt to count() on non C array
37+
TypeError: Parameter must be an array or an object that implements Countable
3838
FFI\Exception: Attempt to iterate on non C array

ext/standard/array.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */
735735

736736
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
737737
if (GC_IS_RECURSIVE(ht)) {
738-
php_error_docref(NULL, E_WARNING, "recursion detected");
738+
zend_throw_error(NULL, "Recursion detected");
739739
return 0;
740740
}
741741
GC_PROTECT_RECURSION(ht);
@@ -773,7 +773,7 @@ PHP_FUNCTION(count)
773773

774774
switch (Z_TYPE_P(array)) {
775775
case IS_NULL:
776-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
776+
zend_type_error("Parameter must be an array or an object that implements Countable");
777777
RETURN_LONG(0);
778778
break;
779779
case IS_ARRAY:
@@ -804,12 +804,12 @@ PHP_FUNCTION(count)
804804
}
805805

806806
/* If There's no handler and it doesn't implement Countable then add a warning */
807-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
807+
zend_type_error("Parameter must be an array or an object that implements Countable");
808808
RETURN_LONG(1);
809809
break;
810810
}
811811
default:
812-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
812+
zend_type_error("Parameter must be an array or an object that implements Countable");
813813
RETURN_LONG(1);
814814
break;
815815
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
Test count() function error conditions
3+
--FILE--
4+
<?php
5+
/* Prototype: int count ( mixed $var [, int $mode] );
6+
Description: Count elements in an array, or properties in an object
7+
*/
8+
9+
try {
10+
count(NULL, COUNT_NORMAL);
11+
} catch (\TypeError $e) {
12+
echo $e->getMessage() . "\n";
13+
}
14+
try {
15+
count(NULL, COUNT_RECURSIVE);
16+
} catch (\TypeError $e) {
17+
echo $e->getMessage() . "\n";
18+
}
19+
try {
20+
count(NULL);
21+
} catch (\TypeError $e) {
22+
echo $e->getMessage() . "\n";
23+
}
24+
25+
try {
26+
count("string", COUNT_NORMAL);
27+
} catch (\TypeError $e) {
28+
echo $e->getMessage() . "\n";
29+
}
30+
try {
31+
count("string", COUNT_RECURSIVE);
32+
} catch (\TypeError $e) {
33+
echo $e->getMessage() . "\n";
34+
}
35+
try {
36+
count("string");
37+
} catch (\TypeError $e) {
38+
echo $e->getMessage() . "\n";
39+
}
40+
try {
41+
count("");
42+
} catch (\TypeError $e) {
43+
echo $e->getMessage() . "\n";
44+
}
45+
46+
try {
47+
count(100);
48+
} catch (\TypeError $e) {
49+
echo $e->getMessage() . "\n";
50+
}
51+
try {
52+
count(-23.45);
53+
} catch (\TypeError $e) {
54+
echo $e->getMessage() . "\n";
55+
}
56+
57+
try {
58+
@count($a);
59+
} catch (\TypeError $e) {
60+
echo $e->getMessage() . "\n";
61+
}
62+
63+
?>
64+
--EXPECT--
65+
Parameter must be an array or an object that implements Countable
66+
Parameter must be an array or an object that implements Countable
67+
Parameter must be an array or an object that implements Countable
68+
Parameter must be an array or an object that implements Countable
69+
Parameter must be an array or an object that implements Countable
70+
Parameter must be an array or an object that implements Countable
71+
Parameter must be an array or an object that implements Countable
72+
Parameter must be an array or an object that implements Countable
73+
Parameter must be an array or an object that implements Countable
74+
Parameter must be an array or an object that implements Countable

ext/standard/tests/array/count_invalid.phpt

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,51 @@ Only arrays and countable objects can be counted
33
--FILE--
44
<?php
55

6-
$result = count(null);
7-
var_dump($result);
8-
9-
$result = count("string");
10-
var_dump($result);
11-
12-
$result = count(123);
13-
var_dump($result);
14-
15-
$result = count(true);
16-
var_dump($result);
17-
18-
$result = count(false);
19-
var_dump($result);
20-
21-
$result = count((object) []);
22-
var_dump($result);
6+
try {
7+
$result = count(null);
8+
var_dump($result);
9+
} catch (\TypeError $e) {
10+
echo $e->getMessage() . "\n";
11+
}
12+
13+
try {
14+
$result = count("string");
15+
var_dump($result);
16+
} catch (\TypeError $e) {
17+
echo $e->getMessage() . "\n";
18+
}
19+
20+
try {
21+
$result = count(123);
22+
var_dump($result);
23+
} catch (\TypeError $e) {
24+
echo $e->getMessage() . "\n";
25+
}
26+
27+
try {
28+
$result = count(true);
29+
var_dump($result);
30+
} catch (\TypeError $e) {
31+
echo $e->getMessage() . "\n";
32+
}
33+
try {
34+
$result = count(false);
35+
var_dump($result);
36+
} catch (\TypeError $e) {
37+
echo $e->getMessage() . "\n";
38+
}
39+
try {
40+
$result = count((object) []);
41+
var_dump($result);
42+
} catch (\TypeError $e) {
43+
echo $e->getMessage() . "\n";
44+
}
2345

2446
?>
25-
--EXPECTF--
26-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
27-
int(0)
28-
29-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
30-
int(1)
31-
32-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
33-
int(1)
34-
35-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
36-
int(1)
37-
38-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
39-
int(1)
40-
41-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
42-
int(1)
47+
--EXPECT--
48+
Parameter must be an array or an object that implements Countable
49+
Parameter must be an array or an object that implements Countable
50+
Parameter must be an array or an object that implements Countable
51+
Parameter must be an array or an object that implements Countable
52+
Parameter must be an array or an object that implements Countable
53+
Parameter must be an array or an object that implements Countable

0 commit comments

Comments
 (0)