From 54a66206986e685787d7e038929618a66e98ec42 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 1 Oct 2024 12:25:35 +0700 Subject: [PATCH] [TypeDeclaration] Add isset(), empty(), and negation support on BoolReturnTypeFromBooleanStrictReturnsRector (#6339) * [TypeDeclaration] Add isset(), empty(), and negation support on BoolReturnTypeFromBooleanStrictReturnsRector * implemented --- .../Fixture/with_empty.php.inc | 27 +++++++++++++++++++ .../Fixture/with_isset.php.inc | 27 +++++++++++++++++++ .../Fixture/with_negation.php.inc | 27 +++++++++++++++++++ ...turnTypeFromBooleanStrictReturnsRector.php | 21 ++++++++++++--- 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_empty.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_isset.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_negation.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_empty.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_empty.php.inc new file mode 100644 index 00000000000..39f1c8b3f7f --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_empty.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_isset.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_isset.php.inc new file mode 100644 index 00000000000..06c95d24589 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_isset.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_negation.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_negation.php.inc new file mode 100644 index 00000000000..1700c1f8e59 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector/Fixture/with_negation.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php index c8976c9e0e0..9918a96cc81 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromBooleanStrictReturnsRector.php @@ -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; @@ -145,7 +148,7 @@ private function hasOnlyBoolScalarReturnExprs(array $returns): bool return false; } - if ($this->isBooleanBinaryOp($return->expr)) { + if ($this->isBooleanOp($return->expr)) { continue; } @@ -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; @@ -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; } /**