Skip to content

Commit

Permalink
[DeadCode] Skip used defined in extract() on RemoveNonExistingVarAnno…
Browse files Browse the repository at this point in the history
…tationRector (#4468)

* [DeadCode] Skip used defined in extract() on RemoveNonExistingVarAnnotationRector

* [DeadCode] Skip used defined in extract() on RemoveNonExistingVarAnnotationRector

* [DeadCode] Skip used defined in extract() on RemoveNonExistingVarAnnotationRector
  • Loading branch information
samsonasik committed Jul 10, 2023
1 parent 3cc6365 commit 5b64258
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class SkipUsedAfterNextStatement
{
public function get(): void
{
$options = [
'a' => 'value a',
'b' => 'value b',
'c' => 'value c',
];

extract($options);
/**
* @var string $a
* @var string $b
* @var string $c
*/
$callback = function ($var1, $var2, $var3) {
return $var1 . $var2 . $var3;
};

$callback($a, $b, $c);
}
}

?>
129 changes: 101 additions & 28 deletions rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@

use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\While_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -25,6 +35,27 @@
*/
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
{
/**
* @var array<class-string<Stmt>>
*/
private const NODE_TYPES = [
Foreach_::class,
Static_::class,
Echo_::class,
Return_::class,
Expression::class,
Throw_::class,
If_::class,
While_::class,
Switch_::class,
Nop::class,
];

public function __construct(
private readonly StmtsManipulator $stmtsManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -61,56 +92,98 @@ public function get()
*/
public function getNodeTypes(): array
{
return [Stmt::class];
return [StmtsAwareInterface::class];
}

/**
* @param Stmt $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
if ($node->stmts === null) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$hasChanged = false;
$extractValues = [];

foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof Expression && $stmt->expr instanceof FuncCall && $this->isName(
$stmt->expr,
'extract'
) && ! $stmt->expr->isFirstClassCallable()) {
$appendExtractValues = $this->valueResolver->getValue($stmt->expr->getArgs()[0]->value);
if (! is_array($appendExtractValues)) {
// nothing can do as value is dynamic
break;
}

$extractValues = [...$extractValues, ...array_keys($appendExtractValues)];
continue;
}

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return null;
}
if ($this->shouldSkip($node, $key, $stmt, $extractValues)) {
continue;
}

if ($this->isObjectShapePseudoType($varTagValueNode)) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);

$variableName = ltrim($varTagValueNode->variableName, '$');
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
continue;
}

if ($variableName === '' && $this->isAnnotatableReturn($node)) {
return null;
}
if ($this->isObjectShapePseudoType($varTagValueNode)) {
continue;
}

if ($this->hasVariableName($node, $variableName)) {
return null;
$variableName = ltrim($varTagValueNode->variableName, '$');

if ($variableName === '' && $this->isAnnotatableReturn($stmt)) {
continue;
}

if ($this->hasVariableName($stmt, $variableName)) {
continue;
}

$comments = $node->getComments();
if (isset($comments[1])) {
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
continue;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
$hasChanged = true;
}

$comments = $node->getComments();
if (isset($comments[1])) {
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
return null;
if ($hasChanged) {
return $node;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
return $node;
return null;
}

private function shouldSkip(Stmt $stmt): bool
/**
* @param string[] $extractValues
*/
private function shouldSkip(StmtsAwareInterface $stmtsAware, int $key, Stmt $stmt, array $extractValues): bool
{
if ($stmt instanceof ClassConst || $stmt instanceof Property) {
if (! in_array($stmt::class, self::NODE_TYPES, true)) {
return true;
}

if (count($stmt->getComments()) !== 1) {
return true;
}

return count($stmt->getComments()) !== 1;
foreach ($extractValues as $extractValue) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, $extractValue)) {
return true;
}
}

return false;
}

private function hasVariableName(Stmt $stmt, string $variableName): bool
Expand Down

0 comments on commit 5b64258

Please sign in to comment.