Skip to content

Commit

Permalink
Fix parsing php 7
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 17, 2024
1 parent 352dbf1 commit 3bb03c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ private function isFollowedByCurlyBracket(File $file, ArrayDimFetch $arrayDimFet
$oldTokens = $file->getOldTokens();
$endTokenPost = $arrayDimFetch->getEndTokenPos();

if (isset($oldTokens[$endTokenPost]) && $oldTokens[$endTokenPost] === '}') {
$startTokenPost = $arrayDimFetch->getStartTokenPos();
return ! (isset($oldTokens[$startTokenPost][1]) && $oldTokens[$startTokenPost][1] === '${');
if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === '}') {
$startTokenPos = $arrayDimFetch->getStartTokenPos();
return ! (isset($oldTokens[$startTokenPos]) && (string) $oldTokens[$startTokenPos] === '${');
}

return false;
Expand Down
9 changes: 7 additions & 2 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private function parseFileAndDecorateNodes(File $file): ?SystemError
{
try {
$this->parseFileNodes($file);
} catch (ParserErrorsException) {
$this->parseFileNodes($file, false);
} catch (ShouldNotHappenException $shouldNotHappenException) {
throw $shouldNotHappenException;
} catch (AnalysedCodeException $analysedCodeException) {
Expand Down Expand Up @@ -187,10 +189,13 @@ private function printFile(File $file, Configuration $configuration, string $fil
FileSystem::write($filePath, $newContent, null);
}

private function parseFileNodes(File $file): void
private function parseFileNodes(File $file, bool $forNewestSupportedVersion = true): void
{
// store tokens by original file content, so we don't have to print them right now
$stmtsAndTokens = $this->rectorParser->parseFileContentToStmtsAndTokens($file->getOriginalFileContent());
$stmtsAndTokens = $this->rectorParser->parseFileContentToStmtsAndTokens(
$file->getOriginalFileContent(),
$forNewestSupportedVersion
);

$oldStmts = $stmtsAndTokens->getStmts();
$oldTokens = $stmtsAndTokens->getTokens();
Expand Down
3 changes: 2 additions & 1 deletion src/NodeAnalyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand All @@ -16,7 +17,7 @@ final class ScopeAnalyzer
/**
* @var array<class-string<Node>>
*/
private const NON_REFRESHABLE_NODES = [Name::class, Identifier::class, Param::class, Arg::class, Variable::class];
private const NON_REFRESHABLE_NODES = [Name::class, Identifier::class, Param::class, Arg::class, Variable::class, ArrayDimFetch::class];

public function isRefreshable(Node $node): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\NodeTypeResolver\PHPStan\Scope;

use PHPStan\Parser\ParserErrorsException;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand Down Expand Up @@ -329,7 +330,11 @@ private function nodeScopeResolverProcessNodes(
): void {
try {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
} catch (ParserErrorsException) {
// nothing we can do as error parsing from deep internal PHPStan service with service injection we cannot reset
// in the middle of process
} catch (ShouldNotHappenException) {
// internal PHPStan error
}
}

Expand Down
25 changes: 22 additions & 3 deletions src/PhpParser/Parser/RectorParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\PhpParser\Parser;

use PhpParser\Node\Stmt;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PHPStan\Parser\Parser;
use Rector\PhpParser\ValueObject\StmtsAndTokens;
use Rector\Util\Reflection\PrivatesAccessor;
Expand Down Expand Up @@ -35,11 +37,28 @@ public function parseString(string $fileContent): array
return $this->parser->parseString($fileContent);
}

public function parseFileContentToStmtsAndTokens(string $fileContent): StmtsAndTokens
public function parseFileContentToStmtsAndTokens(string $fileContent, bool $forNewestSupportedVersion = true): StmtsAndTokens
{
$stmts = $this->parser->parseString($fileContent);
if (! $forNewestSupportedVersion) {
// don't directly change PHPStan Parser service
// to avoid reuse on next file
$phpstanParser = clone $this->parser;

$innerParser = $this->privatesAccessor->getPrivateProperty($this->parser, 'parser');
$parserFactory = new ParserFactory();
$parser = $parserFactory->createForVersion(PhpVersion::fromString('7.0'));
$this->privatesAccessor->setPrivateProperty($phpstanParser, 'parser', $parser);

return $this->resolveStmtsAndTokens($phpstanParser, $fileContent);
}

return $this->resolveStmtsAndTokens($this->parser, $fileContent);
}

private function resolveStmtsAndTokens(Parser $parser, string $fileContent): StmtsAndTokens
{
$stmts = $parser->parseString($fileContent);

$innerParser = $this->privatesAccessor->getPrivateProperty($parser, 'parser');
$tokens = $innerParser->getTokens();

return new StmtsAndTokens($stmts, $tokens);
Expand Down

0 comments on commit 3bb03c6

Please sign in to comment.