mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
Updated Rector to commit 2dccbb6b176a836e174babbaf6611a0d4730c55c
2dccbb6b17
Remove removeNode() from RemoveDeadReturnRector, merge RemoveLastReturnRector to RemoveDeadReturnRector (#4085)
This commit is contained in:
parent
fc28a000c6
commit
54b25e4291
@ -13,7 +13,6 @@ use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
|
||||
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveLastReturnRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector;
|
||||
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
|
||||
@ -90,7 +89,6 @@ return static function (RectorConfig $rectorConfig) : void {
|
||||
RemoveNonExistingVarAnnotationRector::class,
|
||||
RemoveUselessVarTagRector::class,
|
||||
RemoveUnusedPromotedPropertyRector::class,
|
||||
RemoveLastReturnRector::class,
|
||||
RemoveJustPropertyFetchForAssignRector::class,
|
||||
RemoveJustVariableAssignRector::class,
|
||||
RemoveAlwaysTrueIfConditionRector::class,
|
||||
|
@ -1,4 +1,4 @@
|
||||
# 384 Rules Overview
|
||||
# 383 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
- [CodingStyle](#codingstyle) (34)
|
||||
|
||||
- [DeadCode](#deadcode) (45)
|
||||
- [DeadCode](#deadcode) (44)
|
||||
|
||||
- [DependencyInjection](#dependencyinjection) (2)
|
||||
|
||||
@ -2914,27 +2914,6 @@ Remove variable just to assign value or return value
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveLastReturnRector
|
||||
|
||||
Remove very last `return` that has no meaning
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveLastReturnRector`](../rules/DeadCode/Rector/ClassMethod/RemoveLastReturnRector.php)
|
||||
|
||||
```diff
|
||||
function some_function($value)
|
||||
{
|
||||
if ($value === 1000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
- return;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveNonExistingVarAnnotationRector
|
||||
|
||||
Removes non-existing `@var` annotations above the code
|
||||
|
@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeNestingScope\ContextAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\RemoveLastReturnRectorTest
|
||||
*/
|
||||
final class RemoveLastReturnRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeNestingScope\ContextAnalyzer
|
||||
*/
|
||||
private $contextAnalyzer;
|
||||
public function __construct(ContextAnalyzer $contextAnalyzer)
|
||||
{
|
||||
$this->contextAnalyzer = $contextAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove very last `return` that has no meaning', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
function some_function($value)
|
||||
{
|
||||
if ($value === 1000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
function some_function($value)
|
||||
{
|
||||
if ($value === 1000) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, Function_::class, Closure::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|Function_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// last node and last return
|
||||
$lastNode = $this->betterNodeFinder->findLastInstanceOf((array) $node->stmts, Node::class);
|
||||
$lastReturn = $this->betterNodeFinder->findLastInstanceOf((array) $node->stmts, Return_::class);
|
||||
if (!$lastReturn instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($lastNode !== $lastReturn) {
|
||||
return null;
|
||||
}
|
||||
if ($this->contextAnalyzer->isInLoop($lastReturn)) {
|
||||
return null;
|
||||
}
|
||||
$this->removeNode($lastReturn);
|
||||
return null;
|
||||
}
|
||||
}
|
@ -7,7 +7,9 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
@ -61,21 +63,43 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->stmts === []) {
|
||||
if ($node->stmts === [] || $node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
\end($node->stmts);
|
||||
$lastStmtKey = \key($node->stmts);
|
||||
$lastStmt = $node->stmts[$lastStmtKey];
|
||||
if ($lastStmt instanceof If_) {
|
||||
if (!$this->isBareIfWithOnlyStmtEmptyReturn($lastStmt)) {
|
||||
return null;
|
||||
}
|
||||
$lastStmt->stmts = [];
|
||||
return $node;
|
||||
}
|
||||
$stmtValues = \array_values($node->stmts);
|
||||
$lastStmt = \end($stmtValues);
|
||||
if (!$lastStmt instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
if ($lastStmt->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
$this->removeNode($lastStmt);
|
||||
unset($node->stmts[$lastStmtKey]);
|
||||
return $node;
|
||||
}
|
||||
private function isBareIfWithOnlyStmtEmptyReturn(If_ $if) : bool
|
||||
{
|
||||
if ($if->else instanceof Else_) {
|
||||
return \false;
|
||||
}
|
||||
if ($if->elseifs !== []) {
|
||||
return \false;
|
||||
}
|
||||
if (\count($if->stmts) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
$onlyStmt = $if->stmts[0];
|
||||
if (!$onlyStmt instanceof Return_) {
|
||||
return \false;
|
||||
}
|
||||
return !$onlyStmt->expr instanceof Expr;
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'cdc9e6b6d8ce2446e8860016997ad37e8d3b7d44';
|
||||
public const PACKAGE_VERSION = '2dccbb6b176a836e174babbaf6611a0d4730c55c';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-06-05 13:42:45';
|
||||
public const RELEASE_DATE = '2023-06-05 13:57:36';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@ final class RectorKernel
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const CACHE_KEY = 'v55';
|
||||
private const CACHE_KEY = 'v56';
|
||||
/**
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
|
||||
*/
|
||||
|
@ -205,6 +205,7 @@ final class BetterNodeFinder
|
||||
return \false;
|
||||
}
|
||||
/**
|
||||
* @api used in Symfony
|
||||
* @template T of Node
|
||||
*
|
||||
* @param Stmt[] $nodes
|
||||
|
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 ComposerAutoloaderInit81fb50a001fc3a5b67552be5bc7095a0::getLoader();
|
||||
return ComposerAutoloaderInit4c14e8bace132daaa89443888a4a21a7::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1583,7 +1583,6 @@ return array(
|
||||
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector' => $baseDir . '/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveDelegatingParentCallRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveEmptyClassMethodRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveLastReturnRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveLastReturnRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedConstructorParamRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodParameterRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodParameterRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php',
|
||||
|
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 ComposerAutoloaderInit81fb50a001fc3a5b67552be5bc7095a0
|
||||
class ComposerAutoloaderInit4c14e8bace132daaa89443888a4a21a7
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInit81fb50a001fc3a5b67552be5bc7095a0
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit81fb50a001fc3a5b67552be5bc7095a0', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit4c14e8bace132daaa89443888a4a21a7', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit81fb50a001fc3a5b67552be5bc7095a0', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit4c14e8bace132daaa89443888a4a21a7', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit4c14e8bace132daaa89443888a4a21a7::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit4c14e8bace132daaa89443888a4a21a7::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0
|
||||
class ComposerStaticInit4c14e8bace132daaa89443888a4a21a7
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -1825,7 +1825,6 @@ class ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0
|
||||
'Rector\\DeadCode\\Rector\\ClassLike\\RemoveAnnotationRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassLike/RemoveAnnotationRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveDelegatingParentCallRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveEmptyClassMethodRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveLastReturnRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveLastReturnRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedConstructorParamRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedConstructorParamRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodParameterRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodParameterRector.php',
|
||||
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php',
|
||||
@ -3050,9 +3049,9 @@ class ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit81fb50a001fc3a5b67552be5bc7095a0::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit4c14e8bace132daaa89443888a4a21a7::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit4c14e8bace132daaa89443888a4a21a7::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit4c14e8bace132daaa89443888a4a21a7::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user