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,18 @@
<?php

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

/**
* @see https://3v4l.org/9XRF6
*/
final class SkipIdenticalNumericStringCompareBool
{
public function equal()
{
// maybe compare to numeric string
$value = rand(0, 1) ? '1' : null;

var_dump(true == $value);
var_dump($value == true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PHPStan\Type\BooleanType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -73,6 +74,10 @@ public function refactor(Node $node): ?Node
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);

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

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

private function shouldSkipCompareNumericString(Type $leftStaticType, Type $rightStaticType): bool
{
// use ! ->no() as to support both yes and maybe
if ($leftStaticType instanceof BooleanType) {
return ! $rightStaticType->isNumericString()->no();
}

if ($rightStaticType instanceof BooleanType) {
return ! $leftStaticType->isNumericString()->no();
}

return false;
}

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