Updated Rector to commit ac63f2a6261963e8a7d353474b7c20311a72ded3

ac63f2a626 [NodeTypeResolver] Remove type detection from parent FunctionLike docblock on ParamTypeResolver (#4441)
This commit is contained in:
Tomas Votruba 2023-07-08 21:50:41 +00:00
parent 7912b23087
commit 8028d6a7ce
6 changed files with 79 additions and 28 deletions

View File

@ -5,15 +5,23 @@ namespace Rector\DeadCode\Rector\Node;
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\Rector\AbstractRector;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -23,6 +31,15 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer
*/
private $exprUsedInNodeAnalyzer;
public function __construct(ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer)
{
$this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Removes non-existing @var annotations above the code', [new CodeSample(<<<'CODE_SAMPLE'
@ -51,11 +68,8 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Stmt::class];
return [Foreach_::class, Static_::class, Echo_::class, Return_::class, Expression::class, Throw_::class, If_::class, While_::class, Switch_::class, Nop::class];
}
/**
* @param Stmt $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->shouldSkip($node)) {
@ -76,6 +90,9 @@ CODE_SAMPLE
if ($this->hasVariableName($node, $variableName)) {
return null;
}
if ($this->isUsedInNextNodeWithExtractPreviouslyCalled($node, $variableName)) {
return null;
}
$comments = $node->getComments();
if (isset($comments[1])) {
// skip edge case with double comment, as impossible to resolve by PHPStan doc parser
@ -84,16 +101,29 @@ CODE_SAMPLE
$phpDocInfo->removeByType(VarTagValueNode::class);
return $node;
}
private function shouldSkip(Stmt $stmt) : bool
private function isUsedInNextNodeWithExtractPreviouslyCalled(Node $node, string $variableName) : bool
{
if ($stmt instanceof ClassConst || $stmt instanceof Property) {
return \true;
$variable = new Variable($variableName);
$isUsedInNextNode = (bool) $this->betterNodeFinder->findFirstNext($node, function (Node $node) use($variable) : bool {
return $this->exprUsedInNodeAnalyzer->isUsed($node, $variable);
});
if (!$isUsedInNextNode) {
return \false;
}
return \count($stmt->getComments()) !== 1;
return (bool) $this->betterNodeFinder->findFirstPrevious($node, function (Node $subNode) : bool {
if (!$subNode instanceof FuncCall) {
return \false;
}
return $this->nodeNameResolver->isName($subNode, 'extract');
});
}
private function hasVariableName(Stmt $stmt, string $variableName) : bool
private function shouldSkip(Node $node) : bool
{
return (bool) $this->betterNodeFinder->findFirst($stmt, function (Node $node) use($variableName) : bool {
return \count($node->getComments()) !== 1;
}
private function hasVariableName(Node $node, string $variableName) : bool
{
return (bool) $this->betterNodeFinder->findFirst($node, function (Node $node) use($variableName) : bool {
if (!$node instanceof Variable) {
return \false;
}
@ -117,8 +147,8 @@ CODE_SAMPLE
}
return \strpos($varTagValueNode->description, '}') !== \false;
}
private function isAnnotatableReturn(Stmt $stmt) : bool
private function isAnnotatableReturn(Node $node) : bool
{
return $stmt instanceof Return_ && $stmt->expr instanceof CallLike && !$stmt->expr instanceof New_;
return $node instanceof Return_ && $node->expr instanceof CallLike && !$node->expr instanceof New_;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '2f1d4d3e66e896531cbfe6958307b451e972a624';
public const PACKAGE_VERSION = 'ac63f2a6261963e8a7d353474b7c20311a72ded3';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-08 21:46:15';
public const RELEASE_DATE = '2023-07-09 04:45:43';
/**
* @var int
*/

View File

@ -5,6 +5,7 @@ namespace Rector\Core\PhpParser\Node;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
@ -29,6 +30,7 @@ use PhpParser\NodeTraverser;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Exception\StopSearchException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Util\MultiInstanceofChecker;
@ -52,6 +54,11 @@ final class BetterNodeFinder
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
*/
private $nodeComparator;
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
@ -72,10 +79,11 @@ final class BetterNodeFinder
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
public function __construct(NodeFinder $nodeFinder, NodeNameResolver $nodeNameResolver, ClassAnalyzer $classAnalyzer, MultiInstanceofChecker $multiInstanceofChecker, CurrentFileProvider $currentFileProvider, SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
public function __construct(NodeFinder $nodeFinder, NodeNameResolver $nodeNameResolver, NodeComparator $nodeComparator, ClassAnalyzer $classAnalyzer, MultiInstanceofChecker $multiInstanceofChecker, CurrentFileProvider $currentFileProvider, SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
{
$this->nodeFinder = $nodeFinder;
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeComparator = $nodeComparator;
$this->classAnalyzer = $classAnalyzer;
$this->multiInstanceofChecker = $multiInstanceofChecker;
$this->currentFileProvider = $currentFileProvider;
@ -188,6 +196,19 @@ final class BetterNodeFinder
{
return $this->nodeFinder->findFirst($nodes, $filter);
}
/**
* @api symfony
* @return Assign|null
*/
public function findPreviousAssignToExpr(Expr $expr) : ?Node
{
return $this->findFirstPrevious($expr, function (Node $node) use($expr) : bool {
if (!$node instanceof Assign) {
return \false;
}
return $this->nodeComparator->areNodesEqual($node->var, $expr);
});
}
/**
* @deprecated Use nodes directly
*

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75::getLoader();
return ComposerAutoloaderInitfcdbb54769877baae0fc941439369a9e::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75
class ComposerAutoloaderInitfcdbb54769877baae0fc941439369a9e
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitfcdbb54769877baae0fc941439369a9e', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitfcdbb54769877baae0fc941439369a9e', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitfcdbb54769877baae0fc941439369a9e::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitfcdbb54769877baae0fc941439369a9e::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75
class ComposerStaticInitfcdbb54769877baae0fc941439369a9e
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3080,9 +3080,9 @@ class ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitfcdbb54769877baae0fc941439369a9e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfcdbb54769877baae0fc941439369a9e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfcdbb54769877baae0fc941439369a9e::$classMap;
}, null, ClassLoader::class);
}