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;
|
2023-10-10 10:46:27 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2023-03-28 22:41:17 +00:00
|
|
|
use PhpParser\Node\Expr\CallLike;
|
2023-07-10 09:23:28 +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;
|
2023-07-08 21:56:17 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2023-07-10 09:23:28 +00:00
|
|
|
use PhpParser\Node\Stmt\Echo_;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
2023-10-10 10:46:27 +00:00
|
|
|
use PhpParser\Node\Stmt\InlineHTML;
|
2023-07-10 09:23:28 +00:00
|
|
|
use PhpParser\Node\Stmt\Nop;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2023-07-10 09:23:28 +00:00
|
|
|
use PhpParser\Node\Stmt\Static_;
|
|
|
|
use PhpParser\Node\Stmt\Switch_;
|
|
|
|
use PhpParser\Node\Stmt\Throw_;
|
|
|
|
use PhpParser\Node\Stmt\While_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
2022-08-24 14:03:58 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
2023-09-20 12:41:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2023-09-11 13:16:45 +00:00
|
|
|
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\NodeManipulator\StmtsManipulator;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
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
|
|
|
{
|
2023-07-10 09:23:28 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\StmtsManipulator
|
2023-07-10 09:23:28 +00:00
|
|
|
*/
|
|
|
|
private $stmtsManipulator;
|
2023-09-11 13:16:45 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
|
|
|
*/
|
|
|
|
private $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2023-07-10 09:23:28 +00:00
|
|
|
/**
|
|
|
|
* @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];
|
2023-09-20 12:55:35 +00:00
|
|
|
public function __construct(StmtsManipulator $stmtsManipulator, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder)
|
2023-07-10 09:23:28 +00:00
|
|
|
{
|
|
|
|
$this->stmtsManipulator = $stmtsManipulator;
|
2023-09-11 13:16:45 +00:00
|
|
|
$this->docBlockUpdater = $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2023-07-10 09:23:28 +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
|
|
|
{
|
2023-07-10 09:23:28 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
2023-07-08 21:56:17 +00:00
|
|
|
/**
|
2023-07-10 09:23:28 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2023-07-08 21:56:17 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-07-05 12:14:36 +00:00
|
|
|
{
|
2023-07-10 09:23:28 +00:00
|
|
|
if ($node->stmts === null) {
|
2019-07-05 12:14:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-07-10 09:23:28 +00:00
|
|
|
$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 = \array_merge($extractValues, \array_keys($appendExtractValues));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->shouldSkip($node, $key, $stmt, $extractValues)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);
|
|
|
|
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
|
|
|
|
if (!$varTagValueNode instanceof VarTagValueNode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->isObjectShapePseudoType($varTagValueNode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$variableName = \ltrim($varTagValueNode->variableName, '$');
|
2023-10-10 10:46:27 +00:00
|
|
|
if ($variableName === '' && $this->isAllowedEmptyVariableName($stmt)) {
|
2023-07-10 09:23:28 +00:00
|
|
|
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);
|
2023-09-11 13:16:45 +00:00
|
|
|
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
|
2023-07-10 09:23:28 +00:00
|
|
|
$hasChanged = \true;
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
2023-07-10 09:23:28 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2023-03-28 22:41:17 +00:00
|
|
|
}
|
2023-07-10 09:23:28 +00:00
|
|
|
return null;
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|
2023-07-10 09:23:28 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $extractValues
|
|
|
|
*/
|
|
|
|
private function shouldSkip(StmtsAwareInterface $stmtsAware, int $key, Stmt $stmt, array $extractValues) : bool
|
2021-09-04 07:47:01 +00:00
|
|
|
{
|
2023-07-10 09:23:28 +00:00
|
|
|
if (!\in_array(\get_class($stmt), self::NODE_TYPES, \true)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
if (\count($stmt->getComments()) !== 1) {
|
2023-07-08 21:56:17 +00:00
|
|
|
return \true;
|
2021-09-04 07:47:01 +00:00
|
|
|
}
|
2023-07-10 09:23:28 +00:00
|
|
|
foreach ($extractValues as $extractValue) {
|
|
|
|
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, $extractValue)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
2023-10-10 10:46:27 +00:00
|
|
|
return isset($stmtsAware->stmts[$key + 1]) && $stmtsAware->stmts[$key + 1] instanceof InlineHTML;
|
2021-01-19 19:11:10 +00:00
|
|
|
}
|
2023-07-08 21:56:17 +00:00
|
|
|
private function hasVariableName(Stmt $stmt, string $variableName) : bool
|
2021-01-19 19:11:10 +00:00
|
|
|
{
|
2023-07-08 21:56:17 +00:00
|
|
|
return (bool) $this->betterNodeFinder->findFirst($stmt, function (Node $node) use($variableName) : bool {
|
2022-06-07 08:22:29 +00:00
|
|
|
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-10-10 10:46:27 +00:00
|
|
|
private function isAllowedEmptyVariableName(Stmt $stmt) : bool
|
2023-03-28 22:41:17 +00:00
|
|
|
{
|
2023-10-10 10:46:27 +00:00
|
|
|
if ($stmt instanceof Return_ && $stmt->expr instanceof CallLike && !$stmt->expr instanceof New_) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $stmt instanceof Expression && $stmt->expr instanceof Assign && $stmt->expr->var instanceof Variable;
|
2023-03-28 22:41:17 +00:00
|
|
|
}
|
2019-07-05 12:14:36 +00:00
|
|
|
}
|