2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2019-05-25 23:15:21 +00:00
namespace Rector\CodingStyle\Rector\Catch_ ;
2022-03-10 10:09:54 +00:00
use RectorPrefix20220310\Nette\Utils\Strings ;
2019-05-25 23:15:21 +00:00
use PhpParser\Node ;
2021-07-02 10:39:22 +00:00
use PhpParser\Node\Expr\Assign ;
2020-12-21 02:12:42 +00:00
use PhpParser\Node\Expr\Variable ;
2021-07-02 10:39:22 +00:00
use PhpParser\Node\FunctionLike ;
2019-05-25 23:15:21 +00:00
use PhpParser\Node\Stmt\Catch_ ;
2021-07-02 10:39:22 +00:00
use PhpParser\Node\Stmt\TryCatch ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-07-02 10:39:22 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector\CatchExceptionNameMatchingTypeRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class CatchExceptionNameMatchingTypeRector extends \Rector\Core\Rector\AbstractRector
2019-05-25 23:15:21 +00:00
{
2020-07-21 17:04:31 +00:00
/**
* @ var string
2020-10-29 18:04:33 +00:00
* @ see https :// regex101 . com / r / xmfMAX / 1
2020-07-21 17:04:31 +00:00
*/
private const STARTS_WITH_ABBREVIATION_REGEX = '#^([A-Za-z]+?)([A-Z]{1}[a-z]{1})([A-Za-z]*)#' ;
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-05-25 23:15:21 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Type and name of catch exception should match' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-05-25 23:15:21 +00:00
class SomeClass
{
public function run ()
{
try {
// ...
} catch ( SomeException $typoException ) {
$typoException -> getMessage ();
}
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-05-25 23:15:21 +00:00
class SomeClass
{
public function run ()
{
try {
// ...
} catch ( SomeException $someException ) {
$someException -> getMessage ();
}
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-05-25 23:15:21 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-05-25 23:15:21 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-05-25 23:15:21 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Catch_ :: class ];
2019-05-25 23:15:21 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param Catch_ $node
2019-05-25 23:15:21 +00:00
*/
2021-12-10 10:22:23 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-05-25 23:15:21 +00:00
{
2021-05-09 20:15:43 +00:00
if ( \count ( $node -> types ) !== 1 ) {
2019-05-25 23:15:21 +00:00
return null ;
}
2020-06-03 09:39:18 +00:00
if ( $node -> var === null ) {
return null ;
}
2019-05-25 23:15:21 +00:00
$oldVariableName = $this -> getName ( $node -> var );
2021-11-28 17:35:55 +00:00
if ( ! \is_string ( $oldVariableName )) {
2019-05-25 23:15:21 +00:00
return null ;
}
2020-07-21 17:04:31 +00:00
$type = $node -> types [ 0 ];
2021-02-09 01:52:35 +00:00
$typeShortName = $this -> nodeNameResolver -> getShortName ( $type );
2022-03-10 10:09:54 +00:00
$newVariableName = \RectorPrefix20220310\Nette\Utils\Strings :: replace ( \lcfirst ( $typeShortName ), self :: STARTS_WITH_ABBREVIATION_REGEX , function ( array $matches ) : string {
2021-05-09 20:15:43 +00:00
$output = '' ;
$output .= isset ( $matches [ 1 ]) ? \strtolower ( $matches [ 1 ]) : '' ;
$output .= $matches [ 2 ] ? ? '' ;
$output .= $matches [ 3 ] ? ? '' ;
return $output ;
});
2019-05-25 23:15:21 +00:00
if ( $oldVariableName === $newVariableName ) {
return null ;
}
2021-05-10 22:23:08 +00:00
$newVariable = new \PhpParser\Node\Expr\Variable ( $newVariableName );
2022-01-21 06:12:56 +00:00
$isFoundInPrevious = ( bool ) $this -> betterNodeFinder -> findFirstPreviousOfNode ( $node , function ( \PhpParser\Node $n ) use ( $newVariable ) : bool {
2021-02-19 12:01:23 +00:00
return $this -> nodeComparator -> areNodesEqual ( $n , $newVariable );
2020-12-23 14:33:14 +00:00
});
if ( $isFoundInPrevious ) {
return null ;
}
$node -> var -> name = $newVariableName ;
2019-05-25 23:15:21 +00:00
$this -> renameVariableInStmts ( $node , $oldVariableName , $newVariableName );
return $node ;
}
2021-05-10 22:23:08 +00:00
private function renameVariableInStmts ( \PhpParser\Node\Stmt\Catch_ $catch , string $oldVariableName , string $newVariableName ) : void
2019-05-26 11:47:23 +00:00
{
2022-02-19 18:54:29 +00:00
$this -> traverseNodesWithCallable ( $catch -> stmts , function ( \PhpParser\Node $node ) use ( $oldVariableName , $newVariableName ) {
2021-05-10 22:23:08 +00:00
if ( ! $node instanceof \PhpParser\Node\Expr\Variable ) {
2022-02-19 18:54:29 +00:00
return null ;
2020-12-21 02:12:42 +00:00
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeNameResolver -> isName ( $node , $oldVariableName )) {
2022-02-19 18:54:29 +00:00
return null ;
2019-05-25 23:15:21 +00:00
}
$node -> name = $newVariableName ;
2022-02-19 18:54:29 +00:00
return null ;
2019-05-25 23:15:21 +00:00
});
2021-07-02 10:39:22 +00:00
/** @var TryCatch $tryCatch */
$tryCatch = $catch -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
$next = $tryCatch -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
$this -> replaceNextUsageVariable ( $tryCatch , $next , $oldVariableName , $newVariableName );
}
private function replaceNextUsageVariable ( \PhpParser\Node $currentNode , ? \PhpParser\Node $nextNode , string $oldVariableName , string $newVariableName ) : void
{
if ( ! $nextNode instanceof \PhpParser\Node ) {
$parent = $currentNode -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $parent instanceof \PhpParser\Node ) {
return ;
}
if ( $parent instanceof \PhpParser\Node\FunctionLike ) {
return ;
}
$nextNode = $parent -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
$this -> replaceNextUsageVariable ( $parent , $nextNode , $oldVariableName , $newVariableName );
return ;
}
/** @var Variable[] $variables */
$variables = $this -> betterNodeFinder -> find ( $nextNode , function ( \PhpParser\Node $node ) use ( $oldVariableName ) : bool {
if ( ! $node instanceof \PhpParser\Node\Expr\Variable ) {
return \false ;
}
return $this -> nodeNameResolver -> isName ( $node , $oldVariableName );
});
$processRenameVariables = $this -> processRenameVariable ( $variables , $oldVariableName , $newVariableName );
if ( ! $processRenameVariables ) {
return ;
}
$currentNode = $nextNode ;
$nextNode = $nextNode -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
$this -> replaceNextUsageVariable ( $currentNode , $nextNode , $oldVariableName , $newVariableName );
}
/**
* @ param Variable [] $variables
*/
private function processRenameVariable ( array $variables , string $oldVariableName , string $newVariableName ) : bool
{
foreach ( $variables as $variable ) {
$parent = $variable -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( $parent instanceof \PhpParser\Node\Expr\Assign && $this -> nodeComparator -> areNodesEqual ( $parent -> var , $variable ) && $this -> nodeNameResolver -> isName ( $parent -> var , $oldVariableName ) && ! $this -> nodeComparator -> areNodesEqual ( $parent -> expr , $variable )) {
return \false ;
}
$variable -> name = $newVariableName ;
}
return \true ;
2019-05-25 23:15:21 +00:00
}
}