Skip to content

Commit

Permalink
[EarlyReturn] Skip ReturnEarlyIfVariableRector with @var doc (#2616)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jul 3, 2022
1 parent f608461 commit 25accb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\Fixture;

final class SkipWithVarDoc
{
public function run(int $value): int|string
{
if ($value === 50) {
/** @var string $value */
$value = execute();
}

return $value;
}
}
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -118,6 +119,11 @@ private function matchOnlyIfStmtReturnExpr(Stmt $onlyIfStmt, Variable $returnVar
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($onlyIfStmt);
if ($phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
return null;
}

$assign = $onlyIfStmt->expr;

// assign to same variable that is returned
Expand Down

0 comments on commit 25accb2

Please sign in to comment.