Skip to content

Commit

Permalink
Make RemoveEmptyClassMethodRector skip deprecated class method, as us…
Browse files Browse the repository at this point in the history
…ed for BC (#5061)
  • Loading branch information
TomasVotruba committed Sep 21, 2023
1 parent 84916ed commit 36f0b4a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
31 changes: 5 additions & 26 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenCollectorRector;
Expand Down Expand Up @@ -57,33 +53,16 @@

$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();
$rectorConfig->parallel();

$rectorConfig->skip([
RenamePropertyToMatchTypeRector::class => [
__DIR__ . '/src/Console/Command/ListRulesCommand.php',
__DIR__ . '/src/Configuration/ConfigInitializer.php',
],

RemoveEmptyClassMethodRector::class => [
__DIR__ . '/packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
],

// resolve later
RenameParamToMatchTypeRector::class => [
__DIR__ . '/src/Console/Command/ListRulesCommand.php',
__DIR__ . '/src/Configuration/ConfigInitializer.php',
__DIR__ . '/src/PhpParser/NodeTraverser/RectorNodeTraverser.php',
],

RenameVariableToMatchMethodCallReturnTypeRector::class => [__DIR__ . '/packages/Config/RectorConfig.php'],

StringClassNameToClassConstantRector::class,
__DIR__ . '/bin/validate-phpstan-version.php',
// tests
'**/Fixture*',
'**/Source*',
'**/Expected*',
'*/Fixture/*',
'*/Fixture*',
'*/Source/*',
'*/Source*',
'*/Expected*',

// keep configs untouched, as the classes are just strings
UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config', '*/config/*'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector\Fixture;

final class KeepDeprecated
{
/**
* @deprecated
*/
public function keep()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ public function __construct(
) {
}

public function isControllerClassMethodWithBehaviorAnnotation(Class_ $class, ClassMethod $classMethod): bool
public function isControllerClassMethod(Class_ $class, ClassMethod $classMethod): bool
{
if (! $this->isControllerClassMethod($class, $classMethod)) {
if (! $classMethod->isPublic()) {
return false;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
return $phpDocInfo->hasByType(GenericTagValueNode::class);
}

private function isControllerClassMethod(Class_ $class, ClassMethod $classMethod): bool
{
if (! $classMethod->isPublic()) {
if (! $this->hasParentClassController($class)) {
return false;
}

return $this->hasParentClassController($class);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
return $phpDocInfo->hasByType(GenericTagValueNode::class);
}

private function hasParentClassController(Class_ $class): bool
Expand Down
26 changes: 21 additions & 5 deletions rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\NodeManipulator\ClassMethodManipulator;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -24,7 +27,8 @@ final class RemoveEmptyClassMethodRector extends AbstractRector
public function __construct(
private readonly ClassMethodManipulator $classMethodManipulator,
private readonly ControllerClassMethodManipulator $controllerClassMethodManipulator,
private readonly ParamAnalyzer $paramAnalyzer
private readonly ParamAnalyzer $paramAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
) {
}

Expand Down Expand Up @@ -135,17 +139,29 @@ private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod):
return true;
}

if ($this->controllerClassMethodManipulator->isControllerClassMethodWithBehaviorAnnotation(
$class,
$classMethod
)) {
if ($this->hasDeprecatedAnnotation($classMethod)) {
return true;
}

if ($this->controllerClassMethodManipulator->isControllerClassMethod($class, $classMethod)) {
return true;
}

if ($this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
// has parent class?
return $class->extends instanceof FullyQualified;
}

return $this->nodeNameResolver->isName($classMethod, MethodName::INVOKE);
}

private function hasDeprecatedAnnotation(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}

return $phpDocInfo->hasByType(DeprecatedTagValueNode::class);
}
}
7 changes: 2 additions & 5 deletions tests/Issues/InfiniteIfElse/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
use Rector\Config\RectorConfig;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
BinarySwitchToIfElseRector::class,
RemoveAlwaysElseRector::class,
]);
$rectorConfig->rules([BinarySwitchToIfElseRector::class, RemoveAlwaysElseRector::class]);
};

0 comments on commit 36f0b4a

Please sign in to comment.