From 8e96b1b9dbd4a5fdf8da8d863aa9e153298d7427 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 8 Oct 2024 01:56:21 +0700 Subject: [PATCH] [DeadCode] Skip from call with return docblock on ReduceAlwaysFalseIfOrRector (#6361) * [DeadCode] Skip from call with return docblock on ReduceAlwaysFalseIfOrRector * add native --- .../from_call_with_native_return.php.inc | 51 +++++++++++++++++++ ...kip_from_call_with_return_docblock.php.inc | 23 +++++++++ .../SafeLeftTypeBooleanAndOrAnalyzer.php | 18 ++++++- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/from_call_with_native_return.php.inc create mode 100644 rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/skip_from_call_with_return_docblock.php.inc diff --git a/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/from_call_with_native_return.php.inc b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/from_call_with_native_return.php.inc new file mode 100644 index 00000000000..86030d12d94 --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/from_call_with_native_return.php.inc @@ -0,0 +1,51 @@ +get() || $number > 50) { + return 'yes'; + } + + return 'no'; + } +} + +?> +----- + 50) { + return 'yes'; + } + + return 'no'; + } +} + +?> \ No newline at end of file diff --git a/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/skip_from_call_with_return_docblock.php.inc b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/skip_from_call_with_return_docblock.php.inc new file mode 100644 index 00000000000..f1da9251d7d --- /dev/null +++ b/rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/skip_from_call_with_return_docblock.php.inc @@ -0,0 +1,23 @@ +get() || $number > 50) { + return 'yes'; + } + + return 'no'; + } +} diff --git a/rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php b/rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php index 34a7c788f22..5b8060f420c 100644 --- a/rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php @@ -8,12 +8,15 @@ use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; +use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; use PHPStan\Reflection\ClassReflection; +use PHPStan\Type\MixedType; use Rector\NodeAnalyzer\ExprAnalyzer; +use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Reflection\ReflectionResolver; @@ -22,7 +25,8 @@ public function __construct( private readonly BetterNodeFinder $betterNodeFinder, private readonly ExprAnalyzer $exprAnalyzer, - private readonly ReflectionResolver $reflectionResolver + private readonly ReflectionResolver $reflectionResolver, + private readonly NodeTypeResolver $nodeTypeResolver, ) { } @@ -53,6 +57,16 @@ public function isSafe(BooleanAnd|BooleanOr $booleanAnd): bool return ! $booleanAnd->left instanceof Instanceof_; } - return true; + return ! (bool) $this->betterNodeFinder->findFirst( + $booleanAnd->left, + function (Node $node): bool { + if (! $node instanceof CallLike) { + return false; + } + + $nativeType = $this->nodeTypeResolver->getNativeType($node); + return $nativeType instanceof MixedType && ! $nativeType->isExplicitMixed(); + } + ); } }