Skip to content

Commit

Permalink
[1.0] Add public method BetterNodeFinder::resolvePreviousNode() and B…
Browse files Browse the repository at this point in the history
…etterNodeFinder::resolveNextNode() (#3896)

* [1.0] Add public method BetterNodeFinder::resolvePreviousNode() and BetterNodeFinder::resolveNextNode()

* comment

* Fix conflict

* Use BetterNodeFinder::resolveNextNode() in InlineCodeParser for next Concat

* [ci-review] Rector Rectify

* increaes kernel cache key

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed May 21, 2023
1 parent e1f1980 commit 8ec3a27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v16';
private const CACHE_KEY = 'v17';

private ContainerInterface|null $container = null;

Expand Down
16 changes: 10 additions & 6 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ private function findFirstInlinedNext(Node $node, callable $filter, array $newSt
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
$nextNode = $this->resolveNeighborNextStmt($parentNode, $node, $currentStmtKey);
} else {
$nextNode = $this->resolveNextNodeFromOtherNode($node);
$nextNode = $this->resolveNextNode($node);
}

if (! $nextNode instanceof Node) {
Expand Down Expand Up @@ -714,9 +714,11 @@ private function resolveNextNodeFromFile(array $newStmts, Node $node): ?Node
}

/**
* Resolve previous node from not an Stmt, eg: Expr, Identifier, Name, etc
* @api
*
* Resolve previous node from any Node, eg: Expr, Identifier, Name, etc
*/
private function resolvePreviousNodeFromOtherNode(Node $node): ?Node
public function resolvePreviousNode(Node $node): ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);

Expand Down Expand Up @@ -750,9 +752,11 @@ private function resolvePreviousNodeFromOtherNode(Node $node): ?Node
}

/**
* Resolve next node from not an Stmt, eg: Expr, Identifier, Name, etc
* @api
*
* Resolve next node from any Node, eg: Expr, Identifier, Name, etc
*/
private function resolveNextNodeFromOtherNode(Node $node): ?Node
public function resolveNextNode(Node $node): ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);

Expand Down Expand Up @@ -811,7 +815,7 @@ private function findFirstInlinedPrevious(Node $node, callable $filter, array $n
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
$previousNode = $parentNode->stmts[$currentStmtKey - 1] ?? null;
} else {
$previousNode = $this->resolvePreviousNodeFromOtherNode($node);
$previousNode = $this->resolvePreviousNode($node);
}

if (! $previousNode instanceof Node) {
Expand Down
17 changes: 6 additions & 11 deletions src/PhpParser/Parser/InlineCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Core\PhpParser\Parser;

use PhpParser\Node\Expr\Variable;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use PhpParser\Node\Expr;
Expand All @@ -12,10 +13,9 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;

final class InlineCodeParser
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public function __construct(
private readonly NodePrinterInterface $nodePrinter,
private readonly SimplePhpParser $simplePhpParser,
private readonly ValueResolver $valueResolver,
private readonly CurrentFileProvider $currentFileProvider
private readonly BetterNodeFinder $betterNodeFinder
) {
}

Expand Down Expand Up @@ -147,14 +147,9 @@ private function resolveConcatValue(Concat $concat): string
$concat->right->value = '.' . $concat->right->value;
}

$file = $this->currentFileProvider->getFile();
if ($concat->right instanceof String_ &&
str_starts_with($concat->right->value, '($')
&& $file instanceof File) {
$oldTokens = $file->getOldTokens();
$endTokenPos = $concat->right->getEndTokenPos();

if (isset($oldTokens[$endTokenPos][1]) && str_starts_with((string) $oldTokens[$endTokenPos][1], "'($")) {
if ($concat->right instanceof String_ && str_starts_with($concat->right->value, '($')) {
$node = $this->betterNodeFinder->resolveNextNode($concat);
if ($node instanceof Variable) {
$concat->right->value .= '.';
}
}
Expand Down

0 comments on commit 8ec3a27

Please sign in to comment.