Skip to content

Commit

Permalink
include unary minus check
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 3, 2024
1 parent 2c1ae9b commit e55b834
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictReturnsRector\Fixture;

final class DirectReturn
final class SkipDirectReturn
{
public function resolve()
{
return 1000;
}

public function next()
{
return -400;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\Fixture;

final class IncludeUnaryMinus
{
public function resolve()
{
return -1000;
}

public function next()
{
return +1000;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector\Fixture;

final class IncludeUnaryMinus
{
public function resolve(): int
{
return -1000;
}

public function next()
{
return +1000;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,16 @@ private function isAlwaysNumeric(array $returns): bool
$isAlwaysInt = true;

foreach ($returns as $return) {
if (! $return->expr instanceof DNumber) {
$epxr = $return->expr;
if ($epxr instanceof Expr\UnaryMinus) {
$epxr = $epxr->expr;
}

if (! $epxr instanceof DNumber) {
$isAlwaysFloat = false;
}

if (! $return->expr instanceof LNumber) {
if (! $epxr instanceof LNumber) {
$isAlwaysInt = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
$isAlwaysFloat = true;

foreach ($returns as $return) {
if (! $return->expr instanceof DNumber) {
$expr = $return->expr;
if ($expr instanceof Node\Expr\UnaryMinus) {
$expr = $expr->expr;
}

if (! $expr instanceof DNumber) {
$isAlwaysFloat = false;
}

if (! $return->expr instanceof LNumber) {
if (! $expr instanceof LNumber) {
$isAlwaysInt = false;
}
}
Expand Down

0 comments on commit e55b834

Please sign in to comment.