From da809660a9a2d98231f8f57b337fc3facf803b13 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:51:59 +0700 Subject: [PATCH] [CodeQuality] Handle on assign on FlipTypeControlToUseExclusiveTypeRector (#6653) * [CodeQuality] Handle on assign on FlipTypeControlToUseExclusiveTypeRector * fix * skip on assign * clean * fix * fix * fix --- .../Fixture/on_assign.php.inc | 35 +++++++++++++++++++ ...lipTypeControlToUseExclusiveTypeRector.php | 6 ++++ .../Rector/If_/RemoveDeadInstanceOfRector.php | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc diff --git a/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc b/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc new file mode 100644 index 0000000000..53d236a7d6 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php b/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php index de30df8726..a4e74fb041 100644 --- a/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php +++ b/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php @@ -6,12 +6,14 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name\FullyQualified; use PHPStan\Type\ObjectType; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType; @@ -91,6 +93,10 @@ private function processConvertToExclusiveType( ? $objectType->getFullyQualifiedName() : $objectType->getClassName(); + if ($expr instanceof Assign) { + $expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); + } + $instanceof = new Instanceof_($expr, new FullyQualified($fullyQualifiedType)); if ($binaryOp instanceof NotIdentical) { return $instanceof; diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index 588c03e946..c58e99ac8c 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -22,6 +22,7 @@ use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; use Rector\NodeManipulator\IfManipulator; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -110,6 +111,7 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu } if ($instanceof->expr instanceof Assign) { + $instanceof->expr->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, false); $assignExpression = new Expression($instanceof->expr); return array_merge([$assignExpression], $if->stmts); }