diff --git a/src/Parser/CachedParser.php b/src/Parser/CachedParser.php index 400c21bf5a..98a8dbb854 100644 --- a/src/Parser/CachedParser.php +++ b/src/Parser/CachedParser.php @@ -14,8 +14,10 @@ final class CachedParser implements Parser private int $cachedNodesByStringCount = 0; - /** @var array */ - private array $parsedByString = []; + /** @var array*/ + private array $cachedNodesByFile = []; + + private int $cachedNodesByFileCount = 0; public function __construct( private Parser $originalParser, @@ -30,24 +32,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]; } /** @@ -68,7 +68,6 @@ public function parseString(string $sourceCode): array if (!isset($this->cachedNodesByString[$sourceCode])) { $this->cachedNodesByString[$sourceCode] = $this->originalParser->parseString($sourceCode); $this->cachedNodesByStringCount++; - $this->parsedByString[$sourceCode] = true; } return $this->cachedNodesByString[$sourceCode];