Skip to content

Commit

Permalink
[CodeQuality] Add support for compare $this::class compare with strin…
Browse files Browse the repository at this point in the history
…g on UseIdenticalOverEqualWithSameTypeRector (#5698)
  • Loading branch information
samsonasik committed Mar 7, 2024
1 parent 4a1880b commit 6225460
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
@@ -0,0 +1,37 @@
<?php

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

abstract class ThisClass
{
public function run()
{
return $this::class == 'SomeClass';
}

public function run2()
{
return 'SomeClass' == $this::class;
}
}

?>
-----
<?php

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

abstract class ThisClass
{
public function run()
{
return $this::class === 'SomeClass';
}

public function run2()
{
return 'SomeClass' === $this::class;
}
}

?>
Expand Up @@ -77,6 +77,11 @@ public function refactor(Node $node): ?Node
}

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

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

if ($rightStaticType instanceof MixedType) {
return null;
}
Expand All @@ -86,6 +91,11 @@ public function refactor(Node $node): ?Node
return null;
}

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

private function processIdenticalOrNotIdentical(Equal|NotEqual $node): Identical|NotIdentical
{
if ($node instanceof Equal) {
return new Identical($node->left, $node->right);
}
Expand Down

0 comments on commit 6225460

Please sign in to comment.