2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Node;
|
2019-07-05 12:14:36 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\AssignRef;
|
2023-03-28 22:41:17 +00:00
|
|
|
use PhpParser\Node\Expr\CallLike;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2023-03-28 22:41:17 +00:00
|
|
|
use PhpParser\Node\Expr\New_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
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;
|
2022-08-24 14:03:58 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-09-01 19:30:48 +00:00
|
|
|
use Rector\Core\Util\MultiInstanceofChecker;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-07-05 12:14:36 +00:00
|
|
|
/**
|
2021-03-21 18:41:17 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector\RemoveNonExistingVarAnnotationRectorTest
|
2020-01-31 08:52:34 +00:00
|
|
|
*
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://github.com/phpstan/phpstan/commit/d17e459fd9b45129c5deafe12bca56f30ea5ee99#diff-9f3541876405623b0d18631259763dc1
|
2019-07-05 12:14:36 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
2020-02-09 11:31:31 +00:00
|
|
|
/**
|
2021-02-21 23:39:53 +00:00
|
|
|
* @var array<class-string<Node>>
|
2020-02-09 11:31:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private const NODES_TO_MATCH = [Assign::class, AssignRef::class, Foreach_::class, Static_::class, Echo_::class, Return_::class, Expression::class, Throw_::class, If_::class, While_::class, Switch_::class, Nop::class];
|
2021-01-19 19:11:10 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-09-01 19:30:48 +00:00
|
|
|
* @var \Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer
|
2021-01-19 19:11:10 +00:00
|
|
|
*/
|
2022-09-01 19:30:48 +00:00
|
|
|
private $exprUsedInNodeAnalyzer;
|
2021-09-04 07:47:01 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-09-01 19:30:48 +00:00
|
|
|
* @var \Rector\Core\Util\MultiInstanceofChecker
|
2021-09-04 07:47:01 +00:00
|
|
|
*/
|
2022-09-01 19:30:48 +00:00
|
|
|
private $multiInstanceofChecker;
|
|
|
|
public function __construct(ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer, MultiInstanceofChecker $multiInstanceofChecker)
|
2021-01-19 19:11:10 +00:00
|
|
|
{
|
2021-09-04 07:47:01 +00:00
|
|
|
$this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer;
|
2022-09-01 19:30:48 +00:00
|
|
|
$this->multiInstanceofChecker = $multiInstanceofChecker;
|
2021-01-19 19:11:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Removes non-existing @var annotations above the code', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-07-05 12:14:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
/** @var Training[] $trainings */
|
|
|
|
return $this->getData();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-07-05 12:14:36 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->getData();
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-07-05 12:14:36 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Node::class];
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-18 21:45:13 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
2021-01-19 19:11:10 +00:00
|
|
|
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$varTagValueNode instanceof VarTagValueNode) {
|
2019-07-05 12:14:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-24 14:03:58 +00:00
|
|
|
if ($this->isObjectShapePseudoType($varTagValueNode)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$variableName = \ltrim($varTagValueNode->variableName, '$');
|
2023-03-28 22:41:17 +00:00
|
|
|
if ($variableName === '' && $this->isAnnotatableReturn($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-19 19:11:10 +00:00
|
|
|
if ($this->hasVariableName($node, $variableName)) {
|
2019-07-05 12:14:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-04 07:47:01 +00:00
|
|
|
if ($this->isUsedInNextNodeWithExtractPreviouslyCalled($node, $variableName)) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-26 10:30:52 +00:00
|
|
|
$comments = $node->getComments();
|
2021-11-28 17:35:55 +00:00
|
|
|
if (isset($comments[1])) {
|
2022-05-11 16:53:20 +00:00
|
|
|
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
|
|
|
|
return null;
|
2020-12-26 10:30:52 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$phpDocInfo->removeByType(VarTagValueNode::class);
|
2019-07-05 12:14:36 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isUsedInNextNodeWithExtractPreviouslyCalled(Node $node, string $variableName) : bool
|
2021-09-04 07:47:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$variable = new Variable($variableName);
|
|
|
|
$isUsedInNextNode = (bool) $this->betterNodeFinder->findFirstNext($node, function (Node $node) use($variable) : bool {
|
2021-09-04 07:47:01 +00:00
|
|
|
return $this->exprUsedInNodeAnalyzer->isUsed($node, $variable);
|
|
|
|
});
|
|
|
|
if (!$isUsedInNextNode) {
|
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return (bool) $this->betterNodeFinder->findFirstPrevious($node, function (Node $subNode) : bool {
|
|
|
|
if (!$subNode instanceof FuncCall) {
|
2021-09-04 07:47:01 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return $this->nodeNameResolver->isName($subNode, 'extract');
|
|
|
|
});
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(Node $node) : bool
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof Nop) {
|
2022-09-01 19:30:48 +00:00
|
|
|
return !$this->multiInstanceofChecker->isInstanceOf($node, self::NODES_TO_MATCH);
|
2020-12-04 11:31:49 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->getComments()) <= 1) {
|
2022-09-01 19:30:48 +00:00
|
|
|
return !$this->multiInstanceofChecker->isInstanceOf($node, self::NODES_TO_MATCH);
|
2021-02-21 09:32:45 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-01-19 19:11:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasVariableName(Node $node, string $variableName) : bool
|
2021-01-19 19:11:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return (bool) $this->betterNodeFinder->findFirst($node, function (Node $node) use($variableName) : bool {
|
|
|
|
if (!$node instanceof Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-01-19 19:11:10 +00:00
|
|
|
}
|
|
|
|
return $this->isName($node, $variableName);
|
|
|
|
});
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
2022-08-24 14:03:58 +00:00
|
|
|
/**
|
|
|
|
* This is a hack,
|
|
|
|
* that waits on phpdoc-parser to get merged - https://github.com/phpstan/phpdoc-parser/pull/145
|
|
|
|
*/
|
|
|
|
private function isObjectShapePseudoType(VarTagValueNode $varTagValueNode) : bool
|
|
|
|
{
|
|
|
|
if (!$varTagValueNode->type instanceof IdentifierTypeNode) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($varTagValueNode->type->name !== 'object') {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if (\strncmp($varTagValueNode->description, '{', \strlen('{')) !== 0) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return \strpos($varTagValueNode->description, '}') !== \false;
|
|
|
|
}
|
2023-03-28 22:41:17 +00:00
|
|
|
private function isAnnotatableReturn(Node $node) : bool
|
|
|
|
{
|
|
|
|
return $node instanceof Return_ && $node->expr instanceof CallLike && !$node->expr instanceof New_;
|
|
|
|
}
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|