Skip to content

Commit

Permalink
[TypeDeclaration] Skip assign on while cond on WhileNullableToInstanc…
Browse files Browse the repository at this point in the history
…eofRector (#6628)

* [TypeDeclaration] Skip assign on while cond on WhileNullableToInstanceofRector

* fix

* [ci-review] Rector Rectify

* fix

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Dec 24, 2024
1 parent 2291593 commit 048b893
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\While_\WhileNullableToInstanceofRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\While_\WhileNullableToInstanceofRector\Source\CheckedClass;

class SkipAssign
{
function run(?CheckedClass $someClass)
{
while ($someClass = $this->get()) {
$someClass->run();
}
}

private function get(): ?CheckedClass
{
return rand(0, 1) ? new CheckedClass() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

class CheckedClass
{

public function run()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 048b893

Please sign in to comment.