-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Saner array_(sum|product)() #10161
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
Merged
Merged
Saner array_(sum|product)() #10161
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e793c9a
Add object and resource tests to array_(sum|product)()
Girgias 4a17d74
Start aligning array_(sum|product)() behaviour with binary ops
Girgias 31d963d
Deprecate passing empty array to array_(sum|product)()
Girgias 621d24f
Handle object that define a do_operation handler but no cast handler
Girgias 39975f5
Add warning when trying to perform binop on unsupported types
Girgias fafd690
Merge common array_(sum|product) implementation
Girgias f03c428
Add array_reduce comparison to array_(sum|product) tests
Girgias 1474cb2
Revert deprecation for empty arrays
Girgias 2f90654
Nit: Use English spelling for resource
Girgias 433b941
Nit: use else if instead of second if
Girgias 8a18672
Nit: indentation
Girgias 648bb68
Add FFI example to tests
Girgias e4883e9
Cast objects to numeric
Girgias 53b3e83
Remove fast_add_function() function
Girgias 6f2c1f6
Add UPGRADING.INTERNALS entry back that got yanked by the rebase
Girgias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Test array_product() function with empty array | ||
--FILE-- | ||
<?php | ||
$input = []; | ||
|
||
echo "array_product() version:\n"; | ||
var_dump(array_product($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry * $value, 1)); | ||
?> | ||
--EXPECT-- | ||
array_product() version: | ||
int(1) | ||
array_reduce() version: | ||
int(1) |
28 changes: 28 additions & 0 deletions
28
ext/standard/tests/array/array_product_objects_operation_no_cast.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Test array_product() function with objects that implement addition but not castable to numeric type | ||
--EXTENSIONS-- | ||
zend_test | ||
--FILE-- | ||
<?php | ||
$input = [new DoOperationNoCast(25), new DoOperationNoCast(6), new DoOperationNoCast(10)]; | ||
|
||
echo "array_product() version:\n"; | ||
var_dump(array_product($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry * $value, 1)); | ||
?> | ||
--EXPECTF-- | ||
array_product() version: | ||
|
||
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d | ||
|
||
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d | ||
|
||
Warning: array_product(): Multiplication is not supported on type DoOperationNoCast in %s on line %d | ||
int(1) | ||
array_reduce() version: | ||
object(DoOperationNoCast)#5 (1) { | ||
["val":"DoOperationNoCast":private]=> | ||
int(1500) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Test array_product() function: resources in array | ||
--FILE-- | ||
<?php | ||
$input = [10, STDERR /* Should get casted to 3 as an integer */]; | ||
|
||
echo "array_product() version:\n"; | ||
var_dump(array_product($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
try { | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry * $value, 1)); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(); | ||
} | ||
?> | ||
--EXPECTF-- | ||
array_product() version: | ||
|
||
Warning: array_product(): Multiplication is not supported on type resource in %s on line %d | ||
int(30) | ||
array_reduce() version: | ||
Unsupported operand types: int * resource |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
Test array_product() function with objects castable to numeric type | ||
--EXTENSIONS-- | ||
gmp | ||
--FILE-- | ||
<?php | ||
$input = [gmp_init(25), gmp_init(6)]; | ||
|
||
echo "array_product() version:\n"; | ||
var_dump(array_product($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry * $value, 1)); | ||
?> | ||
--EXPECT-- | ||
array_product() version: | ||
int(150) | ||
array_reduce() version: | ||
object(GMP)#5 (1) { | ||
["num"]=> | ||
string(3) "150" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Test array_sum() function with empty array | ||
--FILE-- | ||
<?php | ||
$input = []; | ||
|
||
echo "array_sum() version:\n"; | ||
var_dump(array_sum($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry + $value, 0)); | ||
?> | ||
--EXPECT-- | ||
array_sum() version: | ||
int(0) | ||
array_reduce() version: | ||
int(0) |
26 changes: 26 additions & 0 deletions
26
ext/standard/tests/array/array_sum_objects_operation_no_cast.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Test array_sum() function with objects that implement addition but not castable to numeric type | ||
--EXTENSIONS-- | ||
zend_test | ||
--FILE-- | ||
<?php | ||
$input = [new DoOperationNoCast(25), new DoOperationNoCast(6)]; | ||
|
||
echo "array_sum() version:\n"; | ||
var_dump(array_sum($input)); | ||
|
||
echo "array_reduce() version:\n"; | ||
var_dump(array_reduce($input, fn($carry, $value) => $carry + $value, 0)); | ||
?> | ||
--EXPECTF-- | ||
array_sum() version: | ||
|
||
Warning: array_sum(): Addition is not supported on type DoOperationNoCast in %s on line %d | ||
|
||
Warning: array_sum(): Addition is not supported on type DoOperationNoCast in %s on line %d | ||
int(0) | ||
array_reduce() version: | ||
object(DoOperationNoCast)#5 (1) { | ||
["val":"DoOperationNoCast":private]=> | ||
int(31) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.