Skip to content

Commit

Permalink
Fix CachedParser returning stale cache when asked for richer AST thro…
Browse files Browse the repository at this point in the history
…ugh parseFile
  • Loading branch information
ondrejmirtes committed Jan 29, 2021
1 parent 0f39444 commit bef5a26
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class CachedParser implements Parser

private int $cachedNodesByStringCountMax;

/** @var array<string, true> */
private array $parsedByString = [];

public function __construct(
Parser $originalParser,
int $cachedNodesByStringCountMax
Expand Down Expand Up @@ -43,9 +46,10 @@ public function parseFile(string $file): array
}

$sourceCode = FileReader::read($file);
if (!isset($this->cachedNodesByString[$sourceCode])) {
if (!isset($this->cachedNodesByString[$sourceCode]) || isset($this->parsedByString[$sourceCode])) {
$this->cachedNodesByString[$sourceCode] = $this->originalParser->parseFile($file);
$this->cachedNodesByStringCount++;
unset($this->parsedByString[$sourceCode]);
}

return $this->cachedNodesByString[$sourceCode];
Expand All @@ -71,6 +75,7 @@ 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];
Expand Down
24 changes: 23 additions & 1 deletion tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace PHPStan\Parser;

class CachedParserTest extends \PHPUnit\Framework\TestCase
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\File\FileReader;
use PHPStan\Testing\TestCase;

class CachedParserTest extends TestCase
{

/**
Expand Down Expand Up @@ -75,4 +79,22 @@ private function getPhpParserNodeMock(): \PhpParser\Node
return $this->createMock(\PhpParser\Node::class);
}

public function testParseTheSameFileWithDifferentMethod(): void
{
$parser = new CachedParser(self::getContainer()->getService('pathRoutingParser'), 500);
$path = __DIR__ . '/data/test.php';
$contents = FileReader::read($path);
$stmts = $parser->parseString($contents);
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
$this->assertNull($stmts[0]->stmts[0]->getAttribute('parent'));

$stmts = $parser->parseFile($path);
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
$this->assertInstanceOf(Namespace_::class, $stmts[0]->stmts[0]->getAttribute('parent'));

$stmts = $parser->parseString($contents);
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
$this->assertInstanceOf(Namespace_::class, $stmts[0]->stmts[0]->getAttribute('parent'));
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Parser/data/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CachedParserBug;

class Foo
{

}

0 comments on commit bef5a26

Please sign in to comment.