Skip to content

Commit

Permalink
[Php80] Handle crash leaveNode() returned invalid value of type integ…
Browse files Browse the repository at this point in the history
…er on TokenGetAllToObjectRector (#3291)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 17, 2023
1 parent 240fa27 commit 541bf45
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\TokenGetAllToObjectRector\Fixture;

final class TernaryIf
{
public function run()
{
$tokens = token_get_all('<?php ' . implode('', array_slice($fileContent, $start, $end - $start)));

foreach ($tokens as $i => $token) {
$readableToken = is_array($token) ? $token[1] : $token;
}
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\TokenGetAllToObjectRector\Fixture;

final class TernaryIf
{
public function run()
{
$tokens = \PhpToken::tokenize('<?php ' . implode('', array_slice($fileContent, $start, $end - $start)));

foreach ($tokens as $i => $token) {
$readableToken = $token->text;
}
}
}

?>
31 changes: 29 additions & 2 deletions rules/Php80/NodeManipulator/TokenManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\If_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Util\StringUtils;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -39,7 +41,8 @@ public function __construct(
private readonly NodesToRemoveCollector $nodesToRemoveCollector,
private readonly ValueResolver $valueResolver,
private readonly NodeComparator $nodeComparator,
private readonly ArgsAnalyzer $argsAnalyzer
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder
) {
}

Expand Down Expand Up @@ -176,7 +179,7 @@ public function removeIsArray(array $nodes, Variable $singleTokenVariable): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (Node $node) use (
$singleTokenVariable
) {
): ?FuncCall {
if (! $node instanceof FuncCall) {
return null;
}
Expand All @@ -202,10 +205,34 @@ public function removeIsArray(array $nodes, Variable $singleTokenVariable): void
// remove correct node
$nodeToRemove = $this->matchParentNodeInCaseOfIdenticalTrue($node);

$parentNode = $nodeToRemove->getAttribute(AttributeKey::PARENT_NODE);

if ($parentNode instanceof Ternary) {
$this->replaceTernary($parentNode);
return $node;
}

$this->nodesToRemoveCollector->addNodeToRemove($nodeToRemove);
return $node;
});
}

private function replaceTernary(Ternary $ternary): void
{
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($ternary);

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$currentStmt,
static function (Node $subNode) use ($ternary): ?Expr {
if ($subNode === $ternary) {
return $ternary->if;
}

return null;
}
);
}

/**
* Replace $token[0] with $token->getTokenName() call
*
Expand Down

0 comments on commit 541bf45

Please sign in to comment.