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

Change DecrementInteger and IncrementInteger mutators behaviour #1351

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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