diff --git a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/CoalesceToTernaryRectorTest.php b/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/CoalesceToTernaryRectorTest.php deleted file mode 100644 index 794029d9f2d..00000000000 --- a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/CoalesceToTernaryRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/non_nullable_left.php.inc b/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/non_nullable_left.php.inc deleted file mode 100644 index 5c094d481ec..00000000000 --- a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/non_nullable_left.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/skip_array_dim_fetch.php.inc b/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/skip_array_dim_fetch.php.inc deleted file mode 100644 index 0eb5a0df2f0..00000000000 --- a/rules-tests/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector/Fixture/skip_array_dim_fetch.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -withRules([CoalesceToTernaryRector::class]); diff --git a/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php b/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php index c0b3e2acb68..33a00bdcf59 100644 --- a/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php +++ b/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php @@ -5,25 +5,19 @@ namespace Rector\CodeQuality\Rector\Coalesce; use PhpParser\Node; -use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\Variable; -use PHPStan\Type\ErrorType; -use PHPStan\Type\MixedType; -use PHPStan\Type\NullType; -use PHPStan\Type\UnionType; -use Rector\PHPStan\ScopeFetcher; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\CoalesceToTernaryRectorTest + * @deprecated This rule is deprecated, as risky. The "??" and "?:" operators are not interchangeable: "?:" also falls back on empty string, "0" and empty array. A regression must be fixed manually, so the rule is removed instead. * * @see https://github.com/rectorphp/rector/issues/9730 */ -final class CoalesceToTernaryRector extends AbstractRector +final class CoalesceToTernaryRector extends AbstractRector implements DeprecatedInterface { public function getRuleDefinition(): RuleDefinition { @@ -62,43 +56,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - /** - * indexed data maybe false positive - */ - if ($node->left instanceof ArrayDimFetch) { - return null; - } - - /** - * Scope needs to use parent Coalesce to properly get type from left side of coalesce - */ - $scope = ScopeFetcher::fetch($node); - $nativeType = $scope->getNativeType($node->left); - - if ($nativeType instanceof ErrorType) { - return null; - } - - if ($nativeType instanceof MixedType) { - return null; - } - - if ($nativeType instanceof NullType) { - return null; - } - - if ($nativeType instanceof UnionType) { - foreach ($nativeType->getTypes() as $unionedType) { - if ($unionedType instanceof NullType) { - return null; - } - } - } - - if ($node->left instanceof Variable && ! $scope->hasVariableType((string) $this->getName($node->left))->yes()) { - return null; - } - - return new Ternary($node->left, null, $node->right); + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as risky. The "??" and "?:" operators are not interchangeable and a regression has to be fixed manually', + self::class + )); } }