Skip to content
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,45 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;

final class IdenticalBool
{
public function equal()
{
if ($this->getValue() == true) {
return 'yes';
}

return 'no';
}

private function getValue(): bool
{
return true;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;

final class IdenticalBool
{
public function equal()
{
if ($this->getValue() === true) {
return 'yes';
}

return 'no';
}

private function getValue(): bool
{
return true;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;

final class IdenticalString
{
public function equal()
{
if ($this->getValue() == 'hi') {
return 'yes';
}

return 'no';
}

private function getValue(): string
{
return 'hello';
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;

final class IdenticalString
{
public function equal()
{
if ($this->getValue() === 'hi') {
Copy link
Member

@samsonasik samsonasik Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cause regression on numeric string "1", see https://3v4l.org/JEeP5

I will create PR to skip numeric string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found another regression that it cause invalid result on integer 0 vs false, see https://3v4l.org/Pp8Vr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found another regression that it cause invalid result on float vs bool, see https://3v4l.org/I3GQS

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a better approach by using TypeComparator so the type are both normalized on equal check.

return 'yes';
}

return 'no';
}

private function getValue(): string
{
return 'hello';
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PHPStan\Type\BooleanType;
use PHPStan\Type\MixedType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -86,7 +87,9 @@ public function refactor(Node $node): ?Node
}

// different types
if (! $leftStaticType->equals($rightStaticType)) {
if (! $leftStaticType->equals(
$rightStaticType
) && (! $leftStaticType instanceof BooleanType && ! $rightStaticType instanceof BooleanType)) {
return null;
}

Expand Down