diff --git a/phpstan.neon b/phpstan.neon index c23170a8ba6..38f660b0e2e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -379,6 +379,3 @@ parameters: - message: '#Property Rector\\PhpParser\\NodeTraverser\\AbstractImmutableNodeTraverser\:\:\$visitors \(list\) does not accept array#' path: src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php - - # handle next - - '#Fetching deprecated class constant STMT_KEY of class Rector\\NodeTypeResolver\\Node\\AttributeKey#' diff --git a/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php b/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php index ee7e828533c..a4d7bf6d4b9 100644 --- a/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php +++ b/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php @@ -82,14 +82,15 @@ public function refactor(Node $node): ?Node return null; } + // remove first arg + $originalArgs = $node->getArgs(); + array_shift($originalArgs); + $methodCall = $this->arrayCallableToMethodCallFactory->create($firstArgValue); if (! $methodCall instanceof MethodCall) { return null; } - $originalArgs = $node->args; - unset($originalArgs[0]); - $methodCall->args = $originalArgs; return $methodCall; } diff --git a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php index c85ac41cd20..2eacda698af 100644 --- a/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php +++ b/rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php @@ -113,6 +113,7 @@ public function refactor(Node $node): ?Node // remove current Stmt if will be overridden in next stmt unset($node->stmts[$key]); + $hasChanged = true; } @@ -120,6 +121,9 @@ public function refactor(Node $node): ?Node return null; } + // update array keys to fit printer + $node->stmts = array_values($node->stmts); + return $node; } diff --git a/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php b/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php index 70604712e08..7db9eb0514c 100644 --- a/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php +++ b/rules/Php85/Rector/FuncCall/RemoveFinfoBufferContextArgRector.php @@ -5,8 +5,6 @@ namespace Rector\Php85\Rector\FuncCall; use PhpParser\Node; -use PhpParser\Node\Arg; -use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Identifier; @@ -58,6 +56,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + // Cannot handle variadic args + if ($node->isFirstClassCallable()) { + return null; + } + if ($node instanceof FuncCall && ! $this->isName($node->name, 'finfo_buffer')) { return null; } @@ -82,10 +85,7 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::DEPRECATE_FINFO_BUFFER_CONTEXT; } - /** - * @param FuncCall|MethodCall $callLike - */ - private function removeContextArg(CallLike $callLike): bool + private function removeContextArg(FuncCall|MethodCall $callLike): bool { // In `finfo::buffer` method calls, the first parameter, compared to `finfo_buffer`, does not exist. $methodArgCorrection = 0; @@ -97,15 +97,7 @@ private function removeContextArg(CallLike $callLike): bool return false; } - // Cannot handle variadic args - foreach ($callLike->args as $position => $arg) { - if (! $arg instanceof Arg) { - return false; - } - } - - /** @var array $args */ - $args = $callLike->args; + $args = $callLike->getArgs(); // Argument 3 ($flags) and argument 4 ($context) are optional, thus named parameters must be considered if (! $this->argsAnalyzer->hasNamedArg($args)) { @@ -115,13 +107,20 @@ private function removeContextArg(CallLike $callLike): bool unset($callLike->args[3 + $methodArgCorrection]); + // update indexed to make printer work as expected + $callLike->args = array_values($callLike->args); + return true; } + // process named arguments foreach ($args as $position => $arg) { - if ($arg->name instanceof Identifier && $arg->name->name === 'context') { + if ($arg->name instanceof Identifier && $this->isName($arg->name, 'context')) { unset($callLike->args[$position]); + // update indexed to make printer work as expected + $callLike->args = array_values($callLike->args); + return true; } } diff --git a/src/Application/ChangedNodeScopeRefresher.php b/src/Application/ChangedNodeScopeRefresher.php index e20d7303296..19760be264d 100644 --- a/src/Application/ChangedNodeScopeRefresher.php +++ b/src/Application/ChangedNodeScopeRefresher.php @@ -58,10 +58,6 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc throw new ShouldNotHappenException($errorMessage); } - // reindex stmt_key already covered on StmtKeyNodeVisitor on next processNodes() - // so set flag $reIndexStmtKey to false to avoid double loop - NodeAttributeReIndexer::reIndexNodeAttributes($node, false); - $stmts = $this->resolveStmts($node); $this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope); } diff --git a/src/Application/NodeAttributeReIndexer.php b/src/Application/NodeAttributeReIndexer.php deleted file mode 100644 index f2051ed47fb..00000000000 --- a/src/Application/NodeAttributeReIndexer.php +++ /dev/null @@ -1,89 +0,0 @@ -stmts === null) { - return null; - } - - $node->stmts = array_values($node->stmts); - - // re-index stmt key under current node - foreach ($node->stmts as $key => $childStmt) { - $childStmt->setAttribute(AttributeKey::STMT_KEY, $key); - } - - return $node; - } - - public static function reIndexNodeAttributes(Node $node, bool $reIndexStmtKey = true): ?Node - { - if ($reIndexStmtKey) { - self::reIndexStmtKeyNodeAttributes($node); - } - - if ($node instanceof If_) { - $node->elseifs = array_values($node->elseifs); - return $node; - } - - if ($node instanceof TryCatch) { - $node->catches = array_values($node->catches); - return $node; - } - - if ($node instanceof FunctionLike) { - /** @var ClassMethod|Function_|Closure $node */ - $node->params = array_values($node->params); - - if ($node instanceof Closure) { - $node->uses = array_values($node->uses); - } - - return $node; - } - - if ($node instanceof CallLike) { - /** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */ - $node->args = array_values($node->args); - return $node; - } - - if ($node instanceof Switch_) { - $node->cases = array_values($node->cases); - return $node; - } - - return null; - } -} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index a63c3dc274b..93ea363db05 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -103,7 +103,6 @@ use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\GlobalVariableNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\NameNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StaticVariableNodeVisitor; -use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StmtKeyNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver; use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider; use Rector\Php80\AttributeDecorator\DoctrineConverterAttributeDecorator; @@ -243,7 +242,7 @@ final class LazyContainerFactory GlobalVariableNodeVisitor::class, NameNodeVisitor::class, StaticVariableNodeVisitor::class, - StmtKeyNodeVisitor::class, + // StmtKeyNodeVisitor::class, ]; /** diff --git a/src/NodeTypeResolver/Node/AttributeKey.php b/src/NodeTypeResolver/Node/AttributeKey.php index 986bbf2973d..235293c1b07 100644 --- a/src/NodeTypeResolver/Node/AttributeKey.php +++ b/src/NodeTypeResolver/Node/AttributeKey.php @@ -162,6 +162,7 @@ final class AttributeKey public const IS_BYREF_RETURN = 'is_byref_return'; /** + * @deprecated This value can change, as based on default input keys. Use existing array keys instead. * @var string */ public const STMT_KEY = 'stmt_key'; diff --git a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php b/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php deleted file mode 100644 index c3b01124594..00000000000 --- a/src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php +++ /dev/null @@ -1,18 +0,0 @@ -getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node; - NodeAttributeReIndexer::reIndexNodeAttributes($node); - $refactoredNode = $this->refactor($node); // nothing to change → continue