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

Add Mutators: SpreadAssignment, SpreadRemoval #1529

Merged
merged 3 commits into from Jul 4, 2021

Conversation

AlexandruGG
Copy link
Contributor

@AlexandruGG AlexandruGG commented Jul 3, 2021

This PR:

Closes: #1528

I manually tested with the .phar the behaviour provided as an example in the issue.

Comment on lines +103 to +105
Assert::allNotNull($node->items);

return $node->items[0]->unpack;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the assertion is more for PHPStan because from what I understand and see in other examples the items will be ArrayItem, but the type here is ArrayItem|null. Let me know if I'm missing something, it's the first time I'm working with the php parser

@AlexandruGG AlexandruGG changed the title Feature/spread mutators Add Mutators: SpreadAssignment, SpreadRemoval Jul 3, 2021
@AlexandruGG AlexandruGG marked this pull request as ready for review July 3, 2021 20:18
@AlexandruGG
Copy link
Contributor Author

@maks-rafalko hey 👋, can you please run the workflows on this? Also, it should be ready for review

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.

Thank you very much for this contribution.

I do like the SpreadAssignment mutator, however I have some concerns about SpreadRemoval, please see detailed comment below.

null,
<<<'DIFF'
- $x = [...$collection, 4, 5];
+ $x = [$collection, 4, 5];
Copy link
Member

Choose a reason for hiding this comment

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

This particular mutator will be killed by static analyzers, which makes it useless.

Please, see the following Psalm examples:

in other words, if we are using for example https://github.com/Roave/infection-static-analysis-plugin - this mutant will always be killed, resulting in wasted resources.

So I would vote to not do this mutation. Does it make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interesting point, I'm familiar with that plugin, however, in your example we are making an assumption: we assume that the array is a list of a single type, which might not be the case. There are many situations where an array will have mixed types and this mutator becomes valuable.

Here's the simplest example, based on yours: https://psalm.dev/r/6ba915ee93. I've added the assertions at the end to show that the first element of each new array is not the same.

There will be other cases where the union type isn't even mentioned because an array can have many different types, so the developer might go with mixed.

Copy link
Member

Choose a reason for hiding this comment

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

ok, makes sense ;)

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.

Overall - looks good to me.

Let's see, if nobody else has any objections, I'm happy to merge

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.

I'm not the biggest fan of SpreadRemoval since it seems like not the most productive mutator, but we have several more of this kind, and yet sometimes they can be of use.

@maks-rafalko
Copy link
Member

Ok, let's merge then.

Thank you @AlexandruGG


I'm starting thinking about somehow gathering user's statistics about mutators (not just killing/escaping ratio, but usefulness from developers points of view during working on real projects), as described in this great article #1526

@maks-rafalko maks-rafalko merged commit 9497615 into infection:master Jul 4, 2021
@AlexandruGG
Copy link
Contributor Author

@maks-rafalko @sanmai thank you both for reviewing and merging this so quickly! 🙌

@AlexandruGG AlexandruGG deleted the feature/spread-mutators branch July 4, 2021 09:32
@drupol
Copy link
Contributor

drupol commented Jul 6, 2021

Very nice stuff ! :)

final class SpreadAssignmentTest extends BaseMutatorTestCase
{
/**
* @requires PHP >= 7.4
Copy link
Member

Choose a reason for hiding this comment

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

I'm late to the party.

@requires is not needed as minimum compatible with Infection version of PHP is 7.4

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, should probably be removed from other places as well then. I might create a PR specifically for those

Copy link
Member

Choose a reason for hiding this comment

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

I might create a PR specifically for those

yes please. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using the splat operator (...) to copy an array
5 participants