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

Decrement mutator makes array indexes negative #1270

Closed
maks-rafalko opened this issue Jun 12, 2020 · 4 comments · Fixed by #1281
Closed

Decrement mutator makes array indexes negative #1270

maks-rafalko opened this issue Jun 12, 2020 · 4 comments · Fixed by #1281

Comments

@maks-rafalko
Copy link
Member

Question Answer
Infection version 0.16
Test Framework version any
PHP version x.y.z (php -v)

This should not happen

10) /SourceClass.php:14    [M] DecrementInteger

--- Original
+++ New
@@ @@
         $c = [1, 2, 3];
-        $d = $c[0];
+        $d = $c[-1];
@sanmai
Copy link
Member

sanmai commented Jun 12, 2020

Negative indexes in an array are not forbidden. You can totally have an array with a negative index.

@maks-rafalko
Copy link
Member Author

maks-rafalko commented Jun 12, 2020

right, but from my experience with this mutator, each time it decrements 0 to -1 as an index of the array, it leads to undefined index notice

Notice: Undefined offset: -1

which in its turn always leads to killed Mutant.

So, I think this is a useless mutation in almost 100% cases. I'm speaking only about 0 to -1, not about -99 to -100.

In other words, I propose to not do such mutation if:

  • the number is an index of the array
  • the number is 0

@LoopKarma
Copy link
Contributor

thank you for labelling issues - I will try to pick up this bug +)
@maks-rafalko can you please explain why mutation should not be applied if the number is an index of the array?

@maks-rafalko
Copy link
Member Author

maks-rafalko commented Jul 7, 2020

sure, as I said:

from my experience with this mutator, each time it decrements 0 to -1 as an index of the array, it leads to undefined index notice

Notice: Undefined offset: -1

So, there are many cases when I have similar code to this:

$items = $object->getItems();

Assert::greaterThan(0, $items); // here we know that there is at least 1 item

$firstItem = $items[0];

So, when we have a list of items (array indexed from 0 to N) - there is no point in mutating 0 to -1, because it will always lead to

Notice: Undefined offset: -1

In other words, rephrasing my previous comment, we should not do a mutations from 0 to -1 when: the number is an index of the array AND the number is 0

@maks-rafalko maks-rafalko added this to the 0.17.0 milestone Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants