Skip to content

Commit

Permalink
use simple parser for defaultAnalysisParser, with native parser php 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 16, 2024
1 parent 4b510bf commit 469b596
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
51 changes: 36 additions & 15 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# see original config.neon in phpstan.neon - https://github.com/phpstan/phpstan-src/blob/386eb913abb6ac05886c5642fd48b5d99db66a20/conf/config.neon#L1582
# this file overrides definitions from the config above
services:
defaultAnalysisParser:
factory: @pathRoutingParser
arguments!: []

cachedRectorParser:
class: PHPStan\Parser\CachedParser
arguments:
originalParser: @rectorParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
autowired: false

pathRoutingParser:
class: PHPStan\Parser\PathRoutingParser
arguments:
Expand All @@ -20,8 +9,40 @@ services:
php8Parser: @php8Parser
autowired: false

rectorParser:
class: PHPStan\Parser\RichParser
arguments:
parser: @currentPhpVersionPhpParser
- Rector\PhpDocParser\PhpParser\SmartPhpParserFactory

nativeParser:
class: PhpParser\Parser
factory: ['@Rector\PhpDocParser\PhpParser\SmartPhpParserFactory', 'createNativePhpParser']
autowired: no

phpstanParser:
class: PhpParser\Parser
factory: ['@Rector\PhpDocParser\PhpParser\SmartPhpParserFactory', 'createPHPStanParser']
autowired: no

-
class: PHPStan\Analyser\NodeScopeResolver
arguments:
parser: @nativeParser
reflector: @nodeScopeResolverReflector
polluteScopeWithLoopInitialAssignments: %polluteScopeWithLoopInitialAssignments%
polluteScopeWithAlwaysIterableForeach: %polluteScopeWithAlwaysIterableForeach%
polluteScopeWithBlock: %polluteScopeWithBlock%
earlyTerminatingMethodCalls: %earlyTerminatingMethodCalls%
earlyTerminatingFunctionCalls: %earlyTerminatingFunctionCalls%
implicitThrows: %exceptions.implicitThrows%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
universalObjectCratesClasses: %universalObjectCratesClasses%
autowired: false

defaultAnalysisParser:
factory: ['@Rector\PhpDocParser\PhpParser\SmartPhpParserFactory', 'createPHPStanParser']
arguments!: []

cachedRectorParser:
class: PHPStan\Parser\CachedParser
arguments:
originalParser: @phpstanParser
cachedNodesByStringCountMax: %cache.nodesByStringCountMax%
autowired: false
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ 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] === '}') {
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\PhpDocParser\PhpParser\SmartPhpParserFactory;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -88,7 +89,8 @@ public function createEmulativeLexer(): Lexer
*/
public function createPHPStanParser(): Parser
{
return $this->container->getService('currentPhpVersionRichParser');
$smartPhpParserFactory = new SmartPhpParserFactory();
return $smartPhpParserFactory->createSimpleParser();
}

/**
Expand Down
18 changes: 15 additions & 3 deletions src/PhpDocParser/PhpParser/SmartPhpParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PHPStan\Parser\CachedParser;
use PHPStan\Parser\SimpleParser;
use PHPStan\Parser\VariadicFunctionsVisitor;
Expand All @@ -31,14 +32,15 @@ public function create(): SmartPhpParser
return new SmartPhpParser($cachedParser);
}

private function createNativePhpParser(): Parser
public function createNativePhpParser(): Parser
{
$parserFactory = new ParserFactory();
return $parserFactory->createForNewestSupportedVersion();
return $parserFactory->createForVersion(PhpVersion::fromString('7.0'));
}

private function createPHPStanParser(Parser $parser): CachedParser
public function createPHPStanParser(): CachedParser
{
$parser = $this->createNativePhpParser();
$nameResolver = new NameResolver();
$variadicMethodsVisitor = new VariadicMethodsVisitor();
$variadicFunctionsVisitor = new VariadicFunctionsVisitor();
Expand All @@ -47,4 +49,14 @@ private function createPHPStanParser(Parser $parser): CachedParser

return new CachedParser($simpleParser, 1024);
}

public function createSimpleParser(): SimpleParser
{
$parser = $this->createNativePhpParser();
$nameResolver = new NameResolver();
$variadicMethodsVisitor = new VariadicMethodsVisitor();
$variadicFunctionsVisitor = new VariadicFunctionsVisitor();

return new SimpleParser($parser, $nameResolver, $variadicMethodsVisitor, $variadicFunctionsVisitor);
}
}
3 changes: 2 additions & 1 deletion src/PhpParser/Parser/SimplePhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\AssignedToNodeVisitor;
use Throwable;

Expand All @@ -22,7 +23,7 @@
public function __construct()
{
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->createForNewestSupportedVersion();
$this->phpParser = $parserFactory->createForVersion(PhpVersion::fromString('7.0'));

$this->nodeTraverser = new NodeTraverser();
$this->nodeTraverser->addVisitor(new AssignedToNodeVisitor());
Expand Down

0 comments on commit 469b596

Please sign in to comment.