Skip to content

Commit ce6b2ad

Browse files
committed
Improve error message for count()
1 parent 8df7018 commit ce6b2ad

13 files changed

+165
-166
lines changed

Zend/tests/generators/errors/count_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ try {
1515

1616
?>
1717
--EXPECTF--
18-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
18+
Warning: count(): Argument #1 ($var) must be of type Countable|array, Generator given in %s on line %d

Zend/zend_vm_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8915,7 +8915,7 @@ ZEND_VM_COLD_CONST_HANDLER(190, ZEND_COUNT, CONST|TMPVAR|CV, UNUSED)
89158915
} else {
89168916
count = 1;
89178917
}
8918-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
8918+
zend_error(E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", opline->extended_value ? "sizeof" : "count", zend_zval_type_name(op1));
89198919
break;
89208920
}
89218921

Zend/zend_vm_execute.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_GENERATOR_CREATE_SPEC_HANDLER(
18401840
static zend_never_inline ZEND_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL zend_cannot_pass_by_ref_helper_SPEC(uint32_t _arg_num, zval *_arg ZEND_OPCODE_HANDLER_ARGS_DC)
18411841
{
18421842
USE_OPLINE
1843-
zval *arg;
18441843
char *func_name = get_function_or_method_name(EX(call)->func);
18451844
const char *param_name = get_function_arg_name(EX(call)->func, _arg_num);
18461845

@@ -9692,7 +9691,7 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CONST_
96929691
} else {
96939692
count = 1;
96949693
}
9695-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
9694+
zend_error(E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", opline->extended_value ? "sizeof" : "count", zend_zval_type_name(op1));
96969695
break;
96979696
}
96989697

@@ -16932,7 +16931,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_TMPVAR_UNUSED_HANDL
1693216931
} else {
1693316932
count = 1;
1693416933
}
16935-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
16934+
zend_error(E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", opline->extended_value ? "sizeof" : "count", zend_zval_type_name(op1));
1693616935
break;
1693716936
}
1693816937

@@ -46688,7 +46687,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_COUNT_SPEC_CV_UNUSED_HANDLER(Z
4668846687
} else {
4668946688
count = 1;
4669046689
}
46691-
zend_error(E_WARNING, "%s(): Parameter must be an array or an object that implements Countable", opline->extended_value ? "sizeof" : "count");
46690+
zend_error(E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", opline->extended_value ? "sizeof" : "count", zend_zval_type_name(op1));
4669246691
break;
4669346692
}
4669446693

ext/standard/array.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ PHP_FUNCTION(count)
745745
switch (Z_TYPE_P(array)) {
746746
case IS_NULL:
747747
/* Intentionally not converted to an exception */
748-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
748+
php_error_docref(NULL, E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", get_active_function_name(), zend_zval_type_name(array));
749749
RETURN_LONG(0);
750750
break;
751751
case IS_ARRAY:
@@ -780,13 +780,13 @@ PHP_FUNCTION(count)
780780

781781
/* If There's no handler and it doesn't implement Countable then add a warning */
782782
/* Intentionally not converted to an exception */
783-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
783+
php_error_docref(NULL, E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", get_active_function_name(), zend_zval_type_name(array));
784784
RETURN_LONG(1);
785785
break;
786786
}
787787
default:
788788
/* Intentionally not converted to an exception */
789-
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
789+
php_error_docref(NULL, E_WARNING, "%s(): Argument #1 ($var) must be of type Countable|array, %s given", get_active_function_name(), zend_zval_type_name(array));
790790
RETURN_LONG(1);
791791
break;
792792
}

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ function krsort(array &$array, int $sort_flags = SORT_REGULAR): bool {}
6363

6464
function ksort(array &$array, int $sort_flags = SORT_REGULAR): bool {}
6565

66-
/** @param array|Countable|null $var */
66+
/** @param Countable|array $var */
6767
function count($var, int $mode = COUNT_NORMAL): int {}
6868

6969
/**
70-
* @param array|object|null $var
70+
* @param Countable|array $var
7171
* @alias count
7272
*/
7373
function sizeof($var, int $mode = COUNT_NORMAL): int {}

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 8b6ef365e9635c92ef86adb40b2aba077867f3b2 */
2+
* Stub hash: 6b08fb08dc5960d200e2b492f55e689d487a6eb5 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)

ext/standard/tests/array/count_invalid.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ var_dump($result);
2323

2424
?>
2525
--EXPECTF--
26-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
26+
Warning: count(): Argument #1 ($var) must be of type Countable|array, null given in %s on line %d
2727
int(0)
2828

29-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
29+
Warning: count(): Argument #1 ($var) must be of type Countable|array, string given in %s on line %d
3030
int(1)
3131

32-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
32+
Warning: count(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
3333
int(1)
3434

35-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
35+
Warning: count(): Argument #1 ($var) must be of type Countable|array, bool given in %s on line %d
3636
int(1)
3737

38-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
38+
Warning: count(): Argument #1 ($var) must be of type Countable|array, bool given in %s on line %d
3939
int(1)
4040

41-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
41+
Warning: count(): Argument #1 ($var) must be of type Countable|array, stdClass given in %s on line %d
4242
int(1)

ext/standard/tests/array/count_recursive.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ closedir( $resource2 );
113113
*** Testing basic functionality of count() function ***
114114
-- Testing NULL --
115115

116-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
116+
Warning: count(): count(): Argument #1 ($var) must be of type Countable|array, null given in %s on line %d
117117
COUNT_NORMAL: should be 0, is 0
118118

119-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
119+
Warning: count(): count(): Argument #1 ($var) must be of type Countable|array, null given in %s on line %d
120120
COUNT_RECURSIVE: should be 0, is 0
121121
-- Testing arrays --
122122
COUNT_NORMAL: should be 2, is 2
@@ -126,14 +126,14 @@ COUNT_NORMAL: should be 3, is 3
126126
COUNT_RECURSIVE: should be 6, is 6
127127
-- Testing strings --
128128

129-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
129+
Warning: count(): count(): Argument #1 ($var) must be of type Countable|array, string given in %s on line %d
130130
COUNT_NORMAL: should be 1, is 1
131131

132-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
132+
Warning: count(): count(): Argument #1 ($var) must be of type Countable|array, string given in %s on line %d
133133
COUNT_RECURSIVE: should be 1, is 1
134134
-- Testing various types with no second argument --
135135

136-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
136+
Warning: count(): Argument #1 ($var) must be of type Countable|array, string given in %s on line %d
137137
COUNT_NORMAL: should be 1, is 1
138138
COUNT_NORMAL: should be 2, is 2
139139
-- Testing really cool arrays --
@@ -175,18 +175,18 @@ COUNT_RECURSIVE is 7
175175

176176
-- Testing count() on constants with no second argument --
177177

178-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
178+
Warning: count(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
179179
COUNT_NORMAL: should be 1, is 1
180180

181-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
181+
Warning: count(): Argument #1 ($var) must be of type Countable|array, float given in %s on line %d
182182
COUNT_NORMAL: should be 1, is 1
183183

184184
-- Testing count() on NULL and Unset variables --
185185

186-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
186+
Warning: count(): Argument #1 ($var) must be of type Countable|array, null given in %s on line %d
187187
COUNT_NORMAL: should be 0, is 0
188188

189-
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
189+
Warning: count(): Argument #1 ($var) must be of type Countable|array, string given in %s on line %d
190190
COUNT_NORMAL: should be 1, is 1
191191
COUNT_NORMAL: should be 0, is 0
192192

ext/standard/tests/array/sizeof_basic1.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@ echo "Done";
3939
*** Testing sizeof() : basic functionality ***
4040
-- Testing sizeof() for integer type in default, COUNT_NORMAL and COUNT_RECURSIVE modes --
4141
default mode:
42-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
42+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
4343
int(1)
4444

4545
COUNT_NORMAL mode:
46-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
46+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
4747
int(1)
4848

4949
COUNT_RECURSIVE mode:
50-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
50+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
5151
int(1)
5252

5353
-- Testing sizeof() for float type in default, COUNT_NORMAL and COUNT_RECURSIVE modes --
5454
default mode:
55-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
55+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, float given in %s on line %d
5656
int(1)
5757

5858
COUNT_NORMAL mode:
59-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
59+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, float given in %s on line %d
6060
int(1)
6161

6262
COUNT_RECURSIVE mode:
63-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
63+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, float given in %s on line %d
6464
int(1)
6565
Done

ext/standard/tests/array/sizeof_object2.phpt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,67 +95,67 @@ echo "Done";
9595
--- Testing sizeof() with objects which doesn't implement Countable interface ---
9696
-- Iteration 1 --
9797
Default Mode:
98-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
98+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, test given in %s on line %d
9999
int(1)
100100

101101
COUNT_NORMAL Mode:
102-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
102+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test given in %s on line %d
103103
int(1)
104104

105105
COUNT_RECURSIVE Mode:
106-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
106+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test given in %s on line %d
107107
int(1)
108108

109109
-- Iteration 2 --
110110
Default Mode:
111-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
111+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, test1 given in %s on line %d
112112
int(1)
113113

114114
COUNT_NORMAL Mode:
115-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
115+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test1 given in %s on line %d
116116
int(1)
117117

118118
COUNT_RECURSIVE Mode:
119-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
119+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test1 given in %s on line %d
120120
int(1)
121121

122122
-- Iteration 3 --
123123
Default Mode:
124-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
124+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, test2 given in %s on line %d
125125
int(1)
126126

127127
COUNT_NORMAL Mode:
128-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
128+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test2 given in %s on line %d
129129
int(1)
130130

131131
COUNT_RECURSIVE Mode:
132-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
132+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, test2 given in %s on line %d
133133
int(1)
134134

135135
-- Iteration 4 --
136136
Default Mode:
137-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
137+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, child_test2 given in %s on line %d
138138
int(1)
139139

140140
COUNT_NORMAL Mode:
141-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
141+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, child_test2 given in %s on line %d
142142
int(1)
143143

144144
COUNT_RECURSIVE Mode:
145-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
145+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, child_test2 given in %s on line %d
146146
int(1)
147147

148148
-- Iteration 5 --
149149
Default Mode:
150-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
150+
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, concrete_class given in %s on line %d
151151
int(1)
152152

153153
COUNT_NORMAL Mode:
154-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
154+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, concrete_class given in %s on line %d
155155
int(1)
156156

157157
COUNT_RECURSIVE Mode:
158-
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
158+
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, concrete_class given in %s on line %d
159159
int(1)
160160

161161
Done

0 commit comments

Comments
 (0)