Skip to content

Commit

Permalink
[DeadCode] Skip from call with return docblock on ReduceAlwaysFalseIf…
Browse files Browse the repository at this point in the history
…OrRector (#6361)

* [DeadCode] Skip from call with return docblock on ReduceAlwaysFalseIfOrRector

* add native
  • Loading branch information
samsonasik authored Oct 7, 2024
1 parent 748330a commit 8e96b1b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class FromCallWithNativeReturn
{
/**
* @return \DateTime
*/
private function get(): \DateTime
{
return new \DateTime('now');
}

public function run($number)
{
if (! $this->get() || $number > 50) {
return 'yes';
}

return 'no';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class FromCallWithNativeReturn
{
/**
* @return \DateTime
*/
private function get(): \DateTime
{
return new \DateTime('now');
}

public function run($number)
{
if ($number > 50) {
return 'yes';
}

return 'no';
}
}

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

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class SkipFromCallWithReturnDocblock
{
/**
* @return \DateTime
*/
private function get()
{
return null;
}

public function run($number)
{
if (! $this->get() || $number > 50) {
return 'yes';
}

return 'no';
}
}
18 changes: 16 additions & 2 deletions rules/DeadCode/NodeAnalyzer/SafeLeftTypeBooleanAndOrAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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();
}
);
}
}

0 comments on commit 8e96b1b

Please sign in to comment.