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

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

final class SkipAssignPlusNegativeZero
{
protected function run()
{
$value = round(-0.001, 2);
var_dump((string) $value);

$value += 0;
var_dump((string) $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

final class SkipNegativeZero
{
protected function run()
{
$value = round(-0.001, 2);
var_dump((string) $value);

$value = $value + 0;
var_dump((string) $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

final class SkipZeroMinusPositiveZero
{
protected function run()
{
$value = 0.0;
var_dump((string) $value);

$value = 0 - $value;
var_dump((string) $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

final class SkipZeroPlusNegativeZero
{
protected function run()
{
$value = round(-0.001, 2);
var_dump((string) $value);

$value = 0 + $value;
var_dump((string) $value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ private function processAssignOp(AssignOp $assignOp): ?Expr
return null;
}

// on float, += 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
if ($assignOp instanceof AssignPlus && ! $this->nodeTypeResolver->getNativeType($assignOp->var)
->isInteger()
->yes()) {
return null;
}

if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
return $assignOp->var;
}
Expand Down Expand Up @@ -152,7 +159,15 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
return null;
}

if ($this->isLiteralZero($binaryOp->left) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
if ($this->isLiteralZero($binaryOp->left)) {
// on float, 0 + $x normalizes negative zero (-0.0) to 0.0 and 0 - $x flips its sign,
// so the operation is not dead
if (! $this->nodeTypeResolver->getNativeType($binaryOp->right)
->isInteger()
->yes()) {
return null;
}

if ($binaryOp instanceof Minus) {
return new UnaryMinus($binaryOp->right);
}
Expand All @@ -164,6 +179,13 @@ private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
return null;
}

// on float, $x + 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
if ($binaryOp instanceof Plus && ! $this->nodeTypeResolver->getNativeType($binaryOp->left)
->isInteger()
->yes()) {
return null;
}

return $binaryOp->left;
}

Expand Down
Loading