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

InstanceOf_ mutator #1232

Merged
merged 9 commits into from Apr 1, 2020
Merged

InstanceOf_ mutator #1232

merged 9 commits into from Apr 1, 2020

Conversation

sanmai
Copy link
Member

@sanmai sanmai commented Mar 30, 2020

Quite simple one. Replaces:

$foo instanceof Anything

With either true or false.

  • When true we assume the value always the of same type, and the check is redundant.
  • When false we assume the testing isn't thorough.

Report for this mutator:

338 mutations were generated:
     316 mutants were killed
       0 mutants were not covered by tests
      22 covered mutants were not detected
       0 errors were encountered
       0 time outs were encountered

@maks-rafalko
Copy link
Member

A couple of questions:

  1. How a developer should kill it? Or is this mutator should show the redundant check (like public -> protected indicating that $a instance of $b is not needed? Can we add such info to description of the mutator?

  2. I would suggest instead of producing 2 mutations (true, false) do the following

    -$a instance of $b
    +! $a instance of $b

    Why? Because producing 2 mutations (true, false) duplicates original value (being it true of false). With the suggested above mutation, we will have only one new mutation - the opposite to the original. What do you think?

@sanmai
Copy link
Member Author

sanmai commented Mar 30, 2020

How a developer should kill it?

By ensuring this check is actually required by using appropriately typed arguments, or by removing the check altogether. I think we have to come up with a style of explanation before writing any of them since stating the obvious might not be as useful.

I we'd go with the negation approach you suggest, we'll have this:

169 mutations were generated:
     169 mutants were killed

Not to say it's bad, but not as nearly as lucrative as in the current implementation.

Consider this escaping mutation in ReflectionVisitor.php:

--- Original
+++ New
@@ @@
         if ($node instanceof Node\Stmt\ClassMethod) {
             return true;
         }
-        if ($node instanceof Node\Expr\Closure) {
+        if (false) {
             return true;
         }
         return false;

Are we really testing this method receives Node\Expr\Closure on input? How can this escape? I just verified it is indeed the case the test does not notice this change.

And why do test notice if we negate this comparison?..

@sanmai sanmai requested a review from theofidry March 30, 2020 07:37
yield new Node\Expr\ConstFetch(new Node\Name('false'));
}

public function canMutate(Node $node): bool
Copy link
Member

Choose a reason for hiding this comment

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

I think canMutate() should be placed before mutate(), but since it's hardly something we can enforce don't mind me if you don't agree with that

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, I took another mutator for a template, and that's how it was there.

@@ -43,7 +43,7 @@ public function leaveNode(Node $node)

// It is important to not rely on the keys here. It might otherwise result in some elements
// being overridden, see https://3v4l.org/JLN73
foreach($this->mutator->mutate($node) as $mutatedNode) {
foreach ($this->mutator->mutate($node) as $mutatedNode) {
Copy link
Member

Choose a reason for hiding this comment

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

I thought the CS fixer would pick that up...

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't fix fixtures for some reason I didn't yet have time to investigate.

sanmai added a commit to infection/site that referenced this pull request Mar 31, 2020
@maks-rafalko maks-rafalko added this to the 0.17.0 milestone Mar 31, 2020
@sanmai sanmai merged commit 4b46248 into infection:master Apr 1, 2020
@sanmai sanmai deleted the pr/2020-03/InstanceOf_ branch April 1, 2020 08:58
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