Skip to content

Commit

Permalink
Refactor from PARENT_NODE in TokenGetAllToObjectRector (#3961)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 25, 2023
1 parent 18ac565 commit 31712a1
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions rules/Php80/Rector/FuncCall/TokenGetAllToObjectRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,44 @@ public function run()
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
return [Assign::class];
}

/**
* @param FuncCall $node
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isName($node, 'token_get_all')) {
if (! $node->expr instanceof FuncCall) {
return null;
}

$this->refactorTokensVariable($node);
$funcCall = $node->expr;
if (! $this->nodeNameResolver->isName($funcCall, 'token_get_all')) {
return null;
}

$this->refactorTokensVariable($funcCall, $node->var);

$node->expr = $this->nodeFactory->createStaticCall('PhpToken', 'tokenize', $funcCall->getArgs());

return $this->nodeFactory->createStaticCall('PhpToken', 'tokenize', $node->args);
return $node;
}

private function refactorTokensVariable(FuncCall $funcCall): void
private function refactorTokensVariable(FuncCall $funcCall, Expr $assignedToExpr): void
{
$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Assign) {
return;
}

/** @var ClassMethod|Function_|null $classMethodOrFunction */
$classMethodOrFunction = $this->betterNodeFinder->findParentByTypes(
$funcCall,
[ClassMethod::class, Function_::class]
);

if ($classMethodOrFunction === null) {
return;
}

// dummy approach, improve when needed
$this->replaceGetNameOrGetValue($classMethodOrFunction, $parentNode->var);
$this->replaceGetNameOrGetValue($classMethodOrFunction, $assignedToExpr);
}

private function replaceGetNameOrGetValue(ClassMethod | Function_ $functionLike, Expr $assignedExpr): void
Expand Down

0 comments on commit 31712a1

Please sign in to comment.