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 972d933
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 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 @@ -54,6 +54,8 @@
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\UnionType;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeContext;
Expand All @@ -63,6 +65,8 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use Rector\Config\RectorConfig;
use Rector\DependencyInjection\LazyContainerFactory;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -329,7 +333,11 @@ private function nodeScopeResolverProcessNodes(
): void {
try {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
} catch (\PHPStan\Parser\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
10 changes: 9 additions & 1 deletion 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,8 +37,14 @@ 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
{
if (! $forNewestSupportedVersion) {
$parserFactory = new ParserFactory();
$parser = $parserFactory->createForVersion(PhpVersion::fromString('7.0'));
$this->privatesAccessor->setPrivateProperty($this->parser, 'parser', $parser);
}

$stmts = $this->parser->parseString($fileContent);

$innerParser = $this->privatesAccessor->getPrivateProperty($this->parser, 'parser');
Expand Down

0 comments on commit 972d933

Please sign in to comment.