Skip to content

Commit

Permalink
[CodeQuality] Handle with assign missing parentheses on ExplicitBoolC…
Browse files Browse the repository at this point in the history
…ompareRector (#6668)
  • Loading branch information
samsonasik authored Jan 13, 2025
1 parent 0a6eb12 commit 5ad5480
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ExplicitBoolCompareRector\Fixture;

final class WithAssign
{
private function getExpiredAt(): \Datetime|null
{
return null;
}

public function run(string $item)
{
if ($expired = $this->getExpiredAt()) {
echo $expired->format('Ymd');
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\ExplicitBoolCompareRector\Fixture;

final class WithAssign
{
private function getExpiredAt(): \Datetime|null
{
return null;
}

public function run(string $item)
{
if (($expired = $this->getExpiredAt()) !== null) {
echo $expired->format('Ymd');
}
}
}

?>
5 changes: 5 additions & 0 deletions rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\NodeTypeResolver\TypeAnalyzer\StringTypeAnalyzer;
use Rector\PhpParser\Node\Value\ValueResolver;
Expand Down Expand Up @@ -129,6 +130,10 @@ public function refactor(Node $node): null|array|Node
return [$expression, $node];
}

if ($node->cond instanceof Assign) {
$binaryOp->left->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}

$node->cond = $binaryOp;

return $node;
Expand Down

0 comments on commit 5ad5480

Please sign in to comment.