Skip to content
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

Enhancement: Implement UnwrapArrayReduce mutator #533

Merged

Conversation

localheinz
Copy link
Member

@localheinz localheinz commented Oct 31, 2018

This PR

Related to #514.

πŸ’β€β™‚οΈ Follows the example in #513.

sidz
sidz previously approved these changes Oct 31, 2018
Copy link
Member

@sidz sidz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘

BackEndTea
BackEndTea previously approved these changes Oct 31, 2018
Copy link
Member

@BackEndTea BackEndTea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘

@maks-rafalko
Copy link
Member

@sanmai as I remember, you suggested to unwrap the 3rd parameter, not the 1st.

array_reduce($array, $func, $initial) -> $initial

Some thoughts about it:

array_reduce can be used to reduce the whole array to one value (this is how it is defined in the spec: Iteratively reduce the array to a single value using a callback function)

It means that the result of the array_reduce can be not only array, but Object, boolean, number and any other type.

This, in its turn, means that unrapping the 1st argument instead of 3rd can lead to type mismatches, which will immediately lead to incorrectly killed mutant. Example:

- function getSum(array $numbers): int {
-     return array_reduce($numbers, function sum($carry, $item) {
-             $carry += $item;
-             return $carry;
-         },
-         0
-     );
- }

// incorrect mutation
+ function getSum(array $numbers): int {
+     return $numbers; // <---------- array instead of int
+ }

// correct mutation
+ function getSum(array $numbers): int {
+     return 0;
+ }

Probably we can think about all possible cases and produce mutation depending on context, existence of the 3rd parameter, etc.? WDYT?

Copy link
Member

@sanmai sanmai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really this has to be about the last argument, not about first as a whole at very least. Sorry about previous review!

@sanmai
Copy link
Member

sanmai commented Oct 31, 2018

I wonder where I was looking... True, unwrapping the first argument only makes hardly any sense, most of the time it’ll be a definite error.

On the other hand, there’s something about the first argument...

@localheinz
Copy link
Member Author

@borNfreee @sanmai

You are right, this makes a lot of sense!

@localheinz localheinz dismissed stale reviews from BackEndTea and sidz via 038d387 October 31, 2018 12:25
@@ -63,6 +63,10 @@
return false;
}

if (!array_key_exists($this->getParameterIndex(), $node->args)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of array_key_exists(), would you prefer me to use count()?

Copy link
Member

@sanmai sanmai Oct 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like array_key_exists() is most explicit for a generic case of this mutator.

count() is not such, adds extra limitations.

@sanmai
Copy link
Member

sanmai commented Oct 31, 2018

First argument opens possibility for an additional mutator:

- array_reduce($array, $func, $initial);
+ $array[0];

I guess this should be a purpose of a different PR.

Copy link
Member

@maks-rafalko maks-rafalko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘ πŸ‘

@maks-rafalko maks-rafalko merged commit aaa244c into infection:master Nov 2, 2018
@maks-rafalko
Copy link
Member

Thank you @localheinz

@maks-rafalko maks-rafalko added this to the 0.11.0 milestone Nov 2, 2018
@localheinz localheinz deleted the feature/unwrap-array-reduce branch November 2, 2018 06:53
@localheinz
Copy link
Member Author

Thank you, @BackEndTea, @borNfreee, @sanmai, and @sidz!

raphaelstolt pushed a commit to raphaelstolt/infection that referenced this pull request Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants