Skip to content

Commit

Permalink
[DeadCode] Add TargetRemoveClassMethodRector (#3240)
Browse files Browse the repository at this point in the history
* [DeadCode] Add TargetRemoveClassMethodRector

* merge fixtures to same test
  • Loading branch information
TomasVotruba committed Dec 22, 2022
1 parent c78e255 commit 70fea54
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 7 deletions.
38 changes: 36 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 416 Rules Overview
# 417 Rules Overview

<br>

Expand All @@ -12,7 +12,7 @@

- [Compatibility](#compatibility) (1)

- [DeadCode](#deadcode) (47)
- [DeadCode](#deadcode) (48)

- [DependencyInjection](#dependencyinjection) (2)

Expand Down Expand Up @@ -3664,6 +3664,40 @@ Removes unneeded `$value` = `$value` assigns

<br>

### TargetRemoveClassMethodRector

Remove defined class method

:wrench: **configure it!**

- class: [`Rector\DeadCode\Rector\Class_\TargetRemoveClassMethodRector`](../rules/DeadCode/Rector/Class_/TargetRemoveClassMethodRector.php)

```php
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Class_\TargetRemoveClassMethodRector;
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(
TargetRemoveClassMethodRector::class,
[new TargetRemoveClassMethod('SomeClass', 'run')]
);
};
```


```diff
class SomeClass
{
- public function run()
- {
- }
}
```

<br>

### TernaryToBooleanOrFalseToBooleanAndRector

Change ternary of bool : false to && bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture;

class SomeClass
{
public function removeMe()
{
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture;

class SomeClass
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
namespace Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class Php74Test extends AbstractRectorTestCase
final class TargetRemoveClassMethodRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand All @@ -17,9 +17,12 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

/**
* @return Iterator<string>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp74');
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Class_\TargetRemoveClassMethodRector;
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(TargetRemoveClassMethodRector::class, [
new TargetRemoveClassMethod(
'Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\Fixture\SomeClass',
'removeMe'
),
]);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\FixturePhp74;
namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

function run()
{
Expand All @@ -14,7 +14,7 @@ function run()
-----
<?php

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\FixturePhp74;
namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

function run()
{
Expand Down
92 changes: 92 additions & 0 deletions rules/DeadCode/Rector/Class_/TargetRemoveClassMethodRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\ValueObject\TargetRemoveClassMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\DeadCode\Rector\Class_\TargetRemoveClassMethodRector\TargetRemoveClassMethodRectorTest
*/
final class TargetRemoveClassMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var TargetRemoveClassMethod[]
*/
private array $targetRemoveClassMethods = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove defined class method', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
}
CODE_SAMPLE
,
[new TargetRemoveClassMethod('SomeClass', 'run')]
),
]);
}

public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->isAnonymous()) {
return null;
}

foreach ($this->targetRemoveClassMethods as $targetRemoveClassMethod) {
if (! $this->isName($node, $targetRemoveClassMethod->getClassName())) {
continue;
}

$classMethod = $node->getMethod($targetRemoveClassMethod->getMethodName());
if (! $classMethod instanceof ClassMethod) {
continue;
}

$this->removeNode($classMethod);
return null;
}

return null;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::notEmpty($configuration);
Assert::allIsInstanceOf($configuration, TargetRemoveClassMethod::class);

$this->targetRemoveClassMethods = $configuration;
}
}
24 changes: 24 additions & 0 deletions rules/DeadCode/ValueObject/TargetRemoveClassMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\ValueObject;

final class TargetRemoveClassMethod
{
public function __construct(
private readonly string $className,
private readonly string $methodName
) {
}

public function getClassName(): string
{
return $this->className;
}

public function getMethodName(): string
{
return $this->methodName;
}
}

0 comments on commit 70fea54

Please sign in to comment.