Skip to content

Commit

Permalink
[TypeDeclaration] Add isset(), empty(), and negation support on BoolR…
Browse files Browse the repository at this point in the history
…eturnTypeFromBooleanStrictReturnsRector (#6339)

* [TypeDeclaration] Add isset(), empty(), and negation support on BoolReturnTypeFromBooleanStrictReturnsRector

* implemented
  • Loading branch information
samsonasik authored Oct 1, 2024
1 parent 6b065ef commit 54a6620
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class WithEmpty
{
public function resolve($a)
{
return empty($a['test']);
}
}

?>
-----
<?php

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

final class WithEmpty
{
public function resolve($a): bool
{
return empty($a['test']);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class WithIsset
{
public function resolve($a)
{
return isset($a['test']);
}
}

?>
-----
<?php

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

final class WithIsset
{
public function resolve($a): bool
{
return isset($a['test']);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

final class WithNegation
{
public function resolve($a)
{
return ! $a;
}
}

?>
-----
<?php

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

final class WithNegation
{
public function resolve($a): bool
{
return ! $a;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -145,7 +148,7 @@ private function hasOnlyBoolScalarReturnExprs(array $returns): bool
return false;
}

if ($this->isBooleanBinaryOp($return->expr)) {
if ($this->isBooleanOp($return->expr)) {
continue;
}

Expand Down Expand Up @@ -184,7 +187,7 @@ private function isNativeBooleanReturnTypeFuncCall(FuncCall $funcCall): bool
return false;
}

private function isBooleanBinaryOp(Expr $expr): bool
private function isBooleanOp(Expr $expr): bool
{
if ($expr instanceof Smaller) {
return true;
Expand Down Expand Up @@ -222,7 +225,19 @@ private function isBooleanBinaryOp(Expr $expr): bool
return true;
}

return $expr instanceof NotEqual;
if ($expr instanceof NotEqual) {
return true;
}

if ($expr instanceof Empty_) {
return true;
}

if ($expr instanceof Isset_) {
return true;
}

return $expr instanceof BooleanNot;
}

/**
Expand Down

0 comments on commit 54a6620

Please sign in to comment.