Skip to content

Commit

Permalink
Improve error message for count()
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Sep 4, 2020
1 parent 1824961 commit 32038d6
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Zend/tests/generators/errors/count_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ try {

?>
--EXPECTF--
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
Warning: count(): Argument #1 ($var) must be of type Countable|array, Generator given in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8927,7 +8927,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_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));
break;
}

Expand Down
7 changes: 3 additions & 4 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_GENERATOR_CREATE_SPEC_HANDLER(
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)
{
USE_OPLINE
zval *arg;
char *func_name = get_function_or_method_name(EX(call)->func);
const char *param_name = get_function_arg_name(EX(call)->func, _arg_num);

Expand Down Expand Up @@ -10576,7 +10575,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_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));
break;
}

Expand Down Expand Up @@ -17889,7 +17888,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_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));
break;
}

Expand Down Expand Up @@ -48254,7 +48253,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_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));
break;
}

Expand Down
6 changes: 3 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ PHP_FUNCTION(count)
switch (Z_TYPE_P(array)) {
case IS_NULL:
/* Intentionally not converted to an exception */
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
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));
RETURN_LONG(0);
break;
case IS_ARRAY:
Expand Down Expand Up @@ -780,13 +780,13 @@ PHP_FUNCTION(count)

/* If There's no handler and it doesn't implement Countable then add a warning */
/* Intentionally not converted to an exception */
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
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));
RETURN_LONG(1);
break;
}
default:
/* Intentionally not converted to an exception */
php_error_docref(NULL, E_WARNING, "Parameter must be an array or an object that implements Countable");
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));
RETURN_LONG(1);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ function krsort(array &$array, int $sort_flags = SORT_REGULAR): bool {}

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

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

/**
* @param array|object|null $var
* @param Countable|array $var
* @alias count
*/
function sizeof($var, int $mode = COUNT_NORMAL): int {}
Expand Down
12 changes: 6 additions & 6 deletions ext/standard/tests/array/count_invalid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ var_dump($result);

?>
--EXPECTF--
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
Warning: count(): Argument #1 ($var) must be of type Countable|array, null given 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
Warning: count(): Argument #1 ($var) must be of type Countable|array, string given 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
Warning: count(): Argument #1 ($var) must be of type Countable|array, int given 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
Warning: count(): Argument #1 ($var) must be of type Countable|array, bool given 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
Warning: count(): Argument #1 ($var) must be of type Countable|array, bool given 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
Warning: count(): Argument #1 ($var) must be of type Countable|array, stdClass given in %s on line %d
int(1)
18 changes: 9 additions & 9 deletions ext/standard/tests/array/count_recursive.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ closedir( $resource2 );
*** Testing basic functionality of count() function ***
-- Testing NULL --

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

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

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

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

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

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

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

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

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions ext/standard/tests/array/sizeof_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ echo "Done";
*** Testing sizeof() : basic functionality ***
-- Testing sizeof() for integer type in default, COUNT_NORMAL and COUNT_RECURSIVE modes --
default mode:
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, int given in %s on line %d
int(1)

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

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

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

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

COUNT_RECURSIVE mode:
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
Warning: sizeof(): sizeof(): Argument #1 ($var) must be of type Countable|array, float given in %s on line %d
int(1)
Done
30 changes: 15 additions & 15 deletions ext/standard/tests/array/sizeof_object2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,67 @@ echo "Done";
--- Testing sizeof() with objects which doesn't implement Countable interface ---
-- Iteration 1 --
Default Mode:
Warning: sizeof(): Parameter must be an array or an object that implements Countable in %s on line %d
Warning: sizeof(): Argument #1 ($var) must be of type Countable|array, test given in %s on line %d
int(1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Done
Loading

0 comments on commit 32038d6

Please sign in to comment.