Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use PhpParser\Node;
use PHPStan\File\FileReader;
use function array_slice;
use function clearstatcache;
use function filemtime;

final class CachedParser implements Parser
{
Expand All @@ -17,6 +19,9 @@ final class CachedParser implements Parser
/** @var array<string, true> */
private array $parsedByString = [];

/** @var array<string, array{int, string}> path => [mtime, source code] */
private array $cachedSourceByFile = [];

public function __construct(
private Parser $originalParser,
private int $cachedNodesByStringCountMax,
Expand All @@ -40,7 +45,20 @@ public function parseFile(string $file): array
--$this->cachedNodesByStringCount;
}

$sourceCode = FileReader::read($file);
// parseFile is called once per class using a trait, so the same file is read many times.
// Memoize the contents by path, keyed by mtime; clearstatcache keeps this correct when a file
// changes between calls in a long-running process (PHPStan Pro, fixer worker).
clearstatcache(true, $file);
$mtime = @filemtime($file);
if ($mtime !== false && isset($this->cachedSourceByFile[$file]) && $this->cachedSourceByFile[$file][0] === $mtime) {
$sourceCode = $this->cachedSourceByFile[$file][1];
} else {
$sourceCode = FileReader::read($file);
if ($mtime !== false) {
$this->cachedSourceByFile[$file] = [$mtime, $sourceCode];
}
}

if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) {
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file);
$this->cachedNodesByStringCount++;
Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
use Generator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PHPStan\BetterReflection\Reflection\ExprCacheHelper;
use PHPStan\File\FileHelper;
use PHPStan\File\FileReader;
use PHPStan\Testing\PHPStanTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\Stub;
use function file_put_contents;
use function sys_get_temp_dir;
use function time;
use function touch;
use function uniqid;
use function unlink;

class CachedParserTest extends PHPStanTestCase
{
Expand Down Expand Up @@ -171,4 +178,38 @@ public function testWithExprCacheHelper(): void
$this->assertSame(['startLine' => 1, 'startTokenPos' => 35, 'startFilePos' => 137, 'endLine' => 10, 'endTokenPos' => 35, 'endFilePos' => 143, 'kind' => 1, 'rawValue' => "'hello'"], $reImported->getAttributes());
}

public function testParseFileSkipsReadingUnchangedFileAndRereadsAfterChange(): void
{
$parser = new CachedParser($this->getContentEchoingParserStub(), 500);
$path = sys_get_temp_dir() . '/phpstan-cached-parser-' . uniqid() . '.php';
$baseTime = time() - 10;

try {
file_put_contents($path, 'first contents');
touch($path, $baseTime);
$this->assertSame('first contents', $parser->parseFile($path)[0]->getAttribute('content'));

// Contents change but the mtime does not: the memoized contents are returned without re-reading.
file_put_contents($path, 'second contents');
touch($path, $baseTime);
$this->assertSame('first contents', $parser->parseFile($path)[0]->getAttribute('content'));

// A newer mtime invalidates the memo, so the file is read again.
touch($path, $baseTime + 10);
$this->assertSame('second contents', $parser->parseFile($path)[0]->getAttribute('content'));
} finally {
@unlink($path);
}
}

private function getContentEchoingParserStub(): Parser&Stub
{
$mock = $this->createStub(Parser::class);
$mock->method('parseFile')->willReturnCallback(
static fn (string $file): array => [new Nop(['content' => FileReader::read($file)])],
);

return $mock;
}

}
Loading