Skip to content
Merged
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
48 changes: 48 additions & 0 deletions tests/PHPStan/Parser/CachedParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Generator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\BetterReflection\Reflection\ExprCacheHelper;
use PHPStan\File\FileHelper;
use PHPStan\File\FileReader;
use PHPStan\Testing\PHPStanTestCase;
Expand Down Expand Up @@ -123,4 +124,51 @@ public function testParseTheSameFileWithDifferentMethod(): void
$this->assertSame(2, $stmts[0]->stmts[1]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
}

public function testWithExprCacheHelper(): void
{
$fileHelper = self::getContainer()->getByType(FileHelper::class);
$pathRoutingParser = new PathRoutingParser(
$fileHelper,
self::getContainer()->getService('currentPhpVersionRichParser'),
self::getContainer()->getService('currentPhpVersionSimpleDirectParser'),
self::getContainer()->getService('php8Parser'),
null,
);
$parser = new CachedParser($pathRoutingParser, 500);
$path = $fileHelper->normalizePath(__DIR__ . '/data/parser-cache-bug.php');
$pathRoutingParser->setAnalysedFiles([$path]);
$contents = FileReader::read($path);
$stmts = $parser->parseString($contents);

$this->assertInstanceOf(Namespace_::class, $stmts[0]);
$ns = $stmts[0];

$this->assertInstanceOf(Node\Stmt\Class_::class, $ns->stmts[1]);
$class = $ns->stmts[1];

$this->assertInstanceOf(Node\Stmt\Property::class, $class->stmts[0]);
$property = $class->stmts[0];
$group = $property->attrGroups[0];
$attribute = $group->attrs[0];

$expr = $attribute->args[0]->value;
$this->assertSame(['startLine' => 8, 'startTokenPos' => 21, 'startFilePos' => 88, 'endLine' => 8, 'endTokenPos' => 21, 'endFilePos' => 94, 'kind' => 1, 'rawValue' => "'hello'"], $expr->getAttributes());
$exported = ExprCacheHelper::export($expr);
$reImported = ExprCacheHelper::import($exported);
$this->assertSame(['startLine' => 8, 'startTokenPos' => 21, 'startFilePos' => 88, 'endLine' => 8, 'endTokenPos' => 21, 'endFilePos' => 94, 'kind' => 1, 'rawValue' => "'hello'"], $reImported->getAttributes());

$this->assertInstanceOf(Node\Stmt\Property::class, $class->stmts[1]);
$property = $class->stmts[1];
$group = $property->attrGroups[0];
$attribute = $group->attrs[0];

$expr = $attribute->args[0]->value;
$this->assertSame(['startLine' => 10, 'startTokenPos' => 35, 'startFilePos' => 137, 'endLine' => 10, 'endTokenPos' => 35, 'endFilePos' => 143, 'kind' => 1, 'rawValue' => "'hello'"], $expr->getAttributes());
$exported = ExprCacheHelper::export($expr);
unset($exported['attributes']['startLine']); // modify attributes
$reImported = ExprCacheHelper::import($exported);
// assert that we get back the default start-line instead of a stale cached startLine of previous same value expression
$this->assertSame(['startLine' => 1, 'startTokenPos' => 35, 'startFilePos' => 137, 'endLine' => 10, 'endTokenPos' => 35, 'endFilePos' => 143, 'kind' => 1, 'rawValue' => "'hello'"], $reImported->getAttributes());
}

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

namespace ParserCacheBug;

use Attribute;

class ParserCacheBug {
#[MyAttribute('hello')]
protected string $foo;
#[MyAttribute('hello')]
protected string $bar;
}

#[Attribute]
class MyAttribute
{
public string $arg;

public function __construct(string $event)
{
$this->arg = $event;
}
}
Loading