mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit 2f1d4d3e66e896531cbfe6958307b451e972a624
2f1d4d3e66
Make RemoveNonExistingVarAnnotationRector simpler, skip compact() check as not generic (#4439)
This commit is contained in:
parent
0ae22238e2
commit
7912b23087
@ -4,16 +4,10 @@ declare (strict_types=1);
|
||||
namespace Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Param;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
use RectorPrefix202307\Symfony\Contracts\Service\Attribute\Required;
|
||||
/**
|
||||
@ -23,25 +17,10 @@ use RectorPrefix202307\Symfony\Contracts\Service\Attribute\Required;
|
||||
*/
|
||||
final class ParamTypeResolver implements NodeTypeResolverInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
/**
|
||||
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
||||
*/
|
||||
private $nodeTypeResolver;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, PhpDocInfoFactory $phpDocInfoFactory)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
}
|
||||
/**
|
||||
* @required
|
||||
*/
|
||||
@ -61,31 +40,9 @@ final class ParamTypeResolver implements NodeTypeResolverInterface
|
||||
*/
|
||||
public function resolve(Node $node) : Type
|
||||
{
|
||||
$paramType = $this->resolveFromParamType($node);
|
||||
if (!$paramType instanceof MixedType) {
|
||||
return $paramType;
|
||||
}
|
||||
return $this->resolveFromFunctionDocBlock($node);
|
||||
}
|
||||
private function resolveFromParamType(Param $param) : Type
|
||||
{
|
||||
if ($param->type === null) {
|
||||
if ($node->type === null) {
|
||||
return new MixedType();
|
||||
}
|
||||
return $this->nodeTypeResolver->getType($param->type);
|
||||
}
|
||||
private function resolveFromFunctionDocBlock(Param $param) : Type
|
||||
{
|
||||
$phpDocInfo = $this->getFunctionLikePhpDocInfo($param);
|
||||
$paramName = $this->nodeNameResolver->getName($param);
|
||||
return $phpDocInfo->getParamType($paramName);
|
||||
}
|
||||
private function getFunctionLikePhpDocInfo(Param $param) : PhpDocInfo
|
||||
{
|
||||
$parentNode = $param->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof FunctionLike) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
return $this->phpDocInfoFactory->createFromNodeOrEmpty($parentNode);
|
||||
return $this->nodeTypeResolver->getType($node->type);
|
||||
}
|
||||
}
|
||||
|
@ -5,23 +5,15 @@ 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\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;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
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;
|
||||
/**
|
||||
@ -31,15 +23,6 @@ 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'
|
||||
@ -68,8 +51,11 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Foreach_::class, Static_::class, Echo_::class, Return_::class, Expression::class, Throw_::class, If_::class, While_::class, Switch_::class, Nop::class];
|
||||
return [Stmt::class];
|
||||
}
|
||||
/**
|
||||
* @param Stmt $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
@ -90,9 +76,6 @@ 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
|
||||
@ -101,29 +84,16 @@ CODE_SAMPLE
|
||||
$phpDocInfo->removeByType(VarTagValueNode::class);
|
||||
return $node;
|
||||
}
|
||||
private function isUsedInNextNodeWithExtractPreviouslyCalled(Node $node, string $variableName) : bool
|
||||
private function shouldSkip(Stmt $stmt) : bool
|
||||
{
|
||||
$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;
|
||||
if ($stmt instanceof ClassConst || $stmt instanceof Property) {
|
||||
return \true;
|
||||
}
|
||||
return (bool) $this->betterNodeFinder->findFirstPrevious($node, function (Node $subNode) : bool {
|
||||
if (!$subNode instanceof FuncCall) {
|
||||
return \false;
|
||||
}
|
||||
return $this->nodeNameResolver->isName($subNode, 'extract');
|
||||
});
|
||||
return \count($stmt->getComments()) !== 1;
|
||||
}
|
||||
private function shouldSkip(Node $node) : bool
|
||||
private function hasVariableName(Stmt $stmt, string $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 {
|
||||
return (bool) $this->betterNodeFinder->findFirst($stmt, function (Node $node) use($variableName) : bool {
|
||||
if (!$node instanceof Variable) {
|
||||
return \false;
|
||||
}
|
||||
@ -147,8 +117,8 @@ CODE_SAMPLE
|
||||
}
|
||||
return \strpos($varTagValueNode->description, '}') !== \false;
|
||||
}
|
||||
private function isAnnotatableReturn(Node $node) : bool
|
||||
private function isAnnotatableReturn(Stmt $stmt) : bool
|
||||
{
|
||||
return $node instanceof Return_ && $node->expr instanceof CallLike && !$node->expr instanceof New_;
|
||||
return $stmt instanceof Return_ && $stmt->expr instanceof CallLike && !$stmt->expr instanceof New_;
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '3c90b2d0ec9aef15c1d869ceadf1dfeaea8235ce';
|
||||
public const PACKAGE_VERSION = '2f1d4d3e66e896531cbfe6958307b451e972a624';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-07-08 22:42:42';
|
||||
public const RELEASE_DATE = '2023-07-08 21:46:15';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -5,7 +5,6 @@ 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;
|
||||
@ -30,7 +29,6 @@ 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;
|
||||
@ -54,11 +52,6 @@ final class BetterNodeFinder
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
||||
*/
|
||||
private $nodeComparator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
|
||||
@ -79,11 +72,10 @@ final class BetterNodeFinder
|
||||
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
|
||||
*/
|
||||
private $simpleCallableNodeTraverser;
|
||||
public function __construct(NodeFinder $nodeFinder, NodeNameResolver $nodeNameResolver, NodeComparator $nodeComparator, ClassAnalyzer $classAnalyzer, MultiInstanceofChecker $multiInstanceofChecker, CurrentFileProvider $currentFileProvider, SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
|
||||
public function __construct(NodeFinder $nodeFinder, NodeNameResolver $nodeNameResolver, 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;
|
||||
@ -196,19 +188,6 @@ 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
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit769b5c3b84fa1409afc39b8b32b13410::getLoader();
|
||||
return ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75::getLoader();
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit769b5c3b84fa1409afc39b8b32b13410
|
||||
class ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInit769b5c3b84fa1409afc39b8b32b13410
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit769b5c3b84fa1409afc39b8b32b13410', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit769b5c3b84fa1409afc39b8b32b13410', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit2e73d182de7c828e6167abacb6ba4f75', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410
|
||||
class ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -3080,9 +3080,9 @@ class ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit769b5c3b84fa1409afc39b8b32b13410::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit2e73d182de7c828e6167abacb6ba4f75::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user