mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-30 07:50:53 +00:00
[DeadCode] Add RemoveAssignOfVoidReturnFunctionRector (#3068)
This commit is contained in:
parent
5204ce1523
commit
8406192dca
|
@ -37,3 +37,4 @@ services:
|
|||
Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector: null
|
||||
Rector\DeadCode\Rector\Function_\RemoveUnusedFunctionRector: null
|
||||
Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector: null
|
||||
Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector: null
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 469 Rectors Overview
|
||||
# All 470 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
|
@ -2512,6 +2512,30 @@ Remove and true that has no added value
|
|||
|
||||
<br>
|
||||
|
||||
### `RemoveAssignOfVoidReturnFunctionRector`
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector`](/../master/rules/dead-code/src/Rector/Assign/RemoveAssignOfVoidReturnFunctionRector.php)
|
||||
- [test fixtures](/../master/rules/dead-code/tests/Rector/Assign/RemoveAssignOfVoidReturnFunctionRector/Fixture)
|
||||
|
||||
Remove assign of void function/method to variable
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $value = $this->getOne();
|
||||
+ $this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `RemoveCodeAfterReturnRector`
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector`](/../master/rules/dead-code/src/Rector/FunctionLike/RemoveCodeAfterReturnRector.php)
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
<?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 PHPStan\Type\VoidType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\DeadCode\Tests\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\RemoveAssignOfVoidReturnFunctionRectorTest
|
||||
*/
|
||||
final class RemoveAssignOfVoidReturnFunctionRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Remove assign of void function/method to variable', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$value = $this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
$nodeStaticType = $this->getStaticType($node->expr);
|
||||
|
||||
// void type
|
||||
if (! $nodeStaticType instanceof VoidType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $node->expr;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$value = $this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector\Fixture;
|
||||
|
||||
class SkipReturnedValue
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$value = $this->getOne();
|
||||
}
|
||||
|
||||
private function getOne(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DeadCode\Tests\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\DeadCode\Rector\Assign\RemoveAssignOfVoidReturnFunctionRector;
|
||||
|
||||
final class RemoveAssignOfVoidReturnFunctionRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveAssignOfVoidReturnFunctionRector::class;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\PHPStan\Reflection\TypeToCallReflectionResolver;
|
||||
namespace Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver;
|
||||
|
||||
use PHPStan\Reflection\ClassMemberAccessAnswerer;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
|
@ -9,6 +9,7 @@ use PHPStan\Reflection\Native\NativeFunctionReflection;
|
|||
use PHPStan\TrinaryLogic;
|
||||
use PHPStan\Type\ClosureType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
|
||||
|
||||
final class ClosureTypeToCallReflectionResolver implements TypeToCallReflectionResolverInterface
|
||||
{
|
||||
|
|
|
@ -13,6 +13,7 @@ use PHPStan\Type\Constant\ConstantStringType;
|
|||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\ObjectWithoutClassType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
|
||||
|
||||
/**
|
||||
* @see https://github.com/phpstan/phpstan-src/blob/b1fd47bda2a7a7d25091197b125c0adf82af6757/src/Type/Constant/ConstantArrayType.php#L188
|
||||
|
|
|
@ -10,6 +10,7 @@ use PHPStan\Reflection\ClassMemberAccessAnswerer;
|
|||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\Constant\ConstantStringType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
|
||||
|
||||
/**
|
||||
* @see https://github.com/phpstan/phpstan-src/blob/b1fd47bda2a7a7d25091197b125c0adf82af6757/src/Type/Constant/ConstantStringType.php#L147
|
||||
|
|
|
@ -8,6 +8,7 @@ use PHPStan\Reflection\ClassMemberAccessAnswerer;
|
|||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
|
||||
|
||||
/**
|
||||
* @see https://github.com/phpstan/phpstan-src/blob/b1fd47bda2a7a7d25091197b125c0adf82af6757/src/Type/ObjectType.php#L705
|
||||
|
@ -40,7 +41,6 @@ final class ObjectTypeToCallReflectionResolver implements TypeToCallReflectionRe
|
|||
}
|
||||
|
||||
$classReflection = $this->reflectionProvider->getClass($className);
|
||||
|
||||
if (! $classReflection->hasNativeMethod('__invoke')) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use PHPStan\Reflection\ClassMemberAccessAnswerer;
|
|||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Contract\PHPStan\Reflection\TypeToCallReflectionResolver\TypeToCallReflectionResolverInterface;
|
||||
|
||||
final class TypeToCallReflectionResolverRegistry
|
||||
{
|
||||
|
|
|
@ -73,6 +73,9 @@ final class FunctionReflectionHelper
|
|||
return $expandedClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getUses(Namespace_ $node): array
|
||||
{
|
||||
$uses = [];
|
||||
|
|
Loading…
Reference in New Issue
Block a user