Skip to content

Commit

Permalink
[CodeQuality] Keep identical false variable compare as is it is in Si…
Browse files Browse the repository at this point in the history
…mplifyBoolIdenticalTrueRector (#2839)
  • Loading branch information
TomasVotruba committed Aug 25, 2022
1 parent fadb6a4 commit 7215c28
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class Directly
final class Directly
{
public function run(bool $value, string $items)
{
Expand All @@ -20,7 +20,7 @@ class Directly

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class Directly
final class Directly
{
public function run(bool $value, string $items)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class DoubleNegate
final class DoubleNegate
{
public function run()
{
Expand All @@ -18,7 +18,7 @@ class DoubleNegate

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class DoubleNegate
final class DoubleNegate
{
public function run()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class Negate
final class Negate
{
public function run($value, array $items)
{
Expand All @@ -19,7 +19,7 @@ class Negate

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

class Negate
final class Negate
{
public function run($value, array $items)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

final class SkipEqualsFalse
{
public function run(bool $value)
{
if ($value === false) {
return 'not yet';
}

return 'yes';
}

public function runFromTheOtherSide(bool $value)
{
if (false === $value) {
return 'not yet';
}

return 'yes';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\BooleanType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -106,6 +107,13 @@ private function refactorIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr
return $leftExpr->expr;
}

$leftExprType = $this->getType($leftExpr);

// keep as it is, readable enough
if ($leftExpr instanceof Variable && $leftExprType instanceof BooleanType) {
return null;
}

return new BooleanNot($leftExpr);
}

Expand Down

0 comments on commit 7215c28

Please sign in to comment.