[DeadCode] Add RemoveUnusedFunctionRector

This commit is contained in:
TomasVotruba 2020-03-25 12:42:39 +01:00
parent 41f841c330
commit 6a0551f004
9 changed files with 224 additions and 9 deletions

View File

@ -35,3 +35,4 @@ services:
Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector: null
Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector: null
Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector: null
Rector\DeadCode\Rector\Function_\RemoveUnusedFunctionRector: null

View File

@ -1,4 +1,4 @@
# All 467 Rectors Overview
# All 468 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -3062,6 +3062,27 @@ Remove unused key in foreach
<br>
### `RemoveUnusedFunctionRector`
- class: [`Rector\DeadCode\Rector\Function_\RemoveUnusedFunctionRector`](/../master/rules/dead-code/src/Rector/Function_/RemoveUnusedFunctionRector.php)
- [test fixtures](/../master/rules/dead-code/tests/Rector/Function_/RemoveUnusedFunctionRector/Fixture)
Remove unused function
```diff
-function removeMe()
-{
-}
-
function useMe()
{
}
useMe();
```
<br>
### `RemoveUnusedParameterRector`
- class: [`Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector`](/../master/rules/dead-code/src/Rector/ClassMethod/RemoveUnusedParameterRector.php)

View File

@ -7,6 +7,7 @@ namespace Rector\NodeCollector\NodeCollector;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
@ -36,6 +37,11 @@ final class ParsedFunctionLikeNodeCollector
*/
private $functionsByName = [];
/**
* @var FuncCall[][]
*/
private $funcCallsByName = [];
/**
* @var MethodCall[][][]|StaticCall[][][]
*/
@ -100,12 +106,13 @@ final class ParsedFunctionLikeNodeCollector
if ($node instanceof Function_) {
$functionName = $this->nodeNameResolver->getName($node);
if ($functionName === null) {
return;
}
$this->functionsByName[$functionName] = $node;
}
if ($node instanceof FuncCall) {
$functionName = $this->nodeNameResolver->getName($node);
$this->funcCallsByName[$functionName][] = $node;
}
}
public function findFunction(string $name): ?Function_
@ -145,6 +152,11 @@ final class ParsedFunctionLikeNodeCollector
return null;
}
public function isFunctionUsed(string $functionName): bool
{
return isset($this->funcCallsByName[$functionName]);
}
private function addMethod(ClassMethod $classMethod): void
{
$className = $classMethod->getAttribute(AttributeKey::CLASS_NAME);

View File

@ -31,7 +31,6 @@ final class FuncCallNameResolver implements NodeNameResolverInterface
}
$functionName = $node->name;
if (! $functionName instanceof Name) {
return (string) $functionName;
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Rector\NodeNameResolver\NodeNameResolver;
use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class FunctionNameResolver implements NodeNameResolverInterface
{
public function getNode(): string
{
return Function_::class;
}
/**
* @param Function_ $node
*/
public function resolve(Node $node): ?string
{
$bareName = (string) $node->name;
$namespaceName = $node->getAttribute(AttributeKey::NAMESPACE_NAME);
if ($namespaceName) {
return $namespaceName . '\\' . $bareName;
}
return $bareName;
}
}

View File

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Function_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeCollector\NodeCollector\ParsedFunctionLikeNodeCollector;
/**
* @see \Rector\DeadCode\Tests\Rector\Function_\RemoveUnusedFunctionRector\RemoveUnusedFunctionRectorTest
*/
final class RemoveUnusedFunctionRector extends AbstractRector
{
/**
* @var ParsedFunctionLikeNodeCollector
*/
private $parsedFunctionLikeNodeCollector;
public function __construct(ParsedFunctionLikeNodeCollector $parsedFunctionLikeNodeCollector)
{
$this->parsedFunctionLikeNodeCollector = $parsedFunctionLikeNodeCollector;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove unused function', [
new CodeSample(
<<<'PHP'
function removeMe()
{
}
function useMe()
{
}
useMe();
PHP
,
<<<'PHP'
function useMe()
{
}
useMe();
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Function_::class];
}
/**
* @param Function_ $node
*/
public function refactor(Node $node): ?Node
{
/** @var string $functionName */
$functionName = $this->getName($node);
if ($this->parsedFunctionLikeNodeCollector->isFunctionUsed($functionName)) {
return null;
}
$this->removeNode($node);
return $node;
}
}

View File

@ -116,7 +116,7 @@ PHP
return true;
}
$functionName = $this->getName($node->name);
$functionName = $this->getName($node);
if ($functionName === null) {
return false;
}
@ -137,8 +137,7 @@ PHP
*/
private function resolveDefaultValuesFromCall(Node $node): array
{
/** @var string|null $nodeName */
$nodeName = $this->getName($node->name);
$nodeName = $this->resolveNodeName($node);
if ($nodeName === null) {
return [];
}
@ -242,4 +241,16 @@ PHP
return $defaultValues;
}
/**
* @param StaticCall|FuncCall|MethodCall $node
*/
private function resolveNodeName(Node $node): ?string
{
if ($node instanceof FuncCall) {
return $this->getName($node);
}
return $this->getName($node->name);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Rector\DeadCode\Tests\Rector\Function_\RemoveUnusedFunctionRector\Fixture;
function removeMe()
{
}
function useMe()
{
}
useMe();
?>
-----
<?php
namespace Rector\DeadCode\Tests\Rector\Function_\RemoveUnusedFunctionRector\Fixture;
function useMe()
{
}
useMe();
?>

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Tests\Rector\Function_\RemoveUnusedFunctionRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\DeadCode\Rector\Function_\RemoveUnusedFunctionRector;
final class RemoveUnusedFunctionRectorTest 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 RemoveUnusedFunctionRector::class;
}
}