Skip to content
Merged
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
Expand Up @@ -10,9 +10,8 @@
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 PHPStan\Type\Type;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -22,6 +21,11 @@
*/
final class UseIdenticalOverEqualWithSameTypeRector extends AbstractRector
{
public function __construct(
private readonly TypeComparator $typeComparator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -74,10 +78,6 @@ public function refactor(Node $node): ?Node
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);

if ($this->shouldSkipCompareBoolToNumeric($leftStaticType, $rightStaticType)) {
return null;
}

// objects can be different by content
if (! $leftStaticType->isObject()->no() || ! $rightStaticType->isObject()->no()) {
return null;
Expand All @@ -92,44 +92,13 @@ public function refactor(Node $node): ?Node
}

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

return $this->processIdenticalOrNotIdentical($node);
}

private function shouldSkipCompareBoolToNumeric(Type $leftStaticType, Type $rightStaticType): bool
{
if ($leftStaticType instanceof BooleanType) {
return $this->shouldSkipNumericType($rightStaticType);
}

if ($rightStaticType instanceof BooleanType) {
return $this->shouldSkipNumericType($leftStaticType);
}

return false;
}

private function shouldSkipNumericType(Type $type): bool
{
// use ! ->no() as to verify both yes and maybe
if (! $type->isNumericString()->no()) {
return true;
}

if (! $type->isInteger()
->no()) {
return true;
}

return ! $type->isFloat()
->no();
}

private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical
{
if ($node instanceof Equal) {
Expand Down