mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-20 10:02:29 +00:00
Updated Rector to commit 5cc41ac787
5cc41ac787
Simplify RemoveUnreachableStatementRector to use less connecting nodes (#2125)
This commit is contained in:
parent
7e10a637a2
commit
8cf0a1bf3a
|
@ -68,10 +68,14 @@ final class AttributeKey
|
|||
*/
|
||||
public const NEXT_NODE = 'next';
|
||||
/**
|
||||
* @deprecated Instead of tree climbing without context, hook into parent node that contains the stmts directly.
|
||||
* E.g. FunctionLike, If_, While_ etc.
|
||||
* @var string
|
||||
*/
|
||||
public const PREVIOUS_STATEMENT = 'previousExpression';
|
||||
/**
|
||||
* @deprecated Instead of tree climbing without context, hook into parent node that contains the stmts directly.
|
||||
* E.g. FunctionLike, If_, While_ etc.
|
||||
* @var string
|
||||
*/
|
||||
public const CURRENT_STATEMENT = 'currentExpression';
|
||||
|
|
|
@ -8,16 +8,11 @@ use PhpParser\Node\Expr\MethodCall;
|
|||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Do_;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -29,19 +24,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class RemoveUnreachableStatementRector extends \Rector\Core\Rector\AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var array<class-string<Node>>
|
||||
*/
|
||||
private const STMTS_WITH_IS_UNREACHABLE = [\PhpParser\Node\Stmt\If_::class, \PhpParser\Node\Stmt\While_::class, \PhpParser\Node\Stmt\Do_::class];
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer
|
||||
*/
|
||||
private $inlineHTMLAnalyzer;
|
||||
public function __construct(\Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer $inlineHTMLAnalyzer)
|
||||
{
|
||||
$this->inlineHTMLAnalyzer = $inlineHTMLAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unreachable statements', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
||||
|
@ -71,96 +53,60 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [\PhpParser\Node\Stmt::class];
|
||||
return [\PhpParser\Node\Stmt\Foreach_::class, \PhpParser\Node\FunctionLike::class, \PhpParser\Node\Stmt\Else_::class, \PhpParser\Node\Stmt\If_::class];
|
||||
}
|
||||
/**
|
||||
* @param Stmt $node
|
||||
* @param Foreach_|FunctionLike|Else_|If_ $node
|
||||
*/
|
||||
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
||||
{
|
||||
if ($this->shouldSkipNode($node)) {
|
||||
$stmts = $node->stmts;
|
||||
$isPassedTheUnreachable = \false;
|
||||
$hasChanged = \false;
|
||||
foreach ($stmts as $key => $stmt) {
|
||||
if ($this->shouldSkipNode($stmt)) {
|
||||
continue;
|
||||
}
|
||||
if ($this->isUnreachable($stmt)) {
|
||||
$isPassedTheUnreachable = \true;
|
||||
}
|
||||
if ($this->isAfterMarkTestSkippedMethodCall($stmt)) {
|
||||
// keep for test case temporary ignored
|
||||
break;
|
||||
}
|
||||
if (!$isPassedTheUnreachable) {
|
||||
continue;
|
||||
}
|
||||
unset($stmts[$key]);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return null;
|
||||
}
|
||||
// might be PHPStan false positive, better skip
|
||||
$previousStatement = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_STATEMENT);
|
||||
if ($previousStatement instanceof \PhpParser\Node\Stmt && \in_array(\get_class($previousStatement), self::STMTS_WITH_IS_UNREACHABLE, \true)) {
|
||||
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE, $previousStatement->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE));
|
||||
}
|
||||
if (!$this->isUnreachable($node)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isAfterMarkTestSkippedMethodCall($node)) {
|
||||
$node->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE, \false);
|
||||
return null;
|
||||
}
|
||||
$this->removeNode($node);
|
||||
return null;
|
||||
$node->stmts = $stmts;
|
||||
return $node;
|
||||
}
|
||||
private function shouldSkipNode(\PhpParser\Node\Stmt $stmt) : bool
|
||||
{
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\Nop) {
|
||||
return \true;
|
||||
}
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\ClassLike) {
|
||||
return \true;
|
||||
}
|
||||
if ($stmt instanceof \PhpParser\Node\FunctionLike) {
|
||||
return \true;
|
||||
}
|
||||
return $this->inlineHTMLAnalyzer->hasInlineHTML($stmt);
|
||||
return $stmt instanceof \PhpParser\Node\Stmt\Nop;
|
||||
}
|
||||
private function isUnreachable(\PhpParser\Node\Stmt $stmt) : bool
|
||||
{
|
||||
$isUnreachable = $stmt->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE);
|
||||
if ($isUnreachable === \true) {
|
||||
return \true;
|
||||
}
|
||||
// traverse up for unreachable node in the same scope
|
||||
$previousNode = $stmt->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_STATEMENT);
|
||||
while ($previousNode instanceof \PhpParser\Node\Stmt && !$this->isBreakingScopeNode($previousNode)) {
|
||||
$isUnreachable = $previousNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE);
|
||||
if ($isUnreachable === \true) {
|
||||
return \true;
|
||||
}
|
||||
$previousNode = $previousNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_STATEMENT);
|
||||
}
|
||||
return \false;
|
||||
return $isUnreachable === \true;
|
||||
}
|
||||
/**
|
||||
* Keep content after markTestSkipped(), intentional temporary
|
||||
*/
|
||||
private function isAfterMarkTestSkippedMethodCall(\PhpParser\Node\Stmt $stmt) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirstPrevious($stmt, function (\PhpParser\Node $node) : bool {
|
||||
if (!$node instanceof \PhpParser\Node\Expr\MethodCall && !$node instanceof \PhpParser\Node\Expr\StaticCall) {
|
||||
return \false;
|
||||
}
|
||||
if ($node->name instanceof \PhpParser\Node\Expr\MethodCall) {
|
||||
return \false;
|
||||
}
|
||||
if ($node->name instanceof \PhpParser\Node\Expr\StaticCall) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isNames($node->name, ['markTestSkipped', 'markTestIncomplete']);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Check nodes that breaks scope while traversing up
|
||||
*/
|
||||
private function isBreakingScopeNode(\PhpParser\Node\Stmt $stmt) : bool
|
||||
{
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\ClassLike) {
|
||||
return \true;
|
||||
if (!$stmt instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
return \false;
|
||||
}
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
||||
return \true;
|
||||
$expr = $stmt->expr;
|
||||
if ($expr instanceof \PhpParser\Node\Expr\MethodCall || $expr instanceof \PhpParser\Node\Expr\StaticCall) {
|
||||
return $this->isNames($expr->name, ['markTestSkipped', 'markTestIncomplete']);
|
||||
}
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\Function_) {
|
||||
return \true;
|
||||
}
|
||||
if ($stmt instanceof \PhpParser\Node\Stmt\Namespace_) {
|
||||
return \true;
|
||||
}
|
||||
return $stmt instanceof \PhpParser\Node\Stmt\Else_;
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '3ddac18c51a77b07caae35d538b88bad0004166a';
|
||||
public const PACKAGE_VERSION = '5cc41ac787fcfcc7a10dbee7645207d839e75982';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-04-22 16:22:21';
|
||||
public const RELEASE_DATE = '2022-04-22 17:16:42';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6::getLoader();
|
||||
return ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47::getLoader();
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6
|
||||
class ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitaf895ef74cf81af30efd49802f044a47::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitaf895ef74cf81af30efd49802f044a47::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequiref559d986824a8d346f6f1a2ea2b93fe6($fileIdentifier, $file);
|
||||
composerRequireaf895ef74cf81af30efd49802f044a47($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequiref559d986824a8d346f6f1a2ea2b93fe6($fileIdentifier, $file)
|
||||
function composerRequireaf895ef74cf81af30efd49802f044a47($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 ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6
|
||||
class ComposerStaticInitaf895ef74cf81af30efd49802f044a47
|
||||
{
|
||||
public static $files = array (
|
||||
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
|
||||
|
@ -3869,9 +3869,9 @@ class ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitf559d986824a8d346f6f1a2ea2b93fe6::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitaf895ef74cf81af30efd49802f044a47::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitaf895ef74cf81af30efd49802f044a47::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitaf895ef74cf81af30efd49802f044a47::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
|
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
|||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20220422\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6', false) && !interface_exists('ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6', false) && !trait_exists('ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6', false)) {
|
||||
spl_autoload_call('RectorPrefix20220422\ComposerAutoloaderInitf559d986824a8d346f6f1a2ea2b93fe6');
|
||||
if (!class_exists('ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47', false) && !interface_exists('ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47', false) && !trait_exists('ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47', false)) {
|
||||
spl_autoload_call('RectorPrefix20220422\ComposerAutoloaderInitaf895ef74cf81af30efd49802f044a47');
|
||||
}
|
||||
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
|
||||
spl_autoload_call('RectorPrefix20220422\Helmich\TypoScriptParser\Parser\AST\Statement');
|
||||
|
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
|
|||
return \RectorPrefix20220422\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequiref559d986824a8d346f6f1a2ea2b93fe6')) {
|
||||
function composerRequiref559d986824a8d346f6f1a2ea2b93fe6() {
|
||||
return \RectorPrefix20220422\composerRequiref559d986824a8d346f6f1a2ea2b93fe6(...func_get_args());
|
||||
if (!function_exists('composerRequireaf895ef74cf81af30efd49802f044a47')) {
|
||||
function composerRequireaf895ef74cf81af30efd49802f044a47() {
|
||||
return \RectorPrefix20220422\composerRequireaf895ef74cf81af30efd49802f044a47(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('scanPath')) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user