Updated Rector to commit 5b64258e7c9628d80fa3247a698c646def55dca4

5b64258e7c [DeadCode] Skip used defined in extract() on RemoveNonExistingVarAnnotationRector (#4468)
This commit is contained in:
Tomas Votruba 2023-07-10 09:23:28 +00:00
parent 3f51587157
commit 2d46a8afb9
5 changed files with 94 additions and 40 deletions

View File

@ -5,14 +5,24 @@ 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\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;
@ -23,6 +33,19 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\StmtsManipulator
*/
private $stmtsManipulator;
/**
* @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(StmtsManipulator $stmtsManipulator)
{
$this->stmtsManipulator = $stmtsManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Removes non-existing @var annotations above the code', [new CodeSample(<<<'CODE_SAMPLE'
@ -51,45 +74,76 @@ CODE_SAMPLE
*/
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);
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (!$varTagValueNode instanceof VarTagValueNode) {
return null;
$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, '$');
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;
}
if ($this->isObjectShapePseudoType($varTagValueNode)) {
return null;
if ($hasChanged) {
return $node;
}
$variableName = \ltrim($varTagValueNode->variableName, '$');
if ($variableName === '' && $this->isAnnotatableReturn($node)) {
return null;
}
if ($this->hasVariableName($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
return null;
}
$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(\get_class($stmt), self::NODE_TYPES, \true)) {
return \true;
}
return \count($stmt->getComments()) !== 1;
if (\count($stmt->getComments()) !== 1) {
return \true;
}
foreach ($extractValues as $extractValue) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmtsAware, $key + 1, $extractValue)) {
return \true;
}
}
return \false;
}
private function hasVariableName(Stmt $stmt, string $variableName) : bool
{

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '3cc636540bccf0fc647ff13e1bea212c6bfa4962';
public const PACKAGE_VERSION = '5b64258e7c9628d80fa3247a698c646def55dca4';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-10 06:51:00';
public const RELEASE_DATE = '2023-07-10 16:18:57';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit49c0abc6a3d0e96b022449e1919ce840
class ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit49c0abc6a3d0e96b022449e1919ce840
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit49c0abc6a3d0e96b022449e1919ce840', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit49c0abc6a3d0e96b022449e1919ce840', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit730acd082dbcead102e4118e2e4c898f', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit730acd082dbcead102e4118e2e4c898f::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$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 ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840
class ComposerStaticInit730acd082dbcead102e4118e2e4c898f
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3067,9 +3067,9 @@ class ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit49c0abc6a3d0e96b022449e1919ce840::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit730acd082dbcead102e4118e2e4c898f::$classMap;
}, null, ClassLoader::class);
}