2021-04-06 18:25:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\If_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Expr\Instanceof_;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PHPStan\Type\NullType;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\UnionType;
|
|
|
|
use Rector\CodeQuality\TypeResolver\AssignVariableTypeResolver;
|
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-04-06 22:00:14 +00:00
|
|
|
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
|
2021-04-06 18:25:16 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector\SimplifyIfNullableReturnRectorTest
|
|
|
|
*/
|
|
|
|
final class SimplifyIfNullableReturnRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var IfManipulator
|
|
|
|
*/
|
|
|
|
private $ifManipulator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AssignVariableTypeResolver
|
|
|
|
*/
|
|
|
|
private $assignVariableTypeResolver;
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
/**
|
|
|
|
* @var VarTagRemover
|
|
|
|
*/
|
|
|
|
private $varTagRemover;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IfManipulator $ifManipulator,
|
|
|
|
AssignVariableTypeResolver $assignVariableTypeResolver,
|
|
|
|
VarTagRemover $varTagRemover
|
2021-04-07 14:09:15 +00:00
|
|
|
) {
|
2021-04-06 18:25:16 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
|
|
|
$this->assignVariableTypeResolver = $assignVariableTypeResolver;
|
2021-04-06 22:00:14 +00:00
|
|
|
$this->varTagRemover = $varTagRemover;
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRuleDefinition(): RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Direct return on if nullable check before return', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
/** @var \stdClass|null $value */
|
|
|
|
$value = $this->foo->bar();
|
|
|
|
if (! $value instanceof \stdClass) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
2021-04-08 11:03:02 +00:00
|
|
|
return $this->foo->bar();
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [If_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param If_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var BooleanNot|Instanceof_ $cond */
|
|
|
|
$cond = $node->cond;
|
|
|
|
/** @var Instanceof_ $instanceof */
|
|
|
|
$instanceof = $cond instanceof BooleanNot
|
|
|
|
? $cond->expr
|
|
|
|
: $cond;
|
|
|
|
$variable = $instanceof->expr;
|
|
|
|
$class = $instanceof->class;
|
|
|
|
|
|
|
|
if (! $class instanceof Name) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Return_ $returnIfStmt */
|
|
|
|
$returnIfStmt = $node->stmts[0];
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
if ($this->isIfStmtReturnIncorrect($cond, $variable, $returnIfStmt)) {
|
2021-04-06 18:25:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$previous = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
|
|
|
|
if (! $previous instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$previousAssign = $previous->expr;
|
|
|
|
if (! $previousAssign instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->nodeComparator->areNodesEqual($previousAssign->var, $variable)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Return_ $next */
|
|
|
|
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
|
2021-04-06 22:00:14 +00:00
|
|
|
if ($this->isNextReturnIncorrect($cond, $variable, $next)) {
|
2021-04-06 18:25:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$variableType = $this->assignVariableTypeResolver->resolve($previousAssign);
|
|
|
|
if (! $variableType instanceof UnionType) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = $class->toString();
|
|
|
|
$types = $variableType->getTypes();
|
2021-04-06 22:00:14 +00:00
|
|
|
|
|
|
|
return $this->processSimplifyNullableReturn(
|
|
|
|
$variableType,
|
|
|
|
$types,
|
|
|
|
$className,
|
|
|
|
$next,
|
|
|
|
$previous,
|
|
|
|
$previousAssign->expr
|
|
|
|
);
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
private function isIfStmtReturnIncorrect(Expr $expr, Expr $variable, Return_ $return): bool
|
2021-04-06 18:25:16 +00:00
|
|
|
{
|
|
|
|
if (! $return->expr instanceof Expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:55:23 +00:00
|
|
|
if ($expr instanceof BooleanNot && ! $this->valueResolver->isNull($return->expr)) {
|
2021-04-06 18:25:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $expr instanceof Instanceof_ && ! $this->nodeComparator->areNodesEqual($variable, $return->expr);
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
private function isNextReturnIncorrect(Expr $expr, Expr $variable, Return_ $return): bool
|
2021-04-06 18:25:16 +00:00
|
|
|
{
|
|
|
|
if (! $return->expr instanceof Expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expr instanceof BooleanNot && ! $this->nodeComparator->areNodesEqual($return->expr, $variable)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:55:23 +00:00
|
|
|
return $expr instanceof Instanceof_ && ! $this->valueResolver->isNull($return->expr);
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Type[] $types
|
|
|
|
*/
|
|
|
|
private function processSimplifyNullableReturn(
|
2021-04-06 22:00:14 +00:00
|
|
|
UnionType $unionType,
|
2021-04-06 18:25:16 +00:00
|
|
|
array $types,
|
|
|
|
string $className,
|
|
|
|
Return_ $return,
|
|
|
|
Expression $expression,
|
|
|
|
Expr $expr
|
|
|
|
): ?Return_ {
|
|
|
|
if (count($types) > 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($types[0] instanceof FullyQualifiedObjectType && $types[1] instanceof NullType && $className === $types[0]->getClassName()) {
|
2021-04-06 22:00:14 +00:00
|
|
|
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($types[0] instanceof NullType && $types[1] instanceof FullyQualifiedObjectType && $className === $types[1]->getClassName()) {
|
2021-04-06 22:00:14 +00:00
|
|
|
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isNotTypedNullable($types, $className)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Type[] $types
|
|
|
|
*/
|
|
|
|
private function isNotTypedNullable(array $types, string $className): bool
|
|
|
|
{
|
|
|
|
if (! $types[0] instanceof ObjectType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $types[1] instanceof NullType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $className !== $types[0]->getClassName();
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
private function removeAndReturn(Return_ $return, Expression $expression, Expr $expr, UnionType $unionType): Return_
|
2021-04-06 18:25:16 +00:00
|
|
|
{
|
|
|
|
$this->removeNode($return);
|
|
|
|
$this->removeNode($expression);
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
$return = new Return_($expr);
|
|
|
|
$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($expression, $unionType);
|
|
|
|
$this->mirrorComments($return, $expression);
|
|
|
|
|
|
|
|
return $return;
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldSkip(If_ $if): bool
|
|
|
|
{
|
|
|
|
if (! $this->ifManipulator->isIfWithOnly($if, Return_::class)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
$next = $if->getAttribute(AttributeKey::NEXT_NODE);
|
|
|
|
if (! $next instanceof Return_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:25:16 +00:00
|
|
|
$cond = $if->cond;
|
|
|
|
|
|
|
|
if (! $cond instanceof BooleanNot) {
|
|
|
|
return ! $cond instanceof Instanceof_;
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:00:14 +00:00
|
|
|
return ! $cond->expr instanceof Instanceof_;
|
2021-04-06 18:25:16 +00:00
|
|
|
}
|
|
|
|
}
|