2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-08-18 15:57:30 +00:00
namespace Rector\Renaming\Rector\ClassMethod ;
2018-02-09 00:14:45 +00:00
use PhpParser\Node ;
2019-09-06 10:30:58 +00:00
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 ;
2021-01-28 21:24:00 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-01-19 15:03:26 +00:00
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer ;
2020-09-12 21:19:08 +00:00
use Rector\Renaming\ValueObject\RenameAnnotation ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2021-11-28 00:36:13 +00:00
use RectorPrefix20211128\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Renaming\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class RenameAnnotationRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\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
*/
2020-08-26 10:54:53 +00:00
public const RENAMED_ANNOTATIONS_IN_TYPES = 'renamed_annotations_in_types' ;
2018-02-09 00:14:45 +00:00
/**
2020-09-12 21:19:08 +00:00
* @ var RenameAnnotation []
2018-02-09 00:14:45 +00:00
*/
2021-01-19 15:03:26 +00:00
private $renamedAnnotations = [];
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer
2021-01-19 15:03:26 +00:00
*/
private $docBlockTagReplacer ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer $docBlockTagReplacer )
2021-01-19 15:03:26 +00:00
{
$this -> docBlockTagReplacer = $docBlockTagReplacer ;
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-04-08 11:51:26 +00:00
{
2021-05-10 22:23:08 +00:00
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'
2018-08-01 19:52:44 +00:00
class SomeTest extends PHPUnit\Framework\TestCase
{
2018-08-04 07:33:54 +00:00
/**
* @ test
*/
2018-08-01 19:52:44 +00:00
public function someMethod ()
{
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +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
/**
2018-08-04 07:33:54 +00:00
* @ scenario
*/
2018-08-01 19:52:44 +00:00
public function someMethod ()
{
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-10 22:23:08 +00:00
, [ self :: RENAMED_ANNOTATIONS_IN_TYPES => [ new \Rector\Renaming\ValueObject\RenameAnnotation ( 'PHPUnit\\Framework\\TestCase' , 'test' , 'scenario' )]])]);
2018-04-08 11:51:26 +00:00
}
2018-08-14 22:12:41 +00:00
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-08-14 22:12:41 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-08-14 21:33:39 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\ClassMethod :: class , \PhpParser\Node\Stmt\Property :: class ];
2018-08-14 21:33:39 +00:00
}
/**
* @ param ClassMethod | Property $node
*/
2021-07-05 22:50:18 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2018-02-09 00:14:45 +00:00
{
2021-11-06 12:10:48 +00:00
$classLike = $this -> betterNodeFinder -> findParentType ( $node , \PhpParser\Node\Stmt\Class_ :: class );
2021-05-10 22:23:08 +00:00
if ( ! $classLike instanceof \PhpParser\Node\Stmt\Class_ ) {
2021-01-19 15:03:26 +00:00
return null ;
}
2021-01-18 21:45:13 +00:00
$phpDocInfo = $this -> phpDocInfoFactory -> createFromNodeOrEmpty ( $node );
2021-03-05 10:55:40 +00:00
foreach ( $this -> renamedAnnotations as $renamedAnnotation ) {
2021-05-09 20:15:43 +00:00
if ( ! $this -> isObjectType ( $classLike , $renamedAnnotation -> getObjectType ())) {
2020-08-26 10:54:53 +00:00
continue ;
2018-02-09 09:27:48 +00:00
}
2021-05-09 20:15:43 +00:00
$this -> docBlockTagReplacer -> replaceTagByAnother ( $phpDocInfo , $renamedAnnotation -> getOldAnnotation (), $renamedAnnotation -> getNewAnnotation ());
2018-02-09 00:14:45 +00:00
}
2018-02-09 09:27:48 +00:00
return $node ;
}
2021-05-02 10:46:55 +00:00
/**
* @ param array < string , RenameAnnotation [] > $configuration
*/
2021-05-09 20:15:43 +00:00
public function configure ( array $configuration ) : void
2020-07-29 13:55:33 +00:00
{
2021-11-27 13:43:28 +00:00
$renamedAnnotationsInTypes = $configuration [ self :: RENAMED_ANNOTATIONS_IN_TYPES ] ? ? ( $configuration ? : []);
2021-11-28 00:36:13 +00:00
\RectorPrefix20211128\Webmozart\Assert\Assert :: allIsInstanceOf ( $renamedAnnotationsInTypes , \Rector\Renaming\ValueObject\RenameAnnotation :: class );
2021-01-19 15:03:26 +00:00
$this -> renamedAnnotations = $renamedAnnotationsInTypes ;
2020-07-29 13:55:33 +00:00
}
2018-02-09 00:14:45 +00:00
}