[DeadCode] Add Closure support on RemoveLastReturnRector (#1993)

This commit is contained in:
Abdul Malik Ikhsan 2022-04-02 15:30:52 +07:00 committed by GitHub
parent 759b7c881d
commit 7a37df2b60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 7 deletions

View File

@ -0,0 +1,36 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
class InClassMethod
{
function run($value) {
if ($value === 1000) {
return;
}
if ($value) {
return;
}
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
class InClassMethod
{
function run($value) {
if ($value === 1000) {
return;
}
if ($value) {
}
}
}
?>

View File

@ -0,0 +1,30 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
function ($value) {
if ($value === 1000) {
return;
}
if ($value) {
return;
}
};
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
function ($value) {
if ($value === 1000) {
return;
}
if ($value) {
}
};
?>

View File

@ -2,7 +2,7 @@
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
function some_function($value)
function InFunction($value)
{
if ($value === 1000) {
return;
@ -19,7 +19,7 @@ function some_function($value)
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
function some_function($value)
function InFunction($value)
{
if ($value === 1000) {
return;

View File

@ -0,0 +1,44 @@
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
class InInnerClosure
{
function run($value) {
$value = (bool) function ($value) {
if ($value === 1000) {
return;
}
if ($value) {
return;
}
};
return $value;
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\Fixture;
class InInnerClosure
{
function run($value) {
$value = (bool) function ($value) {
if ($value === 1000) {
return;
}
if ($value) {
}
};
return $value;
}
}
?>

View File

@ -5,6 +5,7 @@ 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_;
@ -61,7 +62,7 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class];
return [ClassMethod::class, Function_::class, Closure::class];
}
/**
@ -77,10 +78,6 @@ CODE_SAMPLE
return null;
}
if (! $lastNode instanceof Node) {
return null;
}
if ($lastNode !== $lastReturn) {
return null;
}