Skip to content
Draft
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
57 changes: 11 additions & 46 deletions src/Parser/CachedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Node\Stmt[]>*/
private array $cachedNodesByString = [];
private array $cachedNodesByFile = [];

private int $cachedNodesByStringCount = 0;

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

public function __construct(
private Parser $originalParser,
Expand All @@ -30,66 +26,35 @@ 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];
}

/**
* @return Node\Stmt[]
*/
public function parseString(string $sourceCode): array
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I wonder whether/why we need this parseString function. it seems its only used from tests and everywhere else we read a file to pass the contents into parseString.

I think we could remove some unnecessary file-read operations by dropping parseString and just use parseFile everywhere with PHPStan 3.x ... wdyt?

{
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
{
return $this->cachedNodesByStringCountMax;
}

/**
* @return array<string, Node[]>
*/
public function getCachedNodesByString(): array
{
return $this->cachedNodesByString;
}

}
15 changes: 0 additions & 15 deletions tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}

/**
Expand Down
Loading