Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip assign in if condition on BinaryOpNullableToInstanceofRector #6629

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Source\SomeInstance;

final class SkipAssign
{
private ?SomeInstance $someClass;

public function run()
{
if (($this->someClass = $this->get()) && $this->someClass->someMethod()) {
return $this->someClass->someMethod();
}

return 'no';
}

private function get(): ?SomeInstance
{
return rand(0, 1) ? new SomeInstance() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector\Source\SomeInstance;

final class SkipAssign2
{
private ?SomeInstance $someClass;

public function run(bool $a)
{
if ($a === true && ($this->someClass = $this->get()) && $this->someClass->someMethod()) {
return $this->someClass->someMethod();
}

return 'no';
}

private function get(): ?SomeInstance
{
return rand(0, 1) ? new SomeInstance() : null;
}
}
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\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BooleanNot;
Expand Down Expand Up @@ -70,6 +71,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->left instanceof Assign || $node->right instanceof Assign) {
return null;
}

if ($node instanceof BooleanOr) {
return $this->processNegationBooleanOr($node);
}
Expand Down
Loading