Skip to content

Commit 1183ef0

Browse files
[stmts-aware] Use NodeGroup::STMTS_AWARE over StmtsAwareInterface (#346)
* [stmts-aware] Use NodeGroup::STMTS_AWARE over StmtsAwareInterface * [stmts-aware] Use NodeGroup::STMTS_AWARE over StmtsAwareInterface * [ci-review] Rector Rectify * cleanup ignored phpstan notice --------- Co-authored-by: GitHub Action <[email protected]>
1 parent dd62a5e commit 1183ef0

File tree

9 files changed

+35
-32
lines changed

9 files changed

+35
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"phpstan/phpstan": "^2.1.32",
1212
"phpstan/phpstan-webmozart-assert": "^2.0",
1313
"phpunit/phpunit": "^11.5",
14-
"rector/rector-src": "dev-main",
14+
"rector/rector-src": "dev-tv-stmts-interface",
1515
"symplify/easy-coding-standard": "^12.3",
1616
"symplify/rule-doc-generator": "^12.2",
1717
"symplify/vendor-patches": "^11.5",

phpstan.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ parameters:
88

99
reportUnmatchedIgnoredErrors: false
1010

11+
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
12+
typeAliases:
13+
StmtsAware: \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_
14+
1115
# requires exact closure types
1216
checkMissingCallableSignature: true
1317

@@ -44,9 +48,6 @@ parameters:
4448

4549
- '#(.*?)\:\:refactor\(\) should return array<PhpParser\\Node\\Stmt>\|PhpParser\\Node\\Stmt\\ClassConst\|null but returns array<PhpParser\\Node\\Stmt>\|PhpParser\\Node\\Stmt\|null#'
4650

47-
# phpstan need to load rector nodes first to avoid this
48-
- '#Access to an undefined property Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#'
49-
5051
-
5152
message: '#Parameter \#1 \$attributeClass of class Rector\\DowngradePhp80\\ValueObject\\DowngradeAttributeToAnnotation constructor expects class\-string, string given#'
5253
path: "config/*"
@@ -66,7 +67,6 @@ parameters:
6667
identifier: argument.type
6768

6869
- '#Asking about instanceof PHPStan\\.* is not covered by backward compatibility promise#'
69-
- '#Method Rector\\(.*?)\\Rector\\FuncCall\\(.*?)::refactor\(\) never returns \d so it can be removed from the return type#'
7070

7171
# false positive intanceof in OR usage
7272
-

rules/DowngradePhp72/Rector/FuncCall/DowngradeStreamIsattyRector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
use PhpParser\Node\Stmt\Return_;
1515
use PhpParser\Node\Stmt\Switch_;
1616
use PHPStan\Analyser\Scope;
17-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1817
use Rector\Exception\ShouldNotHappenException;
1918
use Rector\Naming\Naming\VariableNaming;
2019
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
2120
use Rector\NodeTypeResolver\Node\AttributeKey;
21+
use Rector\PhpParser\Enum\NodeGroup;
2222
use Rector\PhpParser\Parser\InlineCodeParser;
2323
use Rector\PHPStan\ScopeFetcher;
2424
use Rector\Rector\AbstractRector;
@@ -93,11 +93,12 @@ public function run($stream)
9393
*/
9494
public function getNodeTypes(): array
9595
{
96-
return [StmtsAwareInterface::class, Switch_::class, Return_::class, Expression::class, Echo_::class];
96+
$stmtsAware = NodeGroup::STMTS_AWARE;
97+
return [...$stmtsAware, Switch_::class, Return_::class, Expression::class, Echo_::class];
9798
}
9899

99100
/**
100-
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
101+
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
101102
* @return Node[]|null
102103
*/
103104
public function refactor(Node $node): ?array

rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
use PhpParser\Node\Stmt\Return_;
2626
use PhpParser\Node\Stmt\Switch_;
2727
use PHPStan\Analyser\Scope;
28-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2928
use Rector\Naming\Naming\VariableNaming;
3029
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
3130
use Rector\NodeTypeResolver\Node\AttributeKey;
31+
use Rector\PhpParser\Enum\NodeGroup;
3232
use Rector\Rector\AbstractRector;
3333
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3434
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -80,11 +80,12 @@ public function run($items)
8080
*/
8181
public function getNodeTypes(): array
8282
{
83-
return [StmtsAwareInterface::class, Switch_::class, Return_::class, Expression::class, Echo_::class];
83+
$stmtsAware = NodeGroup::STMTS_AWARE;
84+
return [...$stmtsAware, Switch_::class, Return_::class, Expression::class, Echo_::class];
8485
}
8586

8687
/**
87-
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
88+
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
8889
* @return Node[]|null
8990
*/
9091
public function refactor(Node $node): ?array
@@ -143,7 +144,7 @@ private function resolveVariableFromCallLikeScope(CallLike $callLike, ?Scope $sc
143144
*/
144145
private function refactorArrayKeyFirst(
145146
FuncCall $funcCall,
146-
StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt
147+
Node|Switch_|Return_|Expression|Echo_ $stmt
147148
): null|array {
148149
$args = $funcCall->getArgs();
149150
if (! isset($args[0])) {
@@ -182,7 +183,7 @@ private function refactorArrayKeyFirst(
182183
*/
183184
private function refactorArrayKeyLast(
184185
FuncCall $funcCall,
185-
StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt
186+
Node|Switch_|Return_|Expression|Echo_ $stmt
186187
): null|array {
187188
$args = $funcCall->getArgs();
188189
$firstArg = $args[0] ?? null;
@@ -216,7 +217,7 @@ private function refactorArrayKeyLast(
216217

217218
$resetExpression = new Expression($this->nodeFactory->createFuncCall('reset', [$array]));
218219

219-
if ($stmt instanceof StmtsAwareInterface) {
220+
if (property_exists($stmt, 'stmts')) {
220221
$stmt->stmts = array_merge([$resetExpression], $stmt->stmts);
221222
} elseif (! $stmt instanceof Return_) {
222223
$newStmts[] = $resetExpression;
@@ -228,7 +229,7 @@ private function refactorArrayKeyLast(
228229
private function resolvePrependNewStmt(
229230
Expr|Variable $array,
230231
FuncCall $funcCall,
231-
Stmt|StmtsAwareInterface $stmt
232+
Stmt|Node $stmt
232233
): Expression|If_ {
233234
if (! $stmt instanceof If_ || $stmt->cond instanceof FuncCall || ! $stmt->cond instanceof BooleanOr) {
234235
return new Expression($funcCall);

rules/DowngradePhp81/Rector/FuncCall/DowngradeArrayIsListRector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
use PhpParser\Node\Stmt\Return_;
1616
use PhpParser\Node\Stmt\Switch_;
1717
use PHPStan\Analyser\Scope;
18-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1918
use Rector\Exception\ShouldNotHappenException;
2019
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
2120
use Rector\NodeTypeResolver\Node\AttributeKey;
21+
use Rector\PhpParser\Enum\NodeGroup;
2222
use Rector\PhpParser\Parser\InlineCodeParser;
2323
use Rector\Rector\AbstractRector;
2424
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -78,11 +78,12 @@ public function getRuleDefinition(): RuleDefinition
7878
*/
7979
public function getNodeTypes(): array
8080
{
81-
return [StmtsAwareInterface::class, Switch_::class, Return_::class, Expression::class, Echo_::class];
81+
$stmtsAware = NodeGroup::STMTS_AWARE;
82+
return [...$stmtsAware, Switch_::class, Return_::class, Expression::class, Echo_::class];
8283
}
8384

8485
/**
85-
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
86+
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
8687
* @return Node[]|null
8788
*/
8889
public function refactor(Node $node): ?array

rules/DowngradePhp81/Rector/StmtsAwareInterface/DowngradeSetAccessibleReflectionPropertyRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use PhpParser\Node\Stmt\Expression;
1919
use PhpParser\Node\Stmt\If_;
2020
use PhpParser\Node\Stmt\Return_;
21-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2221
use Rector\Naming\Naming\VariableNaming;
22+
use Rector\PhpParser\Enum\NodeGroup;
2323
use Rector\PHPStan\ScopeFetcher;
2424
use Rector\Rector\AbstractRector;
2525
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -81,11 +81,11 @@ public function run($object)
8181
*/
8282
public function getNodeTypes(): array
8383
{
84-
return [StmtsAwareInterface::class];
84+
return NodeGroup::STMTS_AWARE;
8585
}
8686

8787
/**
88-
* @param StmtsAwareInterface $node
88+
* @param StmtsAware $node
8989
*/
9090
public function refactor(Node $node): ?Node
9191
{

rules/DowngradePhp83/Rector/FuncCall/DowngradeJsonValidateRector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use PhpParser\Node\Stmt\Return_;
1616
use PhpParser\Node\Stmt\Switch_;
1717
use PHPStan\Analyser\Scope;
18-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1918
use Rector\Exception\ShouldNotHappenException;
2019
use Rector\Naming\Naming\VariableNaming;
2120
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
2221
use Rector\NodeTypeResolver\Node\AttributeKey;
22+
use Rector\PhpParser\Enum\NodeGroup;
2323
use Rector\PhpParser\Parser\InlineCodeParser;
2424
use Rector\PHPStan\ScopeFetcher;
2525
use Rector\Rector\AbstractRector;
@@ -84,11 +84,12 @@ public function getRuleDefinition(): RuleDefinition
8484
*/
8585
public function getNodeTypes(): array
8686
{
87-
return [StmtsAwareInterface::class, Switch_::class, Return_::class, Expression::class, Echo_::class];
87+
$stmtsAware = NodeGroup::STMTS_AWARE;
88+
return [...$stmtsAware, Switch_::class, Return_::class, Expression::class, Echo_::class];
8889
}
8990

9091
/**
91-
* @param StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $node
92+
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
9293
* @return Node[]|null
9394
*/
9495
public function refactor(Node $node): ?array

rules/DowngradePhp85/Rector/StmtsAwareInterface/DowngradePipeOperatorRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use PhpParser\Node\Expr\StaticCall;
1515
use PhpParser\Node\Expr\Variable;
1616
use PhpParser\Node\Stmt\Expression;
17-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1817
use Rector\NodeFactory\NamedVariableFactory;
18+
use Rector\PhpParser\Enum\NodeGroup;
1919
use Rector\Rector\AbstractRector;
2020
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2121
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -67,11 +67,11 @@ public function getRuleDefinition(): RuleDefinition
6767

6868
public function getNodeTypes(): array
6969
{
70-
return [StmtsAwareInterface::class];
70+
return NodeGroup::STMTS_AWARE;
7171
}
7272

7373
/**
74-
* @param StmtsAwareInterface $node
74+
* @param StmtsAware $node
7575
*/
7676
public function refactor(Node $node): ?Node
7777
{

src/NodeAnalyzer/ExprInTopStmtMatcher.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use PhpParser\Node\Stmt\Switch_;
1818
use PhpParser\Node\Stmt\While_;
1919
use PHPStan\Analyser\Scope;
20-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
2120
use Rector\NodeTypeResolver\Node\AttributeKey;
2221
use Rector\PhpParser\Node\BetterNodeFinder;
2322

@@ -35,7 +34,7 @@ public function __construct(
3534
/**
3635
* @param callable(Node $node): bool $filter
3736
*/
38-
public function match(StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt, callable $filter): null|Expr
37+
public function match(Node|Switch_|Return_|Expression|Echo_ $stmt, callable $filter): null|Expr
3938
{
4039
if ($stmt instanceof Closure) {
4140
return null;
@@ -80,7 +79,7 @@ public function match(StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt
8079
* @param callable(Node $node): bool $filter
8180
*/
8281
private function resolveOnReturnOrExpression(
83-
StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt,
82+
Node|Switch_|Return_|Expression|Echo_ $stmt,
8483
callable $filter
8584
): ?Expr {
8685
if (! $stmt instanceof Return_ && ! $stmt instanceof Expression) {
@@ -99,7 +98,7 @@ private function resolveOnReturnOrExpression(
9998
* @param callable(Node $node): bool $filter
10099
*/
101100
private function resolveExpr(
102-
StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt,
101+
Node|Switch_|Return_|Expression|Echo_ $stmt,
103102
array|Expr $exprs,
104103
callable $filter
105104
): ?Expr {
@@ -127,7 +126,7 @@ private function resolveExpr(
127126
* @param callable(Node $node): bool $filter
128127
*/
129128
private function resolveFromChildCond(
130-
StmtsAwareInterface|Switch_|Return_|Expression|Echo_ $stmt,
129+
Node|Switch_|Return_|Expression|Echo_ $stmt,
131130
callable $filter
132131
): null|Expr {
133132
if (! $stmt instanceof If_ && ! $stmt instanceof Switch_) {

0 commit comments

Comments
 (0)