Skip to content

Commit

Permalink
[Php71] Handle not identical to Float for empty string should compare…
Browse files Browse the repository at this point in the history
… to 0.0 on BinaryOpBetweenNumberAndStringRector (#6117)

* [Php71] Handle not identical to Float for empty string should compare to 0.0 on BinaryOpBetweenNumberAndStringRector

* Fix
  • Loading branch information
samsonasik committed Jul 5, 2024
1 parent 3fe5553 commit e63e9a2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Fixture
public function run()
{
$value = 5 + 0;
$value = 5.0 + 0;
$value = 5.0 + 0.0;
$value = 5 + 0;

$value = 5 * 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\Fixture;

class NotIdenticalFloat
{
function foo(): ?float
{
return 0;
}

public function run()
{
$data = $this->foo();
if (! is_null($data)) {
var_dump($data !== '');
}
}
}

?>
-----
<?php

namespace Rector\Tests\Php71\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\Fixture;

class NotIdenticalFloat
{
function foo(): ?float
{
return 0;
}

public function run()
{
$data = $this->foo();
if (! is_null($data)) {
var_dump($data !== 0.0);
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\MagicConst\Line;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -60,7 +61,7 @@ class SomeClass
public function run()
{
$value = 5 + 0;
$value = 5.0 + 0;
$value = 5.0 + 0.0;
}
}
CODE_SAMPLE
Expand Down Expand Up @@ -101,15 +102,19 @@ public function refactor(Node $node): ?Node
if ($this->isStringOrStaticNonNumericString($node->left) && $this->nodeTypeResolver->isNumberType(
$node->right
)) {
$node->left = new LNumber(0);
$node->left = $this->nodeTypeResolver->getNativeType($node->right)->isInteger()->yes()
? new LNumber(0)
: new DNumber(0);

return $node;
}

if ($this->isStringOrStaticNonNumericString($node->right) && $this->nodeTypeResolver->isNumberType(
$node->left
)) {
$node->right = new LNumber(0);
$node->right = $this->nodeTypeResolver->getNativeType($node->left)->isInteger()->yes()
? new LNumber(0)
: new DNumber(0);

return $node;
}
Expand Down

0 comments on commit e63e9a2

Please sign in to comment.