Skip to content

Commit daf10af

Browse files
committed
make use of AbstractRector and scope fetcher
1 parent bd6a6af commit daf10af

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
2121
use Rector\NodeTypeResolver\Node\AttributeKey;
2222
use Rector\PhpParser\Parser\InlineCodeParser;
23-
use Rector\Rector\AbstractScopeAwareRector;
23+
use Rector\PHPStan\ScopeFetcher;
24+
use Rector\Rector\AbstractRector;
2425
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2526
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2627

@@ -29,7 +30,7 @@
2930
*
3031
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\DowngradeStreamIsattyRectorTest
3132
*/
32-
final class DowngradeStreamIsattyRector extends AbstractScopeAwareRector
33+
final class DowngradeStreamIsattyRector extends AbstractRector
3334
{
3435
private ?Closure $cachedClosure = null;
3536

@@ -99,7 +100,7 @@ public function getNodeTypes(): array
99100
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
100101
* @return Node[]|null
101102
*/
102-
public function refactorWithScope(Node $node, Scope $scope): ?array
103+
public function refactor(Node $node): ?array
103104
{
104105
$expr = $this->exprInTopStmtMatcher->match(
105106
$node,
@@ -128,6 +129,8 @@ function (Node $subNode): bool {
128129

129130
$function = $this->createClosure();
130131

132+
$scope = ScopeFetcher::fetch($node);
133+
131134
$variable = new Variable($this->variableNaming->createCountedValueName('streamIsatty', $scope));
132135
$assign = new Assign($variable, $function);
133136

rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
use PhpParser\Node\Stmt\ClassConst;
1414
use PhpParser\Node\Stmt\ClassLike;
1515
use PHPStan\Analyser\MutatingScope;
16-
use PHPStan\Analyser\Scope;
1716
use PHPStan\Type\Type;
1817
use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer;
1918
use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory;
2019
use Rector\PhpParser\AstResolver;
2120
use Rector\PhpParser\Node\BetterNodeFinder;
22-
use Rector\Rector\AbstractScopeAwareRector;
21+
use Rector\PHPStan\ScopeFetcher;
22+
use Rector\Rector\AbstractRector;
2323
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2424
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2525
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -29,7 +29,7 @@
2929
*
3030
* @see \Rector\Tests\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector\DowngradeArraySpreadRectorTest
3131
*/
32-
final class DowngradeArraySpreadRector extends AbstractScopeAwareRector
32+
final class DowngradeArraySpreadRector extends AbstractRector
3333
{
3434
public function __construct(
3535
private readonly ArrayMergeFromArraySpreadFactory $arrayMergeFromArraySpreadFactory,
@@ -98,7 +98,7 @@ public function getNodeTypes(): array
9898
/**
9999
* @param Array_|ClassConst $node
100100
*/
101-
public function refactorWithScope(Node $node, Scope $scope): ?Node
101+
public function refactor(Node $node): ?Node
102102
{
103103
if ($node instanceof ClassConst) {
104104
return $this->refactorUnderClassConst($node);
@@ -109,6 +109,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
109109
}
110110

111111
/** @var MutatingScope $scope */
112+
$scope = ScopeFetcher::fetch($node);
113+
112114
return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope);
113115
}
114116

rules/DowngradePhp80/Rector/Catch_/DowngradeNonCapturingCatchesRector.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Variable;
99
use PhpParser\Node\Stmt\Catch_;
10-
use PHPStan\Analyser\Scope;
1110
use Rector\Naming\Naming\VariableNaming;
12-
use Rector\Rector\AbstractScopeAwareRector;
11+
use Rector\PHPStan\ScopeFetcher;
12+
use Rector\Rector\AbstractRector;
1313
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1414
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1515

@@ -18,7 +18,7 @@
1818
*
1919
* @see \Rector\Tests\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector\DowngradeNonCapturingCatchesRectorTest
2020
*/
21-
final class DowngradeNonCapturingCatchesRector extends AbstractScopeAwareRector
21+
final class DowngradeNonCapturingCatchesRector extends AbstractRector
2222
{
2323
public function __construct(
2424
private readonly VariableNaming $variableNaming
@@ -72,12 +72,14 @@ public function getNodeTypes(): array
7272
/**
7373
* @param Catch_ $node
7474
*/
75-
public function refactorWithScope(Node $node, Scope $scope): ?Node
75+
public function refactor(Node $node): ?Node
7676
{
7777
if ($node->var instanceof Variable) {
7878
return null;
7979
}
8080

81+
$scope = ScopeFetcher::fetch($node);
82+
8183
$exceptionVarName = $this->variableNaming->createCountedValueName('exception', $scope);
8284
$node->var = new Variable($exceptionVarName);
8385

rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
use PHPStan\Analyser\Scope;
3030
use Rector\NodeTypeResolver\Node\AttributeKey;
3131
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
32-
use Rector\Rector\AbstractScopeAwareRector;
32+
use Rector\PHPStan\ScopeFetcher;
33+
use Rector\Rector\AbstractRector;
3334
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3435
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
3536

@@ -38,7 +39,7 @@
3839
*
3940
* @see \Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\DowngradeMatchToSwitchRectorTest
4041
*/
41-
final class DowngradeMatchToSwitchRector extends AbstractScopeAwareRector
42+
final class DowngradeMatchToSwitchRector extends AbstractRector
4243
{
4344
public function __construct(
4445
private readonly AnonymousFunctionFactory $anonymousFunctionFactory
@@ -99,12 +100,14 @@ public function getNodeTypes(): array
99100
/**
100101
* @param Echo_|Expression|Return_ $node
101102
*/
102-
public function refactorWithScope(Node $node, Scope $scope): ?Node
103+
public function refactor(Node $node): ?Node
103104
{
104105
/** @var Match_|null $match */
105106
$match = null;
106107
$hasChanged = false;
107108

109+
$scope = ScopeFetcher::fetch($node);
110+
108111
$this->traverseNodesWithCallable(
109112
$node,
110113
function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
@@ -180,6 +183,7 @@ function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
180183
return null;
181184
}
182185

186+
$scope = ScopeFetcher::fetch($node);
183187
if (! $this->isEqualScope($match, $scope)) {
184188
return null;
185189
}
@@ -191,6 +195,7 @@ function (Node $subNode) use ($node, &$match, &$hasChanged, $scope) {
191195

192196
private function isEqualScope(Match_ $match, ?Scope $containerScope): bool
193197
{
198+
194199
$matchScope = $match->getAttribute(AttributeKey::SCOPE);
195200
if (! $matchScope instanceof Scope) {
196201
return false;

rules/DowngradePhp81/Rector/Array_/DowngradeArraySpreadStringKeyRector.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Expr\ArrayItem;
1010
use PHPStan\Analyser\MutatingScope;
11-
use PHPStan\Analyser\Scope;
1211
use PHPStan\Type\ArrayType;
1312
use PHPStan\Type\IntegerType;
1413
use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer;
1514
use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory;
16-
use Rector\Rector\AbstractScopeAwareRector;
15+
use Rector\PHPStan\ScopeFetcher;
16+
use Rector\Rector\AbstractRector;
1717
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1818
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1919

@@ -22,7 +22,7 @@
2222
*
2323
* @see \Rector\Tests\DowngradePhp81\Rector\Array_\DowngradeArraySpreadStringKeyRector\DowngradeArraySpreadStringKeyRectorTest
2424
*/
25-
final class DowngradeArraySpreadStringKeyRector extends AbstractScopeAwareRector
25+
final class DowngradeArraySpreadStringKeyRector extends AbstractRector
2626
{
2727
public function __construct(
2828
private readonly ArrayMergeFromArraySpreadFactory $arrayMergeFromArraySpreadFactory,
@@ -65,7 +65,7 @@ public function getNodeTypes(): array
6565
/**
6666
* @param Array_ $node
6767
*/
68-
public function refactorWithScope(Node $node, Scope $scope): ?Node
68+
public function refactor(Node $node): ?Node
6969
{
7070
if (! $this->arraySpreadAnalyzer->isArrayWithUnpack($node)) {
7171
return null;
@@ -76,6 +76,8 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
7676
}
7777

7878
/** @var MutatingScope $scope */
79+
$scope = ScopeFetcher::fetch($node);
80+
7981
return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope);
8082
}
8183

0 commit comments

Comments
 (0)