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 UnwrapArrayReplace mutator #536

Merged

Conversation

localheinz
Copy link
Member

@localheinz localheinz commented Oct 31, 2018

This PR

Related to #514.

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

maks-rafalko
maks-rafalko previously approved these changes Oct 31, 2018
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.

LGTM

@localheinz
Copy link
Member Author

@borNfreee

I could imagine that this mutator could create as many mutations as arguments are passed to array_replace(), what do you think?

@maks-rafalko
Copy link
Member

maks-rafalko commented Oct 31, 2018

Do you mean this

- $d = array_replace(['a'], ['b'], ['c']);
+ $d = ['a'];
+ $d = ['b'];
+ $d = ['c'];

?

I didn't do any analyzing but this sounds good to me.

@localheinz
Copy link
Member Author

@borNfreee

Exactly!

@sanmai
Copy link
Member

sanmai commented Oct 31, 2018

So there go mutators for array_merge(), array_diff(), array_replace_recursive(),array_intersect(), array_intersect_assoc(), array_uintersect()... Many of them. I had these on my mind for some time, but implementing them now is like putting #494 back even further. And this important PR already needs some work to cover these new mutators.

But probably it is not a bad idea to update this mutator to use the current API now, rather than wait for #494. For inspiration see #500.

In a nutshell for this to work AbstractUnwrapMutator needs to be updated as such:

@@ -50,12 +50,16 @@ abstract class AbstractUnwrapMutator extends Mutator
      */
     final public function mutate(Node $node)
     {
-        return $node->args[$this->getParameterIndex()];
+        foreach ($this->getParameterIndex() as $index) {
+            if (array_key_exists($index, $node->args)) {
+                yield $node->args[$index];
+            }
+        }
     }
 
     abstract protected function getFunctionName(): string;
 
-    abstract protected function getParameterIndex(): int;
+    abstract protected function getParameterIndex(): \Generator;
 
     final protected function mutatesNode(Node $node): bool

And concrete mutators:

-    protected function getParameterIndex(): int
+    protected function getParameterIndex(): \Generator
     {
-        return 0;
+        yield 0;
     }

Seems doable, what do you think?

@localheinz
Copy link
Member Author

@sanmai

Seems doable, what do you think?

Absolutely! For now I implemented it for the UnwrapArrayReplace mutator specifically, but I don't mind adjusting.

@localheinz
Copy link
Member Author

@borNfreee @sanmai

What do you think about aaa1f75?

maks-rafalko
maks-rafalko previously approved these changes Nov 2, 2018
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.

Awesome!

@maks-rafalko
Copy link
Member

We should probably merge this one (after resolving conflict) and then rebase #534 to use yield

sanmai
sanmai previously approved these changes Nov 2, 2018
@sanmai
Copy link
Member

sanmai commented Nov 2, 2018

There's something PHPStan did not like.

@maks-rafalko
Copy link
Member

Thank you @localheinz

@maks-rafalko maks-rafalko merged commit 0ba9520 into infection:master Nov 3, 2018
@maks-rafalko maks-rafalko added this to the 0.11.0 milestone Nov 3, 2018
@localheinz localheinz deleted the feature/unwrap-array-replace branch November 3, 2018 23:10
@localheinz
Copy link
Member Author

Thank you, @borNfreee and @sanmai!

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

3 participants