[DeadCode] Remove RemoveAssignOfVoidReturnFunctionRector as unreliable, let PHPStan report and use handle various cases (#1313)

This commit is contained in:
Tomas Votruba 2021-11-25 22:03:04 +03:00 committed by GitHub
parent 51d369a46c
commit aa72166505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 6 additions and 330 deletions

View File

@ -1,4 +1,4 @@
# 499 Rules Overview
# 498 Rules Overview
<br>
@ -18,7 +18,7 @@
- [Composer](#composer) (6)
- [DeadCode](#deadcode) (50)
- [DeadCode](#deadcode) (49)
- [DependencyInjection](#dependencyinjection) (2)
@ -3014,29 +3014,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
<br>
### RemoveAssignOfVoidReturnFunctionRector
Remove assign of void function/method to variable
- class: [`Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector`](../rules/DeadCode/Rector/Assign/RemoveAssignOfVoidReturnFunctionRector.php)
```diff
class SomeClass
{
public function run()
{
- $value = $this->getOne();
+ $this->getOne();
}
private function getOne(): void
{
}
}
```
<br>
### RemoveCodeAfterReturnRector
Remove dead code after return statement
@ -7990,9 +7967,10 @@ Change simple property init and assign to constructor promotion
{
- public float $someVariable;
-
- public function __construct(float $someVariable = 0.0)
+ public function __construct(private float $someVariable = 0.0)
{
public function __construct(
- float $someVariable = 0.0
+ private float $someVariable = 0.0
) {
- $this->someVariable = $someVariable;
}
}

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
use Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector;
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
use Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\DeadCode\Rector\BinaryOp\RemoveDuplicatedInstanceOfRector;
@ -85,7 +84,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(RemoveUnusedVariableAssignRector::class);
$services->set(RemoveDuplicatedIfReturnRector::class);
$services->set(RemoveUnusedNonEmptyArrayBeforeForeachRector::class);
$services->set(RemoveAssignOfVoidReturnFunctionRector::class);
$services->set(RemoveEmptyMethodCallRector::class);
$services->set(RemoveDeadConditionAboveReturnRector::class);
$services->set(RemoveUnusedConstructorParamRector::class);

View File

@ -1,35 +0,0 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
class Fixture
{
public function run()
{
$value = $this->getOne();
}
private function getOne(): void
{
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
class Fixture
{
public function run()
{
$this->getOne();
}
private function getOne(): void
{
}
}
?>

View File

@ -1,19 +0,0 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
class SkipAssignToPropertyFetch
{
public $value = 'value';
public function run()
{
$this->value = $this->getOne();
}
private function getOne(): void
{
}
}
?>

View File

@ -1,16 +0,0 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
class SkipReturnedValue
{
public function run()
{
$value = $this->getOne();
}
private function getOne(): int
{
return 1;
}
}

View File

@ -1,52 +0,0 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
class SkipVariableUsed
{
public function run()
{
if (rand(0, 1)) {
$value = $this->getOne();
} else {
$value = 1;
}
return $value;
}
public function run2()
{
if (rand(0, 1)) {
$this->value = $this->getOne();
} else {
$this->value = 1;
}
return $this->value;
}
public function run3($value = 1)
{
if (rand(0, 1)) {
$value = $this->getOne();
}
return compact('value');
}
public function run4($value = 1)
{
if (rand(0, 1)) {
$value = $this->getOne();
}
include 'anotherFile.php';
}
private function getOne(): void
{
}
}
?>

View File

@ -1,35 +0,0 @@
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
final class SomeStaticCall
{
public function run()
{
$value = self::someMethod();
}
private static function someMethod(): void
{
}
}
?>
-----
<?php
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
final class SomeStaticCall
{
public function run()
{
self::someMethod();
}
private static function someMethod(): void
{
}
}
?>

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class RemoveAssignOfVoidReturnFunctionRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RemoveAssignOfVoidReturnFunctionRector::class);
};

View File

@ -1,99 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\VoidType;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\RemoveAssignOfVoidReturnFunctionRectorTest
*/
final class RemoveAssignOfVoidReturnFunctionRector extends AbstractRector
{
public function __construct(
private ExprUsedInNextNodeAnalyzer $exprUsedInNextNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove assign of void function/method to variable',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = $this->getOne();
}
private function getOne(): void
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$this->getOne();
}
private function getOne(): void
{
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof FuncCall && ! $node->expr instanceof MethodCall && ! $node->expr instanceof StaticCall) {
return null;
}
if (! $node->var instanceof Variable) {
return null;
}
if ($this->exprUsedInNextNodeAnalyzer->isUsed($node->var, true)) {
return null;
}
$exprType = $this->nodeTypeResolver->getType($node->expr);
if (! $exprType instanceof VoidType) {
return null;
}
return $node->expr;
}
}