From 048b893960e3c4c8f31e907ec3af550726b52c11 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 25 Dec 2024 00:30:44 +0700 Subject: [PATCH] [TypeDeclaration] Skip assign on while cond on WhileNullableToInstanceofRector (#6628) * [TypeDeclaration] Skip assign on while cond on WhileNullableToInstanceofRector * fix * [ci-review] Rector Rectify * fix --------- Co-authored-by: GitHub Action --- .../Fixture/skip_assign.php.inc | 20 +++++++++++++++++++ .../Source/CheckedClass.php | 4 +++- .../WhileNullableToInstanceofRector.php | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Fixture/skip_assign.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Fixture/skip_assign.php.inc b/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Fixture/skip_assign.php.inc new file mode 100644 index 00000000000..89df6b085c7 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Fixture/skip_assign.php.inc @@ -0,0 +1,20 @@ +get()) { + $someClass->run(); + } + } + + private function get(): ?CheckedClass + { + return rand(0, 1) ? new CheckedClass() : null; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Source/CheckedClass.php b/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Source/CheckedClass.php index 2c1dd683fa0..afe3f2542f0 100644 --- a/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Source/CheckedClass.php +++ b/rules-tests/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector/Source/CheckedClass.php @@ -4,5 +4,7 @@ class CheckedClass { - + public function run() + { + } } diff --git a/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php b/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php index 5da213e3023..0d7a4c69363 100644 --- a/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php +++ b/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\NotIdentical; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name\FullyQualified; @@ -74,6 +75,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if ($node->cond instanceof Assign) { + return null; + } + if ($node->cond instanceof NotIdentical) { return $this->refactorNotIdentical($node, $node->cond); }