2022-05-04 15:20:50 +00:00
< ? php
declare ( strict_types = 1 );
2024-01-02 02:40:38 +00:00
namespace Rector\Rector ;
2022-05-04 15:20:50 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
2023-05-22 13:31:04 +00:00
use PhpParser\NodeTraverser ;
2022-08-09 13:39:17 +00:00
use PHPStan\Analyser\MutatingScope ;
2022-06-06 17:12:56 +00:00
use PHPStan\Analyser\Scope ;
2024-01-02 02:40:38 +00:00
use Rector\Contract\Rector\ScopeAwareRectorInterface ;
use Rector\Exception\ShouldNotHappenException ;
2022-06-06 17:12:56 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2024-01-02 02:40:38 +00:00
abstract class AbstractScopeAwareRector extends \Rector\Rector\AbstractRector implements ScopeAwareRectorInterface
2022-05-04 15:20:50 +00:00
{
2022-07-03 14:22:15 +00:00
/**
2022-05-04 15:20:50 +00:00
* Process Node of matched type with its PHPStan scope
2023-05-22 13:31:04 +00:00
* @ return Node | Node [] | null | NodeTraverser ::*
2022-05-04 15:20:50 +00:00
*/
2022-06-07 08:22:29 +00:00
public function refactor ( Node $node )
2022-05-04 15:20:50 +00:00
{
2023-01-18 12:08:19 +00:00
/** @var MutatingScope|null $currentScope */
2023-07-19 10:44:22 +00:00
$currentScope = $node -> getAttribute ( AttributeKey :: SCOPE );
2023-01-18 12:08:19 +00:00
if ( ! $currentScope instanceof Scope ) {
2023-07-09 18:49:55 +00:00
$errorMessage = \sprintf ( 'Scope not available on "%s" node, but is required by a refactorWithScope() method of "%s" rule. Fix scope refresh on changed nodes first' , \get_class ( $node ), static :: class );
2022-06-07 08:22:29 +00:00
throw new ShouldNotHappenException ( $errorMessage );
2022-05-04 15:20:50 +00:00
}
2023-01-18 12:08:19 +00:00
return $this -> refactorWithScope ( $node , $currentScope );
2022-05-04 15:20:50 +00:00
}
}