[DeadCode] Skip if the methods have api annotation (#5115)

* [DeadCode] Skip if the methods have api annotation

* [ci-review] Rector Rectify

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2021-01-08 20:15:44 +01:00 committed by GitHub
parent 2872eb095d
commit bd62164c85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 21 deletions

View File

@ -6,11 +6,9 @@ namespace Rector\DeadCode\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Caching\Contract\Rector\ZeroCacheRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\UnusedNodeResolver\UnusedClassResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -114,7 +112,7 @@ CODE_SAMPLE
return true;
}
if ($this->hasApiAnnotation($class)) {
if ($this->hasTagByName($class, 'api')) {
return true;
}
@ -124,7 +122,7 @@ CODE_SAMPLE
private function hasMethodWithApiAnnotation(Class_ $class): bool
{
foreach ($class->getMethods() as $classMethod) {
if (! $this->hasApiAnnotation($classMethod)) {
if (! $this->hasTagByName($classMethod, 'api')) {
continue;
}
@ -133,14 +131,4 @@ CODE_SAMPLE
return false;
}
private function hasApiAnnotation(Node $node): bool
{
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}
return $phpDocInfo->hasByName('api');
}
}

View File

@ -35,6 +35,11 @@ final class PrivatizeLocalOnlyMethodRector extends AbstractRector implements Zer
*/
private const CONTROLLER_PRESENTER_SUFFIX_REGEX = '#(Controller|Presenter)$#';
/**
* @var string
*/
private const API = 'api';
/**
* @var ClassMethodVisibilityVendorLockResolver
*/
@ -127,15 +132,11 @@ CODE_SAMPLE
return true;
}
if ($this->isAnonymousClass($classLike)) {
if ($this->shouldSkipClassLike($classLike)) {
return true;
}
if ($this->isObjectType($classLike, 'PHPUnit\Framework\TestCase')) {
return true;
}
if ($this->isDoctrineEntityClass($classLike)) {
if ($this->hasTagByName($classMethod, self::API)) {
return true;
}
@ -162,7 +163,7 @@ CODE_SAMPLE
return false;
}
return $phpDocInfo->hasByNames(['api', TagName::REQUIRED]);
return $phpDocInfo->hasByNames([self::API, TagName::REQUIRED]);
}
private function isControllerAction(Class_ $class, ClassMethod $classMethod): bool
@ -193,6 +194,10 @@ CODE_SAMPLE
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
if ($this->hasTagByName($classMethod, self::API)) {
return true;
}
if ($classMethod->isPrivate()) {
return true;
}
@ -213,4 +218,21 @@ CODE_SAMPLE
// possibly container service factories
return $this->isNames($classMethod, ['create', 'create*']);
}
private function shouldSkipClassLike(Class_ $class): bool
{
if ($this->isAnonymousClass($class)) {
return true;
}
if ($this->isDoctrineEntityClass($class)) {
return true;
}
if ($this->isObjectType($class, 'PHPUnit\Framework\TestCase')) {
return true;
}
return $this->hasTagByName($class, self::API);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Rector\Privatization\Tests\Rector\ClassMethod\PrivatizeLocalOnlyMethodRector\Fixture;
/**
* @api
*/
class SkipApiClassAnnotation
{
public function run()
{
return '1234';
}
}