2021-01-22 22:05:28 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-01-22 22:05:28 +01:00
|
|
|
namespace Psalm\Tests\LanguageServer;
|
|
|
|
|
2022-12-20 22:09:34 +01:00
|
|
|
use Psalm\Codebase;
|
2021-01-22 22:05:28 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
2021-01-22 22:05:28 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeFileReferenceCacheProvider;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider;
|
|
|
|
use Psalm\Tests\Internal\Provider\ProjectCacheProvider;
|
|
|
|
use Psalm\Tests\TestCase;
|
2021-01-22 22:05:28 +01:00
|
|
|
use Psalm\Tests\TestConfig;
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
use function count;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class FileMapTest extends TestCase
|
2021-01-22 22:05:28 +01:00
|
|
|
{
|
2022-12-20 22:09:34 +01:00
|
|
|
protected Codebase $codebase;
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function setUp(): void
|
2021-01-22 22:05:28 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2021-01-22 22:05:28 +01:00
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
2021-12-03 20:11:20 +01:00
|
|
|
new ParserInstanceCacheProvider(),
|
2021-01-22 22:05:28 +01:00
|
|
|
null,
|
|
|
|
null,
|
2021-12-04 21:55:53 +01:00
|
|
|
new FakeFileReferenceCacheProvider(),
|
2022-12-18 17:15:15 +01:00
|
|
|
new ProjectCacheProvider(),
|
2021-01-22 22:05:28 +01:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase = new Codebase($config, $providers);
|
|
|
|
|
2021-01-22 22:05:28 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
|
|
|
$config,
|
2022-02-15 01:18:49 +01:00
|
|
|
$providers,
|
|
|
|
null,
|
|
|
|
[],
|
|
|
|
1,
|
|
|
|
null,
|
2022-12-20 22:09:34 +01:00
|
|
|
$this->codebase,
|
2021-01-22 22:05:28 +01:00
|
|
|
);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.3', 'tests');
|
2021-01-22 22:05:28 +01:00
|
|
|
$this->project_analyzer->getCodebase()->store_node_types = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMapIsUpdatedOnReloadFiles(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-01-22 22:05:28 +01:00
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function __construct( string $var ) {
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
$a = new A( "foo" );',
|
2021-01-22 22:05:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
|
|
|
|
|
|
|
|
$this->assertTrue(!empty($type_map));
|
|
|
|
|
|
|
|
$codebase->file_provider->setOpenContents('somefile.php', '');
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
|
|
|
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
|
|
|
|
|
|
|
|
$this->assertSame([], $type_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetTypeMap(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-01-22 22:05:28 +01:00
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
function __construct( string $var ) {
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
$a = new A( "foo" );',
|
2021-01-22 22:05:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
[ $type_map ] = $codebase->analyzer->getMapsForFile('somefile.php');
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
[
|
|
|
|
155 => [
|
|
|
|
156,
|
|
|
|
'A',
|
|
|
|
],
|
|
|
|
146 => [
|
|
|
|
148,
|
|
|
|
'146-147:A',
|
|
|
|
],
|
|
|
|
],
|
2022-12-18 17:15:15 +01:00
|
|
|
$type_map,
|
2021-01-22 22:05:28 +01:00
|
|
|
);
|
|
|
|
}
|
2021-02-28 07:36:06 +01:00
|
|
|
|
|
|
|
public function testMapIsUpdatedAfterEditingMethod(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-02-28 07:36:06 +01:00
|
|
|
$codebase->diff_methods = true;
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 07:36:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
[$before] = $codebase->analyzer->getMapsForFile('somefile.php');
|
|
|
|
|
|
|
|
$codebase->addTemporaryFileChanges(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 07:36:06 +01:00
|
|
|
);
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
[$after] = $codebase->analyzer->getMapsForFile('somefile.php');
|
|
|
|
|
2021-12-03 21:07:25 +01:00
|
|
|
$this->assertCount(count($before), $after);
|
2021-02-28 07:36:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 16:36:18 +01:00
|
|
|
public function testMapIsUpdatedAfterDeletingFirstMethod(): void
|
2021-02-28 07:36:06 +01:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-02-28 07:36:06 +01:00
|
|
|
$codebase->diff_methods = true;
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:36:18 +01:00
|
|
|
public function second_method(\DateTimeImmutable $d) : void {
|
|
|
|
new \DateTimeImmutable("2010-01-01");
|
2021-02-28 07:36:06 +01:00
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 07:36:06 +01:00
|
|
|
);
|
|
|
|
|
2021-02-28 16:36:18 +01:00
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(9, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
|
|
|
|
$codebase->addTemporaryFileChanges(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function second_method(\DateTimeImmutable $d) : void {
|
|
|
|
new \DateTimeImmutable("2010-01-01");
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 16:36:18 +01:00
|
|
|
);
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(5, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMapIsUpdatedAfterDeletingSecondMethod(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-02-28 16:36:18 +01:00
|
|
|
$codebase->diff_methods = true;
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 16:36:18 +01:00
|
|
|
);
|
|
|
|
|
2021-02-28 07:36:06 +01:00
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(8, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
|
|
|
|
$codebase->addTemporaryFileChanges(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 07:36:06 +01:00
|
|
|
);
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(4, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
}
|
2021-02-28 16:36:18 +01:00
|
|
|
|
|
|
|
public function testMapIsUpdatedAfterAddingMethod(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$codebase = $this->codebase;
|
2021-02-28 16:36:18 +01:00
|
|
|
$codebase->diff_methods = true;
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 16:36:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->addFilesToAnalyze(['somefile.php' => 'somefile.php']);
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(8, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
|
|
|
|
$codebase->addTemporaryFileChanges(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function first(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function third(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function second(\DateTimeImmutable $d) : void {
|
|
|
|
echo $d->format("Y");
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-02-28 16:36:18 +01:00
|
|
|
);
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
|
|
|
$this->assertCount(12, $codebase->analyzer->getMapsForFile('somefile.php')[0]);
|
|
|
|
}
|
2021-01-22 22:05:28 +01:00
|
|
|
}
|