Skip to content

Commit 9116b3e

Browse files
authored
[StmtsAware] Add Declare_ to NodeGroup::STMTS_AWARE to allow process its stmts (#7791)
* [DeadCode] Add Declare_ to NodeGroup::STMTS_AWARE to allow remove Declare_ stmts * [DeadCode] Add Declare_ to NodeGroup::STMTS_AWARE to allow remove Declare_ stmts * final touch: add to phpstan alias
1 parent f8c7583 commit 9116b3e

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ parameters:
1010

1111
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
1212
typeAliases:
13-
StmtsAware: \PhpParser\Node\Stmt\Block | \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_ | \Rector\PhpParser\Node\FileNode
13+
StmtsAware: \PhpParser\Node\Stmt\Block | \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_ | \Rector\PhpParser\Node\FileNode | \PhpParser\Node\Stmt\Declare_
1414

1515
# requires exact closure types
1616
checkMissingCallableSignature: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;
4+
5+
declare(ticks=1) {
6+
echo 'test';
7+
8+
throw new \Exception();
9+
10+
echo 'unreachable';
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\Fixture;
18+
19+
declare(ticks=1) {
20+
echo 'test';
21+
22+
throw new \Exception();
23+
}
24+
25+
?>

src/Application/NodeAttributeReIndexer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PhpParser\Node\FunctionLike;
1616
use PhpParser\Node\Stmt\ClassLike;
1717
use PhpParser\Node\Stmt\ClassMethod;
18-
use PhpParser\Node\Stmt\Declare_;
1918
use PhpParser\Node\Stmt\Function_;
2019
use PhpParser\Node\Stmt\If_;
2120
use PhpParser\Node\Stmt\Switch_;
@@ -66,7 +65,7 @@ public static function reIndexNodeAttributes(Node $node): ?Node
6665

6766
private static function reIndexStmtsKeys(Node $node): ?Node
6867
{
69-
if (! NodeGroup::isStmtAwareNode($node) && ! $node instanceof ClassLike && ! $node instanceof Declare_) {
68+
if (! NodeGroup::isStmtAwareNode($node) && ! $node instanceof ClassLike) {
7069
return null;
7170
}
7271

src/PhpParser/Enum/NodeGroup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Stmt\Class_;
1313
use PhpParser\Node\Stmt\ClassConst;
1414
use PhpParser\Node\Stmt\ClassMethod;
15+
use PhpParser\Node\Stmt\Declare_;
1516
use PhpParser\Node\Stmt\Do_;
1617
use PhpParser\Node\Stmt\Else_;
1718
use PhpParser\Node\Stmt\ElseIf_;
@@ -56,6 +57,7 @@ final class NodeGroup
5657
TryCatch::class,
5758
While_::class,
5859
FileNode::class,
60+
Declare_::class,
5961
];
6062

6163
/**

0 commit comments

Comments
 (0)