rector/rules/renaming/src/Rector/ClassMethod/RenameAnnotationRector.php

127 lines
3.4 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2018-02-09 00:14:45 +00:00
namespace Rector\Renaming\Rector\ClassMethod;
2018-02-09 00:14:45 +00:00
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
2018-02-09 00:14:45 +00:00
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
2020-07-29 13:55:33 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer;
use Rector\Renaming\ValueObject\RenameAnnotation;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
2018-02-09 00:14:45 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Renaming\Tests\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest
2019-09-03 09:11:45 +00:00
*/
final class RenameAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
2018-02-09 00:14:45 +00:00
{
/**
2020-07-29 13:55:33 +00:00
* @var string
2018-02-09 00:14:45 +00:00
*/
public const RENAMED_ANNOTATIONS_IN_TYPES = 'renamed_annotations_in_types';
2018-02-09 00:14:45 +00:00
/**
* @var RenameAnnotation[]
2018-02-09 00:14:45 +00:00
*/
private $renamedAnnotations = [];
/**
* @var DocBlockTagReplacer
*/
private $docBlockTagReplacer;
public function __construct(DocBlockTagReplacer $docBlockTagReplacer)
{
$this->docBlockTagReplacer = $docBlockTagReplacer;
}
2018-02-09 00:14:45 +00:00
public function getRuleDefinition(): RuleDefinition
2018-04-08 11:51:26 +00:00
{
return new RuleDefinition(
'Turns defined annotations above properties and methods to their new values.',
2018-04-08 14:05:11 +00:00
[
2018-08-01 19:52:44 +00:00
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
2018-08-01 19:52:44 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
/**
* @test
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
CODE_SAMPLE
2018-04-29 17:25:44 +00:00
,
<<<'CODE_SAMPLE'
2018-08-01 19:52:44 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
2020-02-04 07:54:00 +00:00
/**
* @scenario
*/
2018-08-01 19:52:44 +00:00
public function someMethod()
{
}
}
CODE_SAMPLE
2018-08-01 19:52:44 +00:00
,
[
self::RENAMED_ANNOTATIONS_IN_TYPES => [
new RenameAnnotation('PHPUnit\Framework\TestCase', 'test', 'scenario'),
2018-08-01 19:52:44 +00:00
],
]
2018-04-08 14:05:11 +00:00
),
]
);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
* @return array<class-string<Node>>
2018-08-14 22:12:41 +00:00
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Property::class];
}
/**
* @param ClassMethod|Property $node
*/
public function refactor(Node $node): ?Node
2018-02-09 00:14:45 +00:00
{
2020-07-19 18:52:42 +00:00
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof Class_) {
return null;
}
2019-01-25 00:49:26 +00:00
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
2018-02-09 00:14:45 +00:00
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
foreach ($this->renamedAnnotations as $renamedAnnotation) {
if (! $this->isObjectType($classLike, $renamedAnnotation->getObjectType())) {
continue;
2018-02-09 09:27:48 +00:00
}
$this->docBlockTagReplacer->replaceTagByAnother(
$phpDocInfo,
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
$renamedAnnotation->getOldAnnotation(),
$renamedAnnotation->getNewAnnotation()
);
2018-02-09 00:14:45 +00:00
}
2018-08-14 22:12:41 +00:00
2018-02-09 09:27:48 +00:00
return $node;
}
2020-07-29 13:55:33 +00:00
public function configure(array $configuration): void
{
$renamedAnnotationsInTypes = $configuration[self::RENAMED_ANNOTATIONS_IN_TYPES] ?? [];
Assert::allIsInstanceOf($renamedAnnotationsInTypes, RenameAnnotation::class);
$this->renamedAnnotations = $renamedAnnotationsInTypes;
2020-07-29 13:55:33 +00:00
}
2018-02-09 00:14:45 +00:00
}