From 693d2acaef460abc6ee192cae13409cd618cc01b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 11 Mar 2026 11:35:34 +0100 Subject: [PATCH] Implement NodeScopeResolverTestPHPVersionMutator --- resources/infection.json5 | 3 +- ...NodeScopeResolverTestPHPVersionMutator.php | 81 +++++++++++++++++++ ...ScopeResolverTestPHPVersionMutatorTest.php | 74 +++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/Infection/NodeScopeResolverTestPHPVersionMutator.php create mode 100644 tests/Infection/NodeScopeResolverTestPHPVersionMutatorTest.php diff --git a/resources/infection.json5 b/resources/infection.json5 index 04ed3f9..c2dea4a 100644 --- a/resources/infection.json5 +++ b/resources/infection.json5 @@ -15,7 +15,8 @@ "PHPStan\\Infection\\IsSuperTypeOfCalleeAndArgumentMutator": true, "PHPStan\\Infection\\LooseBooleanMutator": true, "PHPStan\\Infection\\TrinaryLogicMutator": true, - "PHPStan\\Infection\\TrueTruthyFalseFalseyTypeSpecifierContextMutator": true + "PHPStan\\Infection\\TrueTruthyFalseFalseyTypeSpecifierContextMutator": true, + "PHPStan\\Infection\\NodeScopeResolverTestPHPVersionMutator": true }, "bootstrap": "build-infection/vendor/autoload.php" } diff --git a/src/Infection/NodeScopeResolverTestPHPVersionMutator.php b/src/Infection/NodeScopeResolverTestPHPVersionMutator.php new file mode 100644 index 0000000..c02becf --- /dev/null +++ b/src/Infection/NodeScopeResolverTestPHPVersionMutator.php @@ -0,0 +1,81 @@ + + */ +final class NodeScopeResolverTestPHPVersionMutator implements Mutator +{ + + public static function getDefinition(): Definition + { + return new Definition( + <<<'TXT' + Replaces "// lint >= 8.0" conditions in test-files with previous versions. + TXT + , + MutatorCategory::ORTHOGONAL_REPLACEMENT, + null, + <<<'DIFF' + - // lint >= 8.0 + + // lint >= 7.4 + DIFF, + ); + } + + public function getName(): string + { + return self::class; + } + + public function canMutate(Node $node): bool + { + if ($node instanceof Node\Stmt\Nop) { + $x=1; + } + + return true; + } + + public function mutate(Node $node): iterable + { + if ($node instanceof Node\Expr\BooleanNot) { + $node = $node->expr; + if (!$node instanceof Node\Expr\MethodCall) { + throw new LogicException(); + } + + if (!$node->name instanceof Node\Identifier) { + throw new LogicException(); + } + + if ($node->name->name === 'yes') { + yield new Node\Expr\MethodCall($node->var, 'no'); + } else { + yield new Node\Expr\MethodCall($node->var, 'yes'); + } + + return; + } + + if (!$node->name instanceof Node\Identifier) { + throw new LogicException(); + } + + if ($node->name->name === 'yes') { + yield new Node\Expr\BooleanNot(new Node\Expr\MethodCall($node->var, 'no')); + } else { + yield new Node\Expr\BooleanNot(new Node\Expr\MethodCall($node->var, 'yes')); + } + } + +} diff --git a/tests/Infection/NodeScopeResolverTestPHPVersionMutatorTest.php b/tests/Infection/NodeScopeResolverTestPHPVersionMutatorTest.php new file mode 100644 index 0000000..bd2dace --- /dev/null +++ b/tests/Infection/NodeScopeResolverTestPHPVersionMutatorTest.php @@ -0,0 +1,74 @@ +assertMutatesInput($input, $expected); + } + + /** + * @return iterable + */ + public static function mutationsProvider(): iterable + { + yield 'It mutates lint-comment to php7' => [ + <<<'PHP' + = 8.0 + + $x = 1; + PHP +, + <<<'PHP' + = 7.4 + + $x = 1; + PHP +, + ]; + /* + + yield 'It mutates lint-comment to previous minor version' => [ + <<<'PHP' + = 8.4 + + $x = 1; + PHP +, + <<<'PHP' + = 8.3 + + $x = 1; + PHP +, + ]; + + yield 'No mutations when no lint comment' => [ + <<<'PHP' +