diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 400c21bf5a..34957ae430 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -3,19 +3,15 @@ namespace PHPStan\Parser; use PhpParser\Node; -use PHPStan\File\FileReader; use function array_slice; final class CachedParser implements Parser { /** @var array*/ - private array $cachedNodesByString = []; + private array $cachedNodesByFile = []; - private int $cachedNodesByStringCount = 0; - - /** @var array */ - private array $parsedByString = []; + private int $cachedNodesByFileCount = 0; public function __construct( private Parser $originalParser, @@ -30,24 +26,22 @@ public function __construct( */ public function parseFile(string $file): array { - if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) { - $this->cachedNodesByString = array_slice( - $this->cachedNodesByString, + if ($this->cachedNodesByFileCount !== 0 && $this->cachedNodesByFileCount >= $this->cachedNodesByStringCountMax) { + $this->cachedNodesByFile = array_slice( + $this->cachedNodesByFile, 1, preserve_keys: true, ); - --$this->cachedNodesByStringCount; + --$this->cachedNodesByFileCount; } - $sourceCode = FileReader::read($file); - if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) { - $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file); - $this->cachedNodesByStringCount++; - unset($this->parsedByString[$sourceCode]); + if (!isset($this->cachedNodesByFile[$file])) { + $this->cachedNodesByFile[$file] = $this->originalParser->parseFile($file); + $this->cachedNodesByFileCount++; } - return $this->cachedNodesByString[$sourceCode]; + return $this->cachedNodesByFile[$file]; } /** @@ -55,28 +49,7 @@ public function parseFile(string $file): array */ public function parseString(string $sourceCode): array { - if ($this->cachedNodesByStringCountMax !== 0 && $this->cachedNodesByStringCount >= $this->cachedNodesByStringCountMax) { - $this->cachedNodesByString = array_slice( - $this->cachedNodesByString, - 1, - preserve_keys: true, - ); - - --$this->cachedNodesByStringCount; - } - - if (!isset($this->cachedNodesByString[$sourceCode])) { - $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode); - $this->cachedNodesByStringCount++; - $this->parsedByString[$sourceCode] = true; - } - - return $this->cachedNodesByString[$sourceCode]; - } - - public function getCachedNodesByStringCount(): int - { - return $this->cachedNodesByStringCount; + return $this->originalParser->parseString($sourceCode); } public function getCachedNodesByStringCountMax(): int @@ -84,12 +57,4 @@ public function getCachedNodesByStringCountMax(): int return $this->cachedNodesByStringCountMax; } - /** - * @return array - */ - public function getCachedNodesByString(): array - { - return $this->cachedNodesByString; - } - } diff --git a/tests/PHPStan/Parser/CachedParserTest.php b/tests/PHPStan/Parser/CachedParserTest.php index 29ca21ee3a..9bcbc98c0c 100644 --- a/tests/PHPStan/Parser/CachedParserTest.php +++ b/tests/PHPStan/Parser/CachedParserTest.php @@ -29,21 +29,6 @@ public function testParseFileClearCache( $cachedNodesByStringCountMax, $parser->getCachedNodesByStringCountMax(), ); - - // Add strings to cache - for ($i = 0; $i <= $cachedNodesByStringCountMax; $i++) { - $parser->parseString('string' . $i); - } - - $this->assertSame( - $cachedNodesByStringCountExpected, - $parser->getCachedNodesByStringCount(), - ); - - $this->assertCount( - $cachedNodesByStringCountExpected, - $parser->getCachedNodesByString(), - ); } /**