mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
9811962c70
b4eb883e91
[DeadCode] Skip isset() from property fetch from docblock on RemoveAlwaysTrueIfConditionRector (#5754)
49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\NodeAnalyzer;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use Rector\NodeAnalyzer\ExprAnalyzer;
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
final class SafeLeftTypeBooleanAndOrAnalyzer
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
|
*/
|
|
private $betterNodeFinder;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
|
*/
|
|
private $exprAnalyzer;
|
|
public function __construct(BetterNodeFinder $betterNodeFinder, ExprAnalyzer $exprAnalyzer)
|
|
{
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
$this->exprAnalyzer = $exprAnalyzer;
|
|
}
|
|
/**
|
|
* @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BinaryOp\BooleanOr $booleanAnd
|
|
*/
|
|
public function isSafe($booleanAnd) : bool
|
|
{
|
|
$hasNonTypedFromParam = (bool) $this->betterNodeFinder->findFirst($booleanAnd->left, function (Node $node) : bool {
|
|
return $node instanceof Variable && $this->exprAnalyzer->isNonTypedFromParam($node);
|
|
});
|
|
if ($hasNonTypedFromParam) {
|
|
return \false;
|
|
}
|
|
// get type from Property and ArrayDimFetch is unreliable
|
|
return !(bool) $this->betterNodeFinder->findFirst($booleanAnd->left, static function (Node $node) : bool {
|
|
return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch || $node instanceof ArrayDimFetch;
|
|
});
|
|
}
|
|
}
|