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/i3oCg
*/
final class SkipBoolEqualInteger
{
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 @@ -74,7 +74,7 @@ public function refactor(Node $node): ?Node
$leftStaticType = $this->nodeTypeResolver->getNativeType($node->left);
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);

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

Expand All @@ -101,15 +101,25 @@ public function refactor(Node $node): ?Node
return $this->processIdenticalOrNotIdentical($node);
}

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

return ! $rightStaticType->isInteger()
->no();
}

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

return ! $leftStaticType->isInteger()
->no();
}

return false;
Expand Down