From d15804724fc2b2fa32293889dceca4455b0f8708 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 26 Aug 2026 21:33:39 +0100 Subject: [PATCH 1/2] [CodeQuality] Add FlipNegatedTernaryInstanceofRector to code quality level --- .../Rector/Ternary/FlipNegatedTernaryInstanceofRector.php | 4 ++-- src/Config/Level/CodeQualityLevel.php | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php b/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php index e2d0445e9af..a437b254bf8 100644 --- a/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php +++ b/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php @@ -22,8 +22,8 @@ public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Flip negated ternary of `instanceof` to direct use of object', [ new CodeSample( - 'echo ! $object instanceof Product ? null : $object->getPrice();', - 'echo $object instanceof Product ? $object->getPrice() : null;' + '$price = ! $object instanceof Product ? null : $object->getPrice();', + '$price = $object instanceof Product ? $object->getPrice() : null;' ), ]); } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 8e4e7e880a5..2d4f4465524 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -76,6 +76,7 @@ use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector; use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector; use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector; +use Rector\Instanceof_\Rector\Ternary\FlipNegatedTernaryInstanceofRector; use Rector\Php52\Rector\Property\VarToPublicPropertyRector; use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector; use Rector\Renaming\Rector\FuncCall\RenameFunctionRector; @@ -163,6 +164,7 @@ final class CodeQualityLevel PreparedValueToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class, InlineIsAInstanceOfRector::class, + FlipNegatedTernaryInstanceofRector::class, InlineConstructorDefaultToPropertyRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, From 9242bb15896c766586890e1e9b0ff541b275fbf6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 26 Aug 2026 21:38:44 +0100 Subject: [PATCH 2/2] [CodeQuality] Verbose code sample with class and method context --- .../FlipNegatedTernaryInstanceofRector.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php b/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php index a437b254bf8..a5c60471f12 100644 --- a/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php +++ b/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php @@ -22,8 +22,25 @@ public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Flip negated ternary of `instanceof` to direct use of object', [ new CodeSample( - '$price = ! $object instanceof Product ? null : $object->getPrice();', - '$price = $object instanceof Product ? $object->getPrice() : null;' + <<<'CODE_SAMPLE' +class SomeClass +{ + public function resolvePrice(object $object): ?int + { + return ! $object instanceof Product ? null : $object->getPrice(); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class SomeClass +{ + public function resolvePrice(object $object): ?int + { + return $object instanceof Product ? $object->getPrice() : null; + } +} +CODE_SAMPLE ), ]); }