mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-27 21:23:31 +00:00
9187ff0430
[Scoped] Update full_build.sh to reflect the current Github workflow for build scoped (#2413)
106 lines
3.7 KiB
PHP
106 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Renaming\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Expression;
|
|
use PhpParser\Node\Stmt\Property;
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer;
|
|
use Rector\Renaming\Contract\RenameAnnotationInterface;
|
|
use Rector\Renaming\ValueObject\RenameAnnotationByType;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix20220602\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Renaming\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest
|
|
*/
|
|
final class RenameAnnotationRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @var RenameAnnotationInterface[]
|
|
*/
|
|
private $renameAnnotations = [];
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer
|
|
*/
|
|
private $docBlockTagReplacer;
|
|
public function __construct(\Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer $docBlockTagReplacer)
|
|
{
|
|
$this->docBlockTagReplacer = $docBlockTagReplacer;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Turns defined annotations above properties and methods to their new values.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class SomeTest extends TestCase
|
|
{
|
|
/**
|
|
* @test
|
|
*/
|
|
public function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class SomeTest extends TestCase
|
|
{
|
|
/**
|
|
* @scenario
|
|
*/
|
|
public function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, [new \Rector\Renaming\ValueObject\RenameAnnotationByType('PHPUnit\\Framework\\TestCase', 'test', 'scenario')])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Property::class, \PhpParser\Node\Stmt\Expression::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod|Property $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
$classLike = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Class_::class);
|
|
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
|
return null;
|
|
}
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
|
$hasChanged = \false;
|
|
foreach ($this->renameAnnotations as $renameAnnotation) {
|
|
if ($renameAnnotation instanceof \Rector\Renaming\ValueObject\RenameAnnotationByType && !$this->isObjectType($classLike, $renameAnnotation->getObjectType())) {
|
|
continue;
|
|
}
|
|
$this->docBlockTagReplacer->replaceTagByAnother($phpDocInfo, $renameAnnotation->getOldAnnotation(), $renameAnnotation->getNewAnnotation());
|
|
$hasChanged = \true;
|
|
}
|
|
if (!$hasChanged) {
|
|
return null;
|
|
}
|
|
return $node;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
\RectorPrefix20220602\Webmozart\Assert\Assert::allIsAOf($configuration, \Rector\Renaming\Contract\RenameAnnotationInterface::class);
|
|
$this->renameAnnotations = $configuration;
|
|
}
|
|
}
|