Skip to content

Commit 4dce718

Browse files
authored
Deprecated STMT_KEY and remove its set attribute value (#7647)
1 parent 040c47f commit 4dce718

File tree

5 files changed

+9
-19
lines changed

5 files changed

+9
-19
lines changed

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,3 @@ parameters:
379379
-
380380
message: '#Property Rector\\PhpParser\\NodeTraverser\\AbstractImmutableNodeTraverser\:\:\$visitors \(list<PhpParser\\NodeVisitor>\) does not accept array<int\|string, PhpParser\\NodeVisitor>#'
381381
path: src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
382-
383-
# handle next
384-
- '#Fetching deprecated class constant STMT_KEY of class Rector\\NodeTypeResolver\\Node\\AttributeKey#'

src/Application/NodeAttributeReIndexer.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
use PhpParser\Node\Stmt\Switch_;
2323
use PhpParser\Node\Stmt\TryCatch;
2424
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
25-
use Rector\NodeTypeResolver\Node\AttributeKey;
2625

2726
final class NodeAttributeReIndexer
2827
{
29-
public static function reIndexStmtKeyNodeAttributes(Node $node): ?Node
28+
public static function reIndexStmtsKeys(Node $node): ?Node
3029
{
3130
if (! $node instanceof StmtsAwareInterface && ! $node instanceof ClassLike && ! $node instanceof Declare_ && ! $node instanceof Block) {
3231
return null;
@@ -37,19 +36,13 @@ public static function reIndexStmtKeyNodeAttributes(Node $node): ?Node
3736
}
3837

3938
$node->stmts = array_values($node->stmts);
40-
41-
// re-index stmt key under current node
42-
foreach ($node->stmts as $key => $childStmt) {
43-
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);
44-
}
45-
4639
return $node;
4740
}
4841

49-
public static function reIndexNodeAttributes(Node $node, bool $reIndexStmtKey = true): ?Node
42+
public static function reIndexNodeAttributes(Node $node, bool $reIndexStmtsKeys = true): ?Node
5043
{
51-
if ($reIndexStmtKey) {
52-
self::reIndexStmtKeyNodeAttributes($node);
44+
if ($reIndexStmtsKeys) {
45+
self::reIndexStmtsKeys($node);
5346
}
5447

5548
if ($node instanceof If_) {

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ final class AttributeKey
162162
public const IS_BYREF_RETURN = 'is_byref_return';
163163

164164
/**
165+
* @deprecated This value can change, as based on default input keys. Use existing array keys instead.
166+
*
165167
* @var string
166168
*/
167169
public const STMT_KEY = 'stmt_key';

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ final class StmtKeyNodeVisitor extends NodeVisitorAbstract implements ScopeResol
1313
{
1414
public function enterNode(Node $node): ?Node
1515
{
16-
return NodeAttributeReIndexer::reIndexStmtKeyNodeAttributes($node);
16+
return NodeAttributeReIndexer::reIndexStmtsKeys($node);
1717
}
1818
}

tests/Issues/IndexedStmt/Source/ChangeLastIndex1Rector.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Scalar\String_;
99
use PhpParser\Node\Stmt\Expression;
10-
use PhpParser\NodeVisitor;
1110
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
12-
use Rector\NodeTypeResolver\Node\AttributeKey;
1311
use Rector\Rector\AbstractRector;
1412
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1513

@@ -34,8 +32,8 @@ public function refactor(Node $node)
3432
return null;
3533
}
3634

37-
foreach ($node->stmts as $stmt) {
38-
if ($stmt->getAttribute(AttributeKey::STMT_KEY) === 1 && $stmt instanceof Expression && $stmt->expr instanceof String_ && $stmt->expr->value === 'with index 2') {
35+
foreach ($node->stmts as $key => $stmt) {
36+
if ($key === 1 && $stmt instanceof Expression && $stmt->expr instanceof String_ && $stmt->expr->value === 'with index 2') {
3937
$stmt->expr->value = 'final index';
4038
return $node;
4139
}

0 commit comments

Comments
 (0)