mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
Updated Rector to commit 79b25e861d51dd85764420a94fe56343b01b1a04
79b25e861d
[DeadCode] Handle always terminated If_ ElseIf_[] Else_ on RemoveUnreachableStatementRector (#2547)
This commit is contained in:
parent
7547ec311c
commit
8c3b690acb
@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\Break_;
|
||||
use PhpParser\Node\Stmt\Continue_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Goto_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\InlineHTML;
|
||||
use PhpParser\Node\Stmt\Label;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
@ -17,7 +18,7 @@ use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\NodeAnalyzer\TryCatchAnalyzer;
|
||||
use Rector\Core\NodeAnalyzer\TerminatedNodeAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
@ -30,12 +31,12 @@ final class RemoveUnreachableStatementRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\TryCatchAnalyzer
|
||||
* @var \Rector\Core\NodeAnalyzer\TerminatedNodeAnalyzer
|
||||
*/
|
||||
private $tryCatchAnalyzer;
|
||||
public function __construct(TryCatchAnalyzer $tryCatchAnalyzer)
|
||||
private $terminatedNodeAnalyzer;
|
||||
public function __construct(TerminatedNodeAnalyzer $terminatedNodeAnalyzer)
|
||||
{
|
||||
$this->tryCatchAnalyzer = $tryCatchAnalyzer;
|
||||
$this->terminatedNodeAnalyzer = $terminatedNodeAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -123,9 +124,12 @@ CODE_SAMPLE
|
||||
if (\in_array(\get_class($previousStmt), [Return_::class, Break_::class, Continue_::class, Goto_::class], \true)) {
|
||||
return \true;
|
||||
}
|
||||
if (!$previousStmt instanceof TryCatch) {
|
||||
return \false;
|
||||
if ($previousStmt instanceof TryCatch) {
|
||||
return $this->terminatedNodeAnalyzer->isAlwaysTerminated($previousStmt);
|
||||
}
|
||||
return $this->tryCatchAnalyzer->isAlwaysTerminated($previousStmt);
|
||||
if ($previousStmt instanceof If_) {
|
||||
return $this->terminatedNodeAnalyzer->isAlwaysTerminated($previousStmt);
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,9 @@ use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use PHPStan\Analyser\MutatingScope;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
@ -109,6 +111,12 @@ final class ChangedNodeScopeRefresher
|
||||
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
|
||||
$node->args = \array_values($node->args);
|
||||
}
|
||||
if ($node instanceof If_) {
|
||||
$node->elseifs = \array_values($node->elseifs);
|
||||
}
|
||||
if ($node instanceof TryCatch) {
|
||||
$node->catches = \array_values($node->catches);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return Stmt[]
|
||||
|
@ -16,11 +16,11 @@ final class VersionResolver
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'dd4064e431f6876e706c7615bee5e01e11a71044';
|
||||
public const PACKAGE_VERSION = '79b25e861d51dd85764420a94fe56343b01b1a04';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-06-21 07:59:38';
|
||||
public const RELEASE_DATE = '2022-06-21 12:12:55';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
75
src/NodeAnalyzer/TerminatedNodeAnalyzer.php
Normal file
75
src/NodeAnalyzer/TerminatedNodeAnalyzer.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Exit_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Finally_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
final class TerminatedNodeAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var array<class-string<Node>>
|
||||
*/
|
||||
private const TERMINATED_NODES = [Return_::class, Throw_::class];
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\TryCatch|\PhpParser\Node\Stmt\If_ $node
|
||||
*/
|
||||
public function isAlwaysTerminated($node) : bool
|
||||
{
|
||||
if ($node instanceof TryCatch) {
|
||||
if ($node->finally instanceof Finally_ && $this->isTerminated($node->finally->stmts)) {
|
||||
return \true;
|
||||
}
|
||||
foreach ($node->catches as $catch) {
|
||||
if (!$this->isTerminated($catch->stmts)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return $this->isTerminated($node->stmts);
|
||||
}
|
||||
return $this->isTerminatedIf($node);
|
||||
}
|
||||
private function isTerminatedIf(If_ $if) : bool
|
||||
{
|
||||
// Without ElseIf_[] and Else_, after If_ is possibly executable
|
||||
if ($if->elseifs === [] && !$if->else instanceof Else_) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($if->elseifs as $elseIf) {
|
||||
if (!$this->isTerminated($elseIf->stmts)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
if (!$this->isTerminated($if->stmts)) {
|
||||
return \false;
|
||||
}
|
||||
if (!$if->else instanceof Else_) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isTerminated($if->else->stmts);
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function isTerminated(array $stmts) : bool
|
||||
{
|
||||
if ($stmts === []) {
|
||||
return \false;
|
||||
}
|
||||
\end($stmts);
|
||||
$lastKey = \key($stmts);
|
||||
$lastNode = $stmts[$lastKey];
|
||||
if ($lastNode instanceof Expression) {
|
||||
return $lastNode->expr instanceof Exit_;
|
||||
}
|
||||
return \in_array(\get_class($lastNode), self::TERMINATED_NODES, \true);
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Exit_;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Finally_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
final class TryCatchAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var array<class-string<Node>>
|
||||
*/
|
||||
private const TERMINATED_NODES = [Return_::class, Throw_::class];
|
||||
public function isAlwaysTerminated(TryCatch $tryCatch) : bool
|
||||
{
|
||||
if ($tryCatch->finally instanceof Finally_ && $this->isTerminated($tryCatch->finally->stmts)) {
|
||||
return \true;
|
||||
}
|
||||
foreach ($tryCatch->catches as $catch) {
|
||||
if (!$this->isTerminated($catch->stmts)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return $this->isTerminated($tryCatch->stmts);
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function isTerminated(array $stmts) : bool
|
||||
{
|
||||
if ($stmts === []) {
|
||||
return \false;
|
||||
}
|
||||
\end($stmts);
|
||||
$lastKey = \key($stmts);
|
||||
$lastNode = $stmts[$lastKey];
|
||||
if ($lastNode instanceof Expression) {
|
||||
return $lastNode->expr instanceof Exit_;
|
||||
}
|
||||
return \in_array(\get_class($lastNode), self::TERMINATED_NODES, \true);
|
||||
}
|
||||
}
|
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 ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a::getLoader();
|
||||
return ComposerAutoloaderInitb483f0d66e61241d4b285b48b4d1653c::getLoader();
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -1570,7 +1570,7 @@ return array(
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyFetchAnalyzer' => $baseDir . '/src/NodeAnalyzer/PropertyFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyPresenceChecker' => $baseDir . '/src/NodeAnalyzer/PropertyPresenceChecker.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ScopeAnalyzer' => $baseDir . '/src/NodeAnalyzer/ScopeAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\TryCatchAnalyzer' => $baseDir . '/src/NodeAnalyzer/TryCatchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\TerminatedNodeAnalyzer' => $baseDir . '/src/NodeAnalyzer/TerminatedNodeAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\UnreachableStmtAnalyzer' => $baseDir . '/src/NodeAnalyzer/UnreachableStmtAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\VariableAnalyzer' => $baseDir . '/src/NodeAnalyzer/VariableAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\VariadicAnalyzer' => $baseDir . '/src/NodeAnalyzer/VariadicAnalyzer.php',
|
||||
|
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 ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
class ComposerAutoloaderInitb483f0d66e61241d4b285b48b4d1653c
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,19 +22,19 @@ class ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb483f0d66e61241d4b285b48b4d1653c', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb483f0d66e61241d4b285b48b4d1653c', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire0a7f76ca8cdee7bbe3371a273e17609a($fileIdentifier, $file);
|
||||
composerRequireb483f0d66e61241d4b285b48b4d1653c($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
@ -46,7 +46,7 @@ class ComposerAutoloaderInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire0a7f76ca8cdee7bbe3371a273e17609a($fileIdentifier, $file)
|
||||
function composerRequireb483f0d66e61241d4b285b48b4d1653c($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
10
vendor/composer/autoload_static.php
vendored
10
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
class ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
@ -1871,7 +1871,7 @@ class ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/PropertyFetchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\PropertyPresenceChecker' => __DIR__ . '/../..' . '/src/NodeAnalyzer/PropertyPresenceChecker.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\ScopeAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/ScopeAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\TryCatchAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/TryCatchAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\TerminatedNodeAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/TerminatedNodeAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\UnreachableStmtAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/UnreachableStmtAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\VariableAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/VariableAnalyzer.php',
|
||||
'Rector\\Core\\NodeAnalyzer\\VariadicAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/VariadicAnalyzer.php',
|
||||
@ -3401,9 +3401,9 @@ class ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit0a7f76ca8cdee7bbe3371a273e17609a::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb483f0d66e61241d4b285b48b4d1653c::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user