Skip to content

Commit

Permalink
Change DecrementInteger mutator behaviour when -1 is mutated to -0 in…
Browse files Browse the repository at this point in the history
…stead of -2. Change IncrementInteger mutator behaviour when -1 is mutated to -2 (#1351)
  • Loading branch information
sidz committed Oct 19, 2020
1 parent 8109917 commit c0006f1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Mutator/Number/DecrementInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ public static function getDefinition(): ?Definition
*/
public function mutate(Node $node): iterable
{
yield new Node\Scalar\LNumber($node->value - 1);
$parentNode = ParentConnector::getParent($node);

$value = $node->value - 1;

if ($parentNode instanceof Node\Expr\UnaryMinus) {
$value = $node->value + 1;
}

yield new Node\Scalar\LNumber($value);
}

public function canMutate(Node $node): bool
Expand Down
11 changes: 10 additions & 1 deletion src/Mutator/Number/IncrementInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ParentConnector;
use PhpParser\Node;

/**
Expand All @@ -63,7 +64,15 @@ public static function getDefinition(): ?Definition
*/
public function mutate(Node $node): iterable
{
yield new Node\Scalar\LNumber($node->value + 1);
$parentNode = ParentConnector::getParent($node);

$value = $node->value + 1;

if ($parentNode instanceof Node\Expr\UnaryMinus) {
$value = $node->value - 1;
}

yield new Node\Scalar\LNumber($value);
}

public function canMutate(Node $node): bool
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/Mutator/Number/DecrementIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function mutationsProvider(): iterable
<<<'PHP'
<?php
if ($foo === -9) {
if ($foo === -11) {
echo 'bar';
}
PHP
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/Mutator/Number/IncrementIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function mutationsProvider(): iterable
PHP
];

yield 'It decrements a negative integer' => [
yield 'It increments a negative integer' => [
<<<'PHP'
<?php
Expand All @@ -122,7 +122,7 @@ public function mutationsProvider(): iterable
<<<'PHP'
<?php
if ($foo === -11) {
if ($foo === -9) {
echo 'bar';
}
PHP
Expand Down

0 comments on commit c0006f1

Please sign in to comment.