Skip to content

Commit

Permalink
[CodeQuality] Skip compare string with stringable object on UseIdenti…
Browse files Browse the repository at this point in the history
…calOverEqualWithSameTypeRector (#5718)

* [CodeQuality] Skip compare string with stringable object on UseIdenticalOverEqualWithSameTypeRector

* using or
  • Loading branch information
samsonasik committed Mar 14, 2024
1 parent c390eab commit d01d16e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

class SkipCompareStringWithStringableObject
{
public function run()
{
$obj = new class {
public function __toString()
{
return 'a';
}
};

return 'a' == $obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,21 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);

// objects can be different by content
if ($leftStaticType instanceof ObjectType) {
if ($leftStaticType instanceof ObjectType || $rightStaticType instanceof ObjectType) {
return null;
}

if ($leftStaticType instanceof MixedType) {
if ($leftStaticType instanceof MixedType || $rightStaticType instanceof MixedType) {
return null;
}

$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);

if ($leftStaticType->isString()->yes() && $rightStaticType->isString()->yes()) {
return $this->processIdenticalOrNotIdentical($node);
}

if ($rightStaticType instanceof MixedType) {
return null;
}

// different types
if (! $leftStaticType->equals($rightStaticType)) {
return null;
Expand Down

0 comments on commit d01d16e

Please sign in to comment.