From cd7445414b59141eede6fd7f795493d8c06fdfea Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:25:55 +0700 Subject: [PATCH 1/7] [CodeQuality] Handle on assign on FlipTypeControlToUseExclusiveTypeRector --- .../Fixture/on_assign.php.inc | 35 +++++++++++++++++++ ...lipTypeControlToUseExclusiveTypeRector.php | 6 ++++ 2 files changed, 41 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; From 8a428e05ed9f7e11e5c65611e38352cc63798731 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:32:42 +0700 Subject: [PATCH 2/7] fix --- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index 588c03e946..c235b8b6d9 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->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, false); $assignExpression = new Expression($instanceof->expr); return array_merge([$assignExpression], $if->stmts); } From 81f6b45d07052250f6bcb62c4020acdd4f4f21a1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:39:07 +0700 Subject: [PATCH 3/7] skip on assign --- .../Fixture/on_assign.php.inc | 35 ------------------- .../Fixture/skip_on_assign.php.inc | 15 ++++++++ ...lipTypeControlToUseExclusiveTypeRector.php | 11 +++--- .../Rector/If_/RemoveDeadInstanceOfRector.php | 1 - 4 files changed, 20 insertions(+), 42 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_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 deleted file mode 100644 index 53d236a7d6..0000000000 --- a/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc +++ /dev/null @@ -1,35 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc b/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc new file mode 100644 index 0000000000..f4b1b1b7ef --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc @@ -0,0 +1,15 @@ +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 c235b8b6d9..1b82ba0b98 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -111,7 +111,6 @@ private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof): nu } if ($instanceof->expr instanceof Assign) { - $instanceof->expr->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, false); $assignExpression = new Expression($instanceof->expr); return array_merge([$assignExpression], $if->stmts); } From 9de51c40f1b9a7c1be47a6dacf9c4d1376dee1e0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:39:54 +0700 Subject: [PATCH 4/7] clean --- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index 1b82ba0b98..588c03e946 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -22,7 +22,6 @@ 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; From 616bfedc8f308e2b35e72d8d0412cffea211f2b9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:44:37 +0700 Subject: [PATCH 5/7] fix --- .../Fixture/on_assign.php.inc | 35 +++++++++++++++++++ .../Fixture/skip_on_assign.php.inc | 15 -------- ...lipTypeControlToUseExclusiveTypeRector.php | 7 +--- 3 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/on_assign.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_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-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc b/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc deleted file mode 100644 index f4b1b1b7ef..0000000000 --- a/rules-tests/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector/Fixture/skip_on_assign.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -getFullyQualifiedName() : $objectType->getClassName(); From 5e9d010c4b2f0209faa2aae55a676bee913a6b9f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:47:47 +0700 Subject: [PATCH 6/7] fix --- .../Identical/FlipTypeControlToUseExclusiveTypeRector.php | 6 ++++++ rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php | 3 +++ 2 files changed, 9 insertions(+) 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..a681d6cc08 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\Rector\If_; +use Attribute; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; @@ -22,6 +23,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 +112,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); } From c4022bf61804bd6c1d38263fa7e0e35641e8d5e5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 6 Jan 2025 20:49:23 +0700 Subject: [PATCH 7/7] fix --- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php index a681d6cc08..c58e99ac8c 100644 --- a/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php +++ b/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php @@ -4,7 +4,6 @@ namespace Rector\DeadCode\Rector\If_; -use Attribute; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign;