From 469b596aa6fb7c9eb0fddb50eb86e32816e2e0e0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 16 Nov 2024 20:51:00 +0700 Subject: [PATCH] use simple parser for defaultAnalysisParser, with native parser php 7.0 --- config/phpstan/parser.neon | 51 +++++++++++++------ .../CurlyToSquareBracketArrayStringRector.php | 5 +- .../PHPStanServicesFactory.php | 4 +- .../PhpParser/SmartPhpParserFactory.php | 18 +++++-- src/PhpParser/Parser/SimplePhpParser.php | 3 +- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/config/phpstan/parser.neon b/config/phpstan/parser.neon index 3b0f385dce2..ee34c603214 100644 --- a/config/phpstan/parser.neon +++ b/config/phpstan/parser.neon @@ -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: @@ -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 diff --git a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php index 848d9638c31..debb9dcd51b 100644 --- a/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php +++ b/rules/Php74/Rector/ArrayDimFetch/CurlyToSquareBracketArrayStringRector.php @@ -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; diff --git a/src/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php b/src/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php index 7f4b450d126..5f9a1f4dfc1 100644 --- a/src/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php +++ b/src/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php @@ -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; @@ -88,7 +89,8 @@ public function createEmulativeLexer(): Lexer */ public function createPHPStanParser(): Parser { - return $this->container->getService('currentPhpVersionRichParser'); + $smartPhpParserFactory = new SmartPhpParserFactory(); + return $smartPhpParserFactory->createSimpleParser(); } /** diff --git a/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php b/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php index b02e3aef6e6..4d5479f33ac 100644 --- a/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php +++ b/src/PhpDocParser/PhpParser/SmartPhpParserFactory.php @@ -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; @@ -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(); @@ -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); + } } diff --git a/src/PhpParser/Parser/SimplePhpParser.php b/src/PhpParser/Parser/SimplePhpParser.php index 8b51c09a77c..4f0803ebab5 100644 --- a/src/PhpParser/Parser/SimplePhpParser.php +++ b/src/PhpParser/Parser/SimplePhpParser.php @@ -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; @@ -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());