mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 22:08:00 +01:00
[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:
parent
2872eb095d
commit
bd62164c85
@ -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');
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Privatization\Tests\Rector\ClassMethod\PrivatizeLocalOnlyMethodRector\Fixture;
|
||||
|
||||
/**
|
||||
* @api
|
||||
*/
|
||||
class SkipApiClassAnnotation
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return '1234';
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user