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_ ;
2021-07-02 00:08:09 +00:00
use RectorPrefix20210702\Nette\Utils\Strings ;
2019-05-25 23:15:21 +00:00
use PhpParser\Node ;
2020-12-21 02:12:42 +00:00
use PhpParser\Node\Expr\Variable ;
2019-05-25 23:15:21 +00:00
use PhpParser\Node\Stmt\Catch_ ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
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
}
/**
* @ param Catch_ $node
*/
2021-05-10 22:23:08 +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-05-09 20:15:43 +00:00
if ( ! $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 );
2021-07-02 00:08:09 +00:00
$newVariableName = \RectorPrefix20210702\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 );
$isFoundInPrevious = ( bool ) $this -> betterNodeFinder -> findFirstPrevious ( $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
{
2021-05-10 22:23:08 +00:00
$this -> traverseNodesWithCallable ( $catch -> stmts , function ( \PhpParser\Node $node ) use ( $oldVariableName , $newVariableName ) : void {
if ( ! $node instanceof \PhpParser\Node\Expr\Variable ) {
2020-12-21 02:12:42 +00:00
return ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeNameResolver -> isName ( $node , $oldVariableName )) {
2019-05-25 23:15:21 +00:00
return ;
}
$node -> name = $newVariableName ;
});
}
}