diff --git a/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php b/rules/Instanceof_/Rector/Ternary/FlipNegatedTernaryInstanceofRector.php index e2d0445e9af..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( - 'echo ! $object instanceof Product ? null : $object->getPrice();', - 'echo $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 ), ]); } 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,