Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[deprecation] Deprecate AbstractScopeAwareRector in favor of single AbstractRector #6425

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Upgrading from Rector 1.x to 2.0

## Upgrade for custom Rules writers

### 1. `AbstractScopeAwareRector` is removed, use `AbstractRector` instead

The `Rector\Rector\AbstractScopeAwareRector` was too granular to fetch single helper object. It made creating new custom rules ambiguous, one layer more complex and confusing. This class has been removed in favor of standard `AbstractRector`. The `Scope` object can be fetched via `ScopeFetcher`.

**Before**

```php
use Rector\Rector\AbstractScopeAwareRector;

final class SimpleRector extends AbstractScopeAwareRector
{
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
// ...
}
}
```

**After**

```php
use Rector\Rector\AbstractRector;
use Rector\PHPStan\ScopeFetcher;

final class SimpleRector extends AbstractRector
{
public function refactor(Node $node): ?Node
{
if (...) {
// this allow to fetch scope only when needed
$scope = ScopeFetcher::fetch($node);
}

// ...
}
}
```
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,8 @@ parameters:
# more advanced usage, but not always working
# see https://github.com/rectorphp/rector-src/actions/runs/11798721617/job/32865546672?pr=6422#step:5:110
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'

# allowed internally only
-
message: '#Fetching (deprecated )?class constant (.*?) of (deprecated )?class (Rector\\Set\\ValueObject\\DowngradeLevelSetList|Rector\\Symfony\\Set\\(.*?))#'
path: src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php80\NodeResolver\ArgumentSorter;
use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\OptionalParametersAfterRequiredRectorTest
*/
final class OptionalParametersAfterRequiredRector extends AbstractScopeAwareRector
final class OptionalParametersAfterRequiredRector extends AbstractRector
{
/**
* @var string
Expand Down Expand Up @@ -86,10 +87,10 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_|New_|MethodCall|StaticCall|FuncCall $node
*/
public function refactorWithScope(
Node $node,
Scope $scope
): ClassMethod|Function_|null|New_|MethodCall|StaticCall|FuncCall {
public function refactor(Node $node): ClassMethod|Function_|null|New_|MethodCall|StaticCall|FuncCall
{
$scope = ScopeFetcher::fetch($node);

if ($node instanceof ClassMethod || $node instanceof Function_) {
return $this->refactorClassMethodOrFunction($node, $scope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\AstResolver;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -32,7 +33,7 @@
/**
* @see \Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\SimplifyEmptyCheckOnEmptyArrayRectorTest
*/
final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractScopeAwareRector
final class SimplifyEmptyCheckOnEmptyArrayRector extends AbstractRector
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer,
Expand Down Expand Up @@ -76,8 +77,9 @@ public function getNodeTypes(): array
/**
* @param Empty_|BooleanNot $node $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
if ($node instanceof BooleanNot) {
if ($node->expr instanceof Empty_ && $this->isAllowedExpr($node->expr->expr, $scope)) {
return new NotIdentical($node->expr->expr, new Array_());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector\TernaryFalseExpressionToIfRectorTest
*/
final class TernaryFalseExpressionToIfRector extends AbstractScopeAwareRector
final class TernaryFalseExpressionToIfRector extends AbstractRector
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer
Expand Down Expand Up @@ -67,7 +66,7 @@ public function getNodeTypes(): array
/**
* @param Expression $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Ternary) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest
*/
final class ForRepeatedCountToOwnVariableRector extends AbstractScopeAwareRector
final class ForRepeatedCountToOwnVariableRector extends AbstractRector
{
/**
* @var string
Expand Down Expand Up @@ -76,8 +76,10 @@ public function getNodeTypes(): array
* @param For_ $node
* @return Stmt[]|null
*/
public function refactorWithScope(Node $node, Scope $scope): ?array
public function refactor(Node $node): ?array
{
$scope = ScopeFetcher::fetch($node);

if ($scope->hasVariableType(self::COUNTER_NAME)->yes()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersion;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use ReflectionException;
Expand All @@ -23,7 +22,7 @@
/**
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\FunctionFirstClassCallableRector\FunctionFirstClassCallableRectorTest
*/
final class FunctionFirstClassCallableRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
final class FunctionFirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -62,7 +61,7 @@ public function getNodeTypes(): array
return [FuncCall::class];
}

public function refactorWithScope(Node $node, Scope $scope): ?FuncCall
public function refactor(Node $node): ?FuncCall
{
if (! $node instanceof FuncCall) {
return null;
Expand Down
9 changes: 5 additions & 4 deletions rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\Analyser\Scope;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleAssignRector\RemoveDoubleAssignRectorTest
*/
final class RemoveDoubleAssignRector extends AbstractScopeAwareRector
final class RemoveDoubleAssignRector extends AbstractRector
{
public function __construct(
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
Expand Down Expand Up @@ -68,8 +68,9 @@ public function getNodeTypes(): array
/**
* @param Foreach_|FileWithoutNamespace|If_|Namespace_|ClassMethod|Function_|Closure $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
$stmts = $node->stmts;
if ($stmts === null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\RemoveUnusedVariableAssignRectorTest
*/
final class RemoveUnusedVariableAssignRector extends AbstractScopeAwareRector
final class RemoveUnusedVariableAssignRector extends AbstractRector
{
public function __construct(
private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getNodeTypes(): array
/**
* @param ClassMethod|Function_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Function_
public function refactor(Node $node): null|ClassMethod|Function_
{
$stmts = $node->stmts;
if ($stmts === null || $stmts === []) {
Expand All @@ -104,6 +105,8 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Fu
/** @var Assign $assign */
$assign = $currentStmt->expr;

$scope = ScopeFetcher::fetch($node);

if ($this->hasCallLikeInAssignExpr($assign, $scope)) {
// clean safely
$cleanAssignedExpr = $this->cleanCastedExpr($assign->expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeManipulator\ClassConstManipulator;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\RemoveUnusedPrivateClassConstantRectorTest
*/
final class RemoveUnusedPrivateClassConstantRector extends AbstractScopeAwareRector
final class RemoveUnusedPrivateClassConstantRector extends AbstractRector
{
public function __construct(
private readonly ClassConstManipulator $classConstManipulator,
Expand Down Expand Up @@ -64,9 +64,9 @@ public function getNodeTypes(): array
/**
* @param ClassConst $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?int
public function refactor(Node $node): ?int
{
if ($this->shouldSkipClassConst($node, $scope)) {
if ($this->shouldSkipClassConst($node)) {
return null;
}

Expand All @@ -82,7 +82,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?int
return NodeTraverser::REMOVE_NODE;
}

private function shouldSkipClassConst(ClassConst $classConst, Scope $scope): bool
private function shouldSkipClassConst(ClassConst $classConst): bool
{
if (! $classConst->isPrivate()) {
return true;
Expand All @@ -92,6 +92,8 @@ private function shouldSkipClassConst(ClassConst $classConst, Scope $scope): boo
return true;
}

$scope = ScopeFetcher::fetch($classConst);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -23,7 +23,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\RemoveUnusedPrivateMethodRectorTest
*/
final class RemoveUnusedPrivateMethodRector extends AbstractScopeAwareRector
final class RemoveUnusedPrivateMethodRector extends AbstractRector
{
public function __construct(
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
Expand Down Expand Up @@ -75,8 +75,9 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$scope = ScopeFetcher::fetch($node);
$classMethods = $node->getMethods();

if ($classMethods === []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Reflection\ClassReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand All @@ -20,7 +19,7 @@
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
Expand All @@ -32,7 +31,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
*/
final class RemoveUnusedPromotedPropertyRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly PropertyFetchFinder $propertyFetchFinder,
Expand Down Expand Up @@ -96,7 +95,7 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
Expand Down
Loading