Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Skip compare string with stringable object on UseIdenticalOverEqualWithSameTypeRector #5718

Merged
merged 2 commits into from
Mar 14, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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