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 Flip Coalesce operator mutator #1389

Merged
merged 2 commits into from Oct 30, 2020
Merged

Conversation

sidz
Copy link
Member

@sidz sidz commented Oct 29, 2020

This PR:

Mutates:

$foo = 'foo';
$bar = 'bar';
- $foo ?? $bar;
+ $bar ?? $foo;
$foo = 'foo';
$bar = 'bar';
$baz = 'baz';
- $foo ?? $bar ?? $baz;
+ $foo ?? $baz ?? $bar;
$foo = 'foo';
$bar = 'bar';
$baz = 'baz';
- $foo ?? $bar ?? $baz;
+ $bar ?? $foo ?? $baz;

Requested in #1387

p.s. From my POV Infection\Mutator\Operator\Coalesce mutator is redundant when this PR is merged. Thoughts?

sidz added a commit to infection/site that referenced this pull request Oct 29, 2020
@sidz sidz added the Mutator label Oct 29, 2020
@maks-rafalko maks-rafalko added this to the 0.20.0 milestone Oct 29, 2020
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
Copy link
Member

p.s. From my POV Infection\Mutator\Operator\Coalesce mutator is redundant when this PR is merged. Thoughts?

I didn't do any analytics, but from the first look - agree, sounds like redundant

@sanmai
Copy link
Member

sanmai commented Oct 29, 2020

Coalesce is redundant indeed.

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.

How about self::A ?? self::B? And with constants in general. These might be unkillable.

@maks-rafalko
Copy link
Member

How about self::A ?? self::B?

is it a real case? I mean it's strange to use constant in if statement. It's either always true or always false

@sanmai
Copy link
Member

sanmai commented Oct 30, 2020

It can be any kind of a constant expression.

define('TEST_ENVIRONMENT', null);
class B {
    const ALLOW_FEATURE_A = TEST_ENVIRONMENT;
}

No doubt it's a poor programming style, but shall we punish it?

@maks-rafalko
Copy link
Member

maks-rafalko commented Oct 30, 2020

Probably I don't understand the real case and that's why don't see an issue here. Any real examples? I just can't remember any cases with such code self::A ?? self::B, even with self::A ?? $variable

@sanmai
Copy link
Member

sanmai commented Oct 30, 2020

The issue is that you might not be able to test self::A ?? self::B reliably. This could be a convenience method in a generated class. Or this could be a method to turn off a pending feature based on a hardcoded constant.

In either case I think this method either not worth the testing, or difficult to test. This makes a useless mutation, a waste of time.

@maks-rafalko
Copy link
Member

maks-rafalko commented Oct 30, 2020

So what are you suggesting? Do not mutate when the left side is a const?

Alternatively, if the user has such a weird case, they can ignore such line of the code, via ignoreSourceCodeByRegex:

{
    "mutators": {
        "global-ignoreSourceCodeByRegex": ["self::.*\\s\\?\\?\\sself::.*"]
    }
}

@sanmai
Copy link
Member

sanmai commented Oct 30, 2020

Yes, I think it's a general enough case to ignore all mutations with a constant fetch on the left by default.

It is much cheaper for us to do that now, than for a user to invent complicated (as we can readily see!) regular expression just to avoid a pointless mutation.

@maks-rafalko
Copy link
Member

TBH I'm not convinced, as I have never seen such code. But if you did - then ok.

@sanmai
Copy link
Member

sanmai commented Oct 30, 2020

Let me put this way: in which circumstances do you think running this mutation against a constant on a left will be useful?

Again, it is not about this mutation is harmful, it's not, but rather it is not useful. Even annoyingly not useful.

One can argue this variant of this mutation isn't useful for most projects just like IdenticalEqual and NotIdenticalNotEqual not useful for the most modern-day projects.

@sanmai
Copy link
Member

sanmai commented Oct 30, 2020

If I learnt something from the IdenticalEqual/NotIdenticalNotEqual debacle is that if a mutation might be seen as a punishment for a certain programming style, you shouldn't do it. Any such mutations make Infection less useful, and that's bad.

@sidz
Copy link
Member Author

sidz commented Oct 30, 2020

@sanmai done

@sidz sidz requested a review from sanmai October 30, 2020 09:00
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