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
Add Mutators: SpreadAssignment, SpreadRemoval #1529
Conversation
Assert::allNotNull($node->items); | ||
|
||
return $node->items[0]->unpack; |
There was a problem hiding this comment.
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
@maks-rafalko hey 👋, can you please run the workflows on this? Also, it should be ready for review |
There was a problem hiding this 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]; |
There was a problem hiding this comment.
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:
$x = [...$collection, 4, 5];
- https://psalm.dev/r/4b159a69f1 - no issues$x = [$collection, 4, 5];
- https://psalm.dev/r/9e9b78e2e1 Psalm error -11:12 - Argument 1 of acceptsInt expects int, array{1, 2, 3} provided
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, makes sense ;)
There was a problem hiding this 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
There was a problem hiding this 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.
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 @sanmai thank you both for reviewing and merging this so quickly! 🙌 |
Very nice stuff ! :) |
final class SpreadAssignmentTest extends BaseMutatorTestCase | ||
{ | ||
/** | ||
* @requires PHP >= 7.4 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
This PR:
...
)Spread
Mutator toSpreadOneItem
Closes: #1528
I manually tested with the
.phar
the behaviour provided as an example in the issue.