From f8ad265fa5e4c58a4d0e973a73cac2c4e1efcb3f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 8 Dec 2022 06:18:54 +0700 Subject: [PATCH] [CodeQuality] Handle BooleanNot on SimplifyEmptyCheckOnEmptyArrayRector (#3170) --- .../Fixture/replace_array_type.php.inc | 2 +- .../Fixture/replace_is_array_check.php.inc | 2 +- .../Fixture/skip_boolean_not_array.php.inc | 13 ++++++++++ .../SimplifyEmptyCheckOnEmptyArrayRector.php | 24 ++++++++++++------- 4 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/skip_boolean_not_array.php.inc diff --git a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_array_type.php.inc b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_array_type.php.inc index 12f92daa246..87253501e8b 100644 --- a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_array_type.php.inc +++ b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_array_type.php.inc @@ -55,7 +55,7 @@ final class ReplaceArrayType public function isNotEmptyArray(array $values): bool { - return !($values === []); + return $values !== []; } public function isEmptyArrayMixedType(mixed $values): bool diff --git a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_is_array_check.php.inc b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_is_array_check.php.inc index 337873b426d..55b0aa3c4ef 100644 --- a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_is_array_check.php.inc +++ b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/replace_is_array_check.php.inc @@ -39,7 +39,7 @@ final class ReplaceIsArrayCheck public function isNotEmptyArray($values): bool { - return is_array($values) && !($values === []); + return is_array($values) && $values !== []; } public function isEmptyArrayProperty(): bool diff --git a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/skip_boolean_not_array.php.inc b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/skip_boolean_not_array.php.inc new file mode 100644 index 00000000000..84fe1c3fcca --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRectorTest/Fixture/skip_boolean_not_array.php.inc @@ -0,0 +1,13 @@ + diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 8b9016cb5ce..f169615f96a 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -8,6 +8,8 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\BinaryOp\Identical; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; @@ -42,33 +44,37 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [Empty_::class]; + return [Empty_::class, BooleanNot::class]; } /** - * @param Empty_ $node $node + * @param Empty_|BooleanNot $node $node */ public function refactorWithScope(Node $node, Scope $scope): ?Node { - if (! $this->isAllowedExpr($node->expr)) { - return null; - } + if ($node instanceof BooleanNot) { + if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) { + return new NotIdentical($node->expr->expr, new Array_()); + } - if (! $scope->getType($node->expr) instanceof ArrayType) { return null; } - if ($this->exprAnalyzer->isNonTypedFromParam($node->expr)) { + if (! $this->isAllowedExpr($node->expr, $scope)) { return null; } return new Identical($node->expr, new Array_()); } - private function isAllowedExpr(Expr $expr): bool + private function isAllowedExpr(Expr $expr, Scope $scope): bool { + if (! $scope->getType($expr) instanceof ArrayType) { + return false; + } + if ($expr instanceof Variable) { - return true; + return ! $this->exprAnalyzer->isNonTypedFromParam($expr); } if ($expr instanceof PropertyFetch) {