Skip to content

Commit

Permalink
Updated Rector to commit 6c713a7
Browse files Browse the repository at this point in the history
rectorphp/rector-src@6c713a7 [DeadCode] Handle always terminated TryCatch on RemoveUnreachableStatementRector (#2545)
  • Loading branch information
TomasVotruba committed Jun 21, 2022
1 parent 9550950 commit bb396cb
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 49 deletions.
31 changes: 11 additions & 20 deletions rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\Goto_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Label;
Expand All @@ -18,8 +17,8 @@
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\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
Expand All @@ -29,6 +28,15 @@
*/
final class RemoveUnreachableStatementRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\TryCatchAnalyzer
*/
private $tryCatchAnalyzer;
public function __construct(TryCatchAnalyzer $tryCatchAnalyzer)
{
$this->tryCatchAnalyzer = $tryCatchAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove unreachable statements', [new CodeSample(<<<'CODE_SAMPLE'
Expand Down Expand Up @@ -118,23 +126,6 @@ private function shouldRemove(Stmt $previousStmt, Stmt $currentStmt) : bool
if (!$previousStmt instanceof TryCatch) {
return \false;
}
$isUnreachable = $currentStmt->getAttribute(AttributeKey::IS_UNREACHABLE);
if ($isUnreachable !== \true) {
return \false;
}
if (!$previousStmt->finally instanceof Finally_) {
return \false;
}
return $this->cleanNop($previousStmt->finally->stmts) !== [];
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function cleanNop(array $stmts) : array
{
return \array_filter($stmts, function (Stmt $stmt) : bool {
return !$stmt instanceof Nop;
});
return $this->tryCatchAnalyzer->isAlwaysTerminated($previousStmt);
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'a16bc1b5762df29a2f71ddab8acfd7684205d8ce';
public const PACKAGE_VERSION = '6c713a776c54053537fcca97630d2da154d95514';
/**
* @var string
*/
public const RELEASE_DATE = '2022-06-20 15:05:45';
public const RELEASE_DATE = '2022-06-21 08:29:08';
/**
* @var int
*/
Expand Down
48 changes: 48 additions & 0 deletions src/NodeAnalyzer/TryCatchAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitc78cb6afb27140288b663d1dae738aed::getLoader();
return ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055::getLoader();
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@
'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\\UnreachableStmtAnalyzer' => $baseDir . '/src/NodeAnalyzer/UnreachableStmtAnalyzer.php',
'Rector\\Core\\NodeAnalyzer\\VariableAnalyzer' => $baseDir . '/src/NodeAnalyzer/VariableAnalyzer.php',
'Rector\\Core\\NodeAnalyzer\\VariadicAnalyzer' => $baseDir . '/src/NodeAnalyzer/VariadicAnalyzer.php',
Expand Down
14 changes: 7 additions & 7 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInitc78cb6afb27140288b663d1dae738aed
class ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055
{
private static $loader;

Expand All @@ -22,19 +22,19 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInitc78cb6afb27140288b663d1dae738aed', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitc78cb6afb27140288b663d1dae738aed', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitc78cb6afb27140288b663d1dae738aed::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit73d382890f12bde41f2d8fcab5746055::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$includeFiles = \Composer\Autoload\ComposerStaticInitc78cb6afb27140288b663d1dae738aed::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirec78cb6afb27140288b663d1dae738aed($fileIdentifier, $file);
composerRequire73d382890f12bde41f2d8fcab5746055($fileIdentifier, $file);
}

return $loader;
Expand All @@ -46,7 +46,7 @@ public static function getLoader()
* @param string $file
* @return void
*/
function composerRequirec78cb6afb27140288b663d1dae738aed($fileIdentifier, $file)
function composerRequire73d382890f12bde41f2d8fcab5746055($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
9 changes: 5 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInitc78cb6afb27140288b663d1dae738aed
class ComposerStaticInit73d382890f12bde41f2d8fcab5746055
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
Expand Down Expand Up @@ -1871,6 +1871,7 @@ class ComposerStaticInitc78cb6afb27140288b663d1dae738aed
'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\\UnreachableStmtAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/UnreachableStmtAnalyzer.php',
'Rector\\Core\\NodeAnalyzer\\VariableAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/VariableAnalyzer.php',
'Rector\\Core\\NodeAnalyzer\\VariadicAnalyzer' => __DIR__ . '/../..' . '/src/NodeAnalyzer/VariadicAnalyzer.php',
Expand Down Expand Up @@ -3399,9 +3400,9 @@ class ComposerStaticInitc78cb6afb27140288b663d1dae738aed
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitc78cb6afb27140288b663d1dae738aed::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc78cb6afb27140288b663d1dae738aed::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc78cb6afb27140288b663d1dae738aed::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$classMap;

}, null, ClassLoader::class);
}
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -2347,12 +2347,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
"reference": "c730231e908be8ab2bfd98ff27c0c06c92e24fa7"
"reference": "66f0fd13e2928cc1882a323ad7bd57a73d3eaf66"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/c730231e908be8ab2bfd98ff27c0c06c92e24fa7",
"reference": "c730231e908be8ab2bfd98ff27c0c06c92e24fa7",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/66f0fd13e2928cc1882a323ad7bd57a73d3eaf66",
"reference": "66f0fd13e2928cc1882a323ad7bd57a73d3eaf66",
"shasum": ""
},
"require": {
Expand Down Expand Up @@ -2381,7 +2381,7 @@
"symplify\/rule-doc-generator": "^11.0",
"symplify\/vendor-patches": "^11.0"
},
"time": "2022-06-20T17:45:14+00:00",
"time": "2022-06-20T22:35:15+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion vendor/composer/installed.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion vendor/rector/extension-installer/src/GeneratedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-cakephp' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-cakephp', 'relative_install_path' => '../../rector-cakephp', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 527dc23'), 'rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main cc103d0'), 'rector/rector-generator' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-generator', 'relative_install_path' => '../../rector-generator', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main cca34de'), 'rector/rector-laravel' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-laravel', 'relative_install_path' => '../../rector-laravel', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a2e37f3'), 'rector/rector-nette' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-nette', 'relative_install_path' => '../../rector-nette', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 3ea1ed7'), 'rector/rector-phpoffice' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpoffice', 'relative_install_path' => '../../rector-phpoffice', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main fdbecca'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 3f4cc81'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main c730231'));
public const EXTENSIONS = array('rector/rector-cakephp' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-cakephp', 'relative_install_path' => '../../rector-cakephp', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 527dc23'), 'rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main cc103d0'), 'rector/rector-generator' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-generator', 'relative_install_path' => '../../rector-generator', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main cca34de'), 'rector/rector-laravel' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-laravel', 'relative_install_path' => '../../rector-laravel', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a2e37f3'), 'rector/rector-nette' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-nette', 'relative_install_path' => '../../rector-nette', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 3ea1ed7'), 'rector/rector-phpoffice' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpoffice', 'relative_install_path' => '../../rector-phpoffice', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main fdbecca'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 3f4cc81'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 66f0fd1'));
private function __construct()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector\MagicClosureTwigExtensionToNativeMethodsRectorTest
*
* @see PHP 8.1 way to handle functions/filters https://github.com/symfony/symfony/blob/e0ad2eead3513a558c09d8aa3ae9e867fb10b419/src/Symfony/Bridge/Twig/Extension/CodeExtension.php#L41-L52
*/
final class MagicClosureTwigExtensionToNativeMethodsRector extends AbstractRector
{
Expand All @@ -38,17 +40,11 @@ final class MagicClosureTwigExtensionToNativeMethodsRector extends AbstractRecto
* @var \Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher
*/
private $arrayCallableMethodMatcher;
/**
* @readonly
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
*/
private $visibilityManipulator;
public function __construct(AnonymousFunctionFactory $anonymousFunctionFactory, ReflectionResolver $reflectionResolver, ArrayCallableMethodMatcher $arrayCallableMethodMatcher, VisibilityManipulator $visibilityManipulator)
public function __construct(AnonymousFunctionFactory $anonymousFunctionFactory, ReflectionResolver $reflectionResolver, ArrayCallableMethodMatcher $arrayCallableMethodMatcher)
{
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
$this->reflectionResolver = $reflectionResolver;
$this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher;
$this->visibilityManipulator = $visibilityManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
Expand Down Expand Up @@ -147,8 +143,6 @@ private function refactorClassMethod(Class_ $class, ClassMethod $classMethod) :
// inline and remove method
$closure->stmts = $localClassMethod->stmts;
$this->removeNode($localClassMethod);
} else {
$this->visibilityManipulator->makePrivate($localClassMethod);
}
}
$hasChanged = \true;
Expand Down

0 comments on commit bb396cb

Please sign in to comment.