Skip to content

Commit 105edec

Browse files
committed
Add warning when trying to perform binop on unsupported types
1 parent 835237e commit 105edec

6 files changed

+56
-14
lines changed

ext/standard/array.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5935,7 +5935,8 @@ PHP_FUNCTION(array_sum)
59355935
ZVAL_LONG(&tmp, 0);
59365936
add_function(return_value, return_value, &tmp);
59375937
}
5938-
// TODO Warning/Deprecation?
5938+
php_error_docref(NULL, E_WARNING, "Addition is not supported on type %s",
5939+
zend_zval_type_name(entry));
59395940
}
59405941
} ZEND_HASH_FOREACH_END();
59415942

@@ -5992,7 +5993,8 @@ PHP_FUNCTION(array_product)
59925993
ZVAL_LONG(&tmp, 0);
59935994
mul_function(return_value, return_value, &tmp);
59945995
}
5995-
// TODO Warning/Deprecation?
5996+
php_error_docref(NULL, E_WARNING, "Multiplication is not supported on type %s",
5997+
zend_zval_type_name(entry));
59965998
}
59975999
} ZEND_HASH_FOREACH_END();
59986000

ext/standard/tests/array/003.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ require(__DIR__ . '/data.inc');
88

99
function cmp ($a, $b) {
1010
is_array ($a)
11-
and $a = array_sum ($a);
11+
and $a = count($a);
1212
is_array ($b)
13-
and $b = array_sum ($b);
13+
and $b = count($b);
1414
return strcmp ($a, $b);
1515
}
1616

17-
echo " -- Testing uasort() -- \n";
17+
echo "-- Testing uasort() --\n";
1818
uasort ($data, 'cmp');
1919
var_dump ($data);
2020

2121

22-
echo "\n -- Testing uksort() -- \n";
22+
echo "\n-- Testing uksort() --\n";
2323
uksort ($data, 'cmp');
2424
var_dump ($data);
2525

26-
echo "\n -- Testing usort() -- \n";
26+
echo "\n-- Testing usort() --\n";
2727
usort ($data, 'cmp');
2828
var_dump ($data);
2929
?>
3030
--EXPECT--
31-
-- Testing uasort() --
31+
-- Testing uasort() --
3232
array(8) {
3333
[16777216]=>
3434
float(-0.3333333333333333)
@@ -53,7 +53,7 @@ array(8) {
5353
string(4) "test"
5454
}
5555

56-
-- Testing uksort() --
56+
-- Testing uksort() --
5757
array(8) {
5858
[-1000]=>
5959
array(2) {
@@ -78,7 +78,7 @@ array(8) {
7878
int(27)
7979
}
8080

81-
-- Testing usort() --
81+
-- Testing usort() --
8282
array(8) {
8383
[0]=>
8484
float(-0.3333333333333333)

ext/standard/tests/array/array_product_variation1.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ foreach ($types as $desc => $type) {
2020
}
2121

2222
?>
23-
--EXPECT--
23+
--EXPECTF--
2424
*** Testing array_product() : variation - using non numeric values ***
2525
boolean (true)
2626
int(1)
@@ -29,20 +29,28 @@ boolean (false)
2929
int(0)
3030

3131
string
32+
33+
Warning: array_product(): Multiplication is not supported on type string in %s on line %d
3234
int(0)
3335

3436
numeric string
3537
int(12)
3638

3739
resource
40+
41+
Warning: array_product(): Multiplication is not supported on type resource in %s on line %d
3842
int(3)
3943

4044
object
45+
46+
Warning: array_product(): Multiplication is not supported on type A in %s on line %d
4147
int(1)
4248

4349
null
4450
int(0)
4551

4652
array
53+
54+
Warning: array_product(): Multiplication is not supported on type array in %s on line %d
4755
int(1)
4856

ext/standard/tests/array/array_product_variation5.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Test array_product() function: ressources in array
55
$input = [10, STDERR /* Should get casted to 3 as an integer */];
66
var_dump(array_product($input));
77
?>
8-
--EXPECT--
8+
--EXPECTF--
9+
Warning: array_product(): Multiplication is not supported on type resource in %s on line %d
910
int(30)

ext/standard/tests/array/array_sum_variation7.phpt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,48 @@ echo "-- array with mixed values --\n";
5757
var_dump( array_sum($input) );
5858
echo "Done"
5959
?>
60-
--EXPECT--
60+
--EXPECTF--
6161
*** Testing array_sum() : array with unexpected entries ***
6262
-- array with string values --
63+
64+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
65+
66+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
67+
68+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
69+
70+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
71+
72+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
6373
int(0)
6474
-- array with bool values --
6575
int(3)
6676
-- array with null values --
6777
int(0)
6878
-- array with subarrays --
79+
80+
Warning: array_sum(): Addition is not supported on type array in %s on line %d
81+
82+
Warning: array_sum(): Addition is not supported on type array in %s on line %d
83+
84+
Warning: array_sum(): Addition is not supported on type array in %s on line %d
6985
int(0)
7086
-- array with object values --
87+
88+
Warning: array_sum(): Addition is not supported on type MyClass in %s on line %d
89+
90+
Warning: array_sum(): Addition is not supported on type MyClass in %s on line %d
91+
92+
Warning: array_sum(): Addition is not supported on type MyClass in %s on line %d
93+
94+
Warning: array_sum(): Addition is not supported on type MyClass in %s on line %d
7195
int(0)
7296
-- array with mixed values --
97+
98+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
99+
100+
Warning: array_sum(): Addition is not supported on type string in %s on line %d
101+
102+
Warning: array_sum(): Addition is not supported on type array in %s on line %d
73103
float(14)
74104
Done

ext/standard/tests/array/array_sum_variation8.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Test array_sum() function: ressources in array
55
$input = [10, STDERR /* Should get casted to 3 as an integer */];
66
var_dump(array_sum($input));
77
?>
8-
--EXPECT--
8+
--EXPECTF--
9+
Warning: array_sum(): Addition is not supported on type resource in %s on line %d
910
int(13)

0 commit comments

Comments
 (0)