Skip to content

Commit afb1a45

Browse files
committed
use explicit array node re-index as best practice to always keep node keys as list
1 parent 4dce718 commit afb1a45

6 files changed

Lines changed: 24 additions & 27 deletions

File tree

rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ public function refactor(Node $node): ?Node
8282
return null;
8383
}
8484

85+
// remove first arg
86+
$originalArgs = $node->getArgs();
87+
array_shift($originalArgs);
88+
8589
$methodCall = $this->arrayCallableToMethodCallFactory->create($firstArgValue);
8690
if (! $methodCall instanceof MethodCall) {
8791
return null;
8892
}
8993

90-
$originalArgs = $node->args;
91-
unset($originalArgs[0]);
92-
9394
$methodCall->args = $originalArgs;
9495
return $methodCall;
9596
}

rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ public function refactor(Node $node): ?Node
113113

114114
// remove current Stmt if will be overridden in next stmt
115115
unset($node->stmts[$key]);
116+
116117
$hasChanged = true;
117118
}
118119

119120
if (! $hasChanged) {
120121
return null;
121122
}
122123

124+
// update array keys to fit printer
125+
$node->stmts = array_values($node->stmts);
126+
123127
return $node;
124128
}
125129

rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Rector\Php85\Rector\FuncCall;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Arg;
9-
use PhpParser\Node\Expr\CallLike;
108
use PhpParser\Node\Expr\FuncCall;
119
use PhpParser\Node\Expr\MethodCall;
1210
use PhpParser\Node\Identifier;
@@ -58,6 +56,11 @@ public function getNodeTypes(): array
5856
*/
5957
public function refactor(Node $node): ?Node
6058
{
59+
// Cannot handle variadic args
60+
if ($node->isFirstClassCallable()) {
61+
return null;
62+
}
63+
6164
if ($node instanceof FuncCall && ! $this->isName($node->name, 'finfo_buffer')) {
6265
return null;
6366
}
@@ -82,10 +85,7 @@ public function provideMinPhpVersion(): int
8285
return PhpVersionFeature::DEPRECATE_FINFO_BUFFER_CONTEXT;
8386
}
8487

85-
/**
86-
* @param FuncCall|MethodCall $callLike
87-
*/
88-
private function removeContextArg(CallLike $callLike): bool
88+
private function removeContextArg(FuncCall|MethodCall $callLike): bool
8989
{
9090
// In `finfo::buffer` method calls, the first parameter, compared to `finfo_buffer`, does not exist.
9191
$methodArgCorrection = 0;
@@ -97,15 +97,7 @@ private function removeContextArg(CallLike $callLike): bool
9797
return false;
9898
}
9999

100-
// Cannot handle variadic args
101-
foreach ($callLike->args as $position => $arg) {
102-
if (! $arg instanceof Arg) {
103-
return false;
104-
}
105-
}
106-
107-
/** @var array<Arg> $args */
108-
$args = $callLike->args;
100+
$args = $callLike->getArgs();
109101

110102
// Argument 3 ($flags) and argument 4 ($context) are optional, thus named parameters must be considered
111103
if (! $this->argsAnalyzer->hasNamedArg($args)) {
@@ -115,13 +107,20 @@ private function removeContextArg(CallLike $callLike): bool
115107

116108
unset($callLike->args[3 + $methodArgCorrection]);
117109

110+
// update indexed to make printer work as expected
111+
$callLike->args = array_values($callLike->args);
112+
118113
return true;
119114
}
120115

116+
// process named arguments
121117
foreach ($args as $position => $arg) {
122-
if ($arg->name instanceof Identifier && $arg->name->name === 'context') {
118+
if ($arg->name instanceof Identifier && $this->isName($arg->name, 'context')) {
123119
unset($callLike->args[$position]);
124120

121+
// update indexed to make printer work as expected
122+
$callLike->args = array_values($callLike->args);
123+
125124
return true;
126125
}
127126
}

src/Application/ChangedNodeScopeRefresher.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
5858
throw new ShouldNotHappenException($errorMessage);
5959
}
6060

61-
// reindex stmt_key already covered on StmtKeyNodeVisitor on next processNodes()
62-
// so set flag $reIndexStmtKey to false to avoid double loop
63-
NodeAttributeReIndexer::reIndexNodeAttributes($node, false);
64-
6561
$stmts = $this->resolveStmts($node);
6662
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
6763
}

src/DependencyInjection/LazyContainerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ final class LazyContainerFactory
243243
GlobalVariableNodeVisitor::class,
244244
NameNodeVisitor::class,
245245
StaticVariableNodeVisitor::class,
246-
StmtKeyNodeVisitor::class,
246+
// StmtKeyNodeVisitor::class,
247247
];
248248

249249
/**

src/Rector/AbstractRector.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use PHPStan\Type\ObjectType;
2121
use PHPStan\Type\Type;
2222
use Rector\Application\ChangedNodeScopeRefresher;
23-
use Rector\Application\NodeAttributeReIndexer;
2423
use Rector\Application\Provider\CurrentFileProvider;
2524
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
2625
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
@@ -139,8 +138,6 @@ final public function enterNode(Node $node): int|Node|null
139138
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
140139
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
141140

142-
NodeAttributeReIndexer::reIndexNodeAttributes($node);
143-
144141
$refactoredNode = $this->refactor($node);
145142

146143
// nothing to change → continue

0 commit comments

Comments
 (0)