1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/tests/LanguageServer/SymbolLookupTest.php

618 lines
20 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\LanguageServer;
use LanguageServerProtocol\Position;
2019-03-23 19:27:54 +01:00
use Psalm\Context;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
2018-11-06 03:57:36 +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;
2019-03-23 19:27:54 +01:00
use Psalm\Tests\TestConfig;
2021-12-03 20:11:20 +01:00
class SymbolLookupTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
$this->file_provider = new FakeFileProvider();
$config = new TestConfig();
$providers = new Providers(
$this->file_provider,
2021-12-03 20:11:20 +01:00
new ParserInstanceCacheProvider(),
null,
null,
2021-12-04 21:55:53 +01:00
new FakeFileReferenceCacheProvider(),
2021-12-03 20:11:20 +01:00
new ProjectCacheProvider()
);
2018-11-11 18:01:14 +01:00
$this->project_analyzer = new ProjectAnalyzer(
$config,
$providers
);
$this->project_analyzer->setPhpVersion('7.3', 'tests');
2019-02-24 07:33:25 +01:00
$this->project_analyzer->getCodebase()->store_node_types = true;
}
public function testSimpleSymbolLookup(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->globals['$my_global'] = 'string';
$this->addFile(
'somefile.php',
'<?php
namespace B;
const APPLE = "🍏";
class A {
/** @var int|null */
protected $a;
const BANANA = "🍌";
public function foo() : void {
$a = 1;
echo $a;
}
}
function bar() : int {
return 5;
}
function baz(int $a) : int {
return $a;
}
function qux(int $a, int $b) : int {
return $a + $b;
}
2022-09-10 13:06:17 +02:00
$_SESSION;'
);
2018-11-11 18:01:14 +01:00
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$this->analyzeFile('somefile.php', new Context());
$information = $codebase->getSymbolInformation('somefile.php', 'B\A::foo()');
$this->assertNotNull($information);
$this->assertSame('<?php public function foo() : void', $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'B\A::$a');
$this->assertNotNull($information);
$this->assertSame('<?php protected int|null $a', $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'B\bar()');
$this->assertNotNull($information);
$this->assertSame('<?php function B\bar() : int', $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'B\A::BANANA');
$this->assertNotNull($information);
$this->assertSame('<?php BANANA', $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'B\baz()');
$this->assertNotNull($information);
$this->assertSame("<?php function B\baz(\n int \$a\n) : int", $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'B\qux()');
$this->assertNotNull($information);
$this->assertSame("<?php function B\qux(\n int \$a,\n int \$b\n) : int", $information['type']);
2022-09-10 13:06:17 +02:00
$information = $codebase->getSymbolInformation('somefile.php', '$_SESSION');
$this->assertNotNull($information);
2022-09-10 13:06:17 +02:00
$this->assertSame("<?php array<string, mixed>", $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', '$my_global');
$this->assertNotNull($information);
$this->assertSame("<?php string", $information['type']);
}
public function testSimpleSymbolLookupGlobalConst(): void
{
$this->addFile(
'somefile.php',
'<?php
define("BANANA", "🍌");
const APPLE = "🍏";'
);
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
$codebase = $this->project_analyzer->getCodebase();
$this->analyzeFile('somefile.php', new Context());
$information = $codebase->getSymbolInformation('somefile.php', 'APPLE');
$this->assertNotNull($information);
$this->assertSame("<?php const APPLE string", $information['type']);
$information = $codebase->getSymbolInformation('somefile.php', 'BANANA');
$this->assertNotNull($information);
$this->assertSame("<?php const BANANA string", $information['type']);
}
public function testSimpleSymbolLocation(): void
{
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
2019-07-02 00:48:33 +02:00
const BANANA = "nana";
2019-07-02 00:48:33 +02:00
public function foo() : void {
$a = 1;
echo $a;
}
}
function bar() : int {
return 5;
}'
);
2018-11-11 18:01:14 +01:00
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$this->analyzeFile('somefile.php', new Context());
$method_symbol_location = $codebase->getSymbolLocation('somefile.php', 'B\A::foo()');
$this->assertNotNull($method_symbol_location);
$this->assertSame(10, $method_symbol_location->getLineNumber());
$this->assertSame(37, $method_symbol_location->getColumn());
$property_symbol_location = $codebase->getSymbolLocation('somefile.php', 'B\A::$a');
$this->assertNotNull($property_symbol_location);
$this->assertSame(6, $property_symbol_location->getLineNumber());
$this->assertSame(31, $property_symbol_location->getColumn());
$constant_symbol_location = $codebase->getSymbolLocation('somefile.php', 'B\A::BANANA');
$this->assertNotNull($constant_symbol_location);
$this->assertSame(8, $constant_symbol_location->getLineNumber());
$this->assertSame(27, $constant_symbol_location->getColumn());
$function_symbol_location = $codebase->getSymbolLocation('somefile.php', 'B\bar()');
$this->assertNotNull($function_symbol_location);
2019-07-02 00:48:33 +02:00
$this->assertSame(16, $function_symbol_location->getLineNumber());
$this->assertSame(26, $function_symbol_location->getColumn());
2019-07-02 00:48:33 +02:00
$function_symbol_location = $codebase->getSymbolLocation('somefile.php', '257-259');
$this->assertNotNull($function_symbol_location);
$this->assertSame(11, $function_symbol_location->getLineNumber());
$this->assertSame(25, $function_symbol_location->getColumn());
}
public function testSymbolLookupAfterAlteration(): void
{
2018-11-11 18:01:14 +01:00
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() : voi {
$a = 1;
$b = $this->a;
$c = $b;
echo $a;
}
public function bar() : void {
$a = 2;
echo $a;
}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$codebase->addTemporaryFileChanges(
'somefile.php',
'<?php
namespace B;
class A {
/** @var int|null */
protected $a;
public function foo() : void {
$a = 1;
$b = $this->a;
$c = $b;
echo $a;
}
public function bar() : void {
$a = 2;
echo $a;
}
}'
);
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
2018-11-11 18:01:14 +01:00
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(10, 30));
$this->assertNotNull($symbol_at_position);
2019-07-02 00:48:33 +02:00
$this->assertSame('245-246:int|null', $symbol_at_position[0]);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(12, 30));
$this->assertNotNull($symbol_at_position);
$this->assertSame('213-214:1', $symbol_at_position[0]);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(17, 30));
$this->assertNotNull($symbol_at_position);
$this->assertSame('425-426:2', $symbol_at_position[0]);
}
public function testGetSymbolPositionMissingArg(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo(int $i) : string {
return "hello";
}
public function bar() : void {
$this->foo();
}
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(9, 33));
$this->assertNotNull($symbol_at_position);
$this->assertSame('B\A::foo()', $symbol_at_position[0]);
}
public function testGetSymbolPositionGlobalVariable(): void
{
$codebase = $this->project_analyzer->getCodebase();
$codebase->reportUnusedVariables();
$config = $codebase->config;
$config->throw_exception = false;
$config->globals['$my_global'] = 'string';
$this->addFile(
'somefile.php',
'<?php
function foo() : void {
global $my_global;
echo $my_global;
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(2, 31));
$this->assertNotNull($symbol_at_position);
$this->assertSame('$my_global', $symbol_at_position[0]);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(3, 28));
$this->assertNotNull($symbol_at_position);
$this->assertSame('73-82:string', $symbol_at_position[0]);
}
public function testGetSymbolPositionNullableArg(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class AClass {
}
function B( ?AClass $class ) {
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(4, 33));
$this->assertNotNull($symbol_at_position);
$this->assertSame('B\AClass', $symbol_at_position[0]);
}
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
public function testGetSymbolPositionMethodWrongReturnType(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class AClass {
/**
* @return Some
*/
protected function get_command() : AClass {
}
}
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(6, 60));
$this->assertNotNull($symbol_at_position);
$this->assertSame('B\AClass', $symbol_at_position[0]);
}
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
public function testGetSymbolPositionUseStatement(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
use StreamWrapper;
'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(2, 25));
$this->assertNotNull($symbol_at_position);
$this->assertSame('StreamWrapper', $symbol_at_position[0]);
}
public function testGetSymbolPositionRange(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
function foo() : string {
}
$active_symbol = foo();'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
// This is focusing the $active_symbol variable, the LSP Range that is
// returned should also point to the same variable (that's where hover popovers will show)
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(6, 26));
$this->assertNotNull($symbol_at_position);
$this->assertSame(16, $symbol_at_position[1]->start->character);
$this->assertSame(30, $symbol_at_position[1]->end->character);
}
public function testGetTypeInDocblock(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
/** @var \Exception|null */
public $prop;
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(4, 35));
$this->assertNotNull($symbol_at_position);
2019-06-23 15:10:43 +02:00
$this->assertSame('Exception', $symbol_at_position[0]);
}
/**
* @return array<int, array{0: Position, 1: ?string, 2: ?int, 3: ?int}>
*/
public function providerGetSignatureHelp(): array
{
return [
2019-07-19 05:08:54 +02:00
[new Position(5, 34), null, null, null],
[new Position(5, 35), 'B\A::foo', 0, 2],
[new Position(5, 36), null, null, null],
[new Position(6, 34), null, null, null],
[new Position(6, 35), 'B\A::foo', 0, 2],
[new Position(6, 40), 'B\A::foo', 0, 2],
[new Position(6, 41), 'B\A::foo', 1, 2],
[new Position(6, 47), 'B\A::foo', 1, 2],
[new Position(6, 48), null, null, null],
[new Position(7, 40), 'B\A::foo', 0, 2],
[new Position(7, 41), 'B\A::foo', 1, 2],
[new Position(7, 42), 'B\A::foo', 1, 2],
[new Position(8, 40), 'B\A::foo', 0, 2],
[new Position(8, 46), 'B\A::bar', 0, 1],
[new Position(8, 47), 'B\A::foo', 0, 2],
[new Position(10, 40), 'B\A::staticfoo', 0, 1],
// [new Position(12, 28), 'B\foo', 0, 1],
2019-07-19 05:08:54 +02:00
[new Position(14, 30), 'B\A::__construct', 0, 0],
2019-07-19 05:30:44 +02:00
[new Position(16, 31), 'strlen', 0, 1],
];
}
/**
* @dataProvider providerGetSignatureHelp
*/
public function testGetSignatureHelp(
Position $position,
?string $expected_symbol,
2019-07-19 05:08:54 +02:00
?int $expected_argument_number,
?int $expected_param_count
): void {
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
class A {
public function foo(string $a, array $b) {
$this->foo();
$this->foo("Foo", "Bar");
$this->foo("Foo", );
$this->foo($this->bar());
self::staticFoo();
foo();
new A();
strlen();
}
public function bar(string $a) {}
public static function staticFoo(string $a) {}
2019-07-02 00:48:33 +02:00
public function __construct() {}
}
function foo(string $a) {
}'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$reference_location = $codebase->getFunctionArgumentAtPosition('somefile.php', $position);
2019-07-19 05:08:54 +02:00
if ($expected_symbol !== null) {
$this->assertNotNull($reference_location);
[$symbol, $argument_number] = $reference_location;
2019-07-19 05:08:54 +02:00
$this->assertSame($expected_symbol, $symbol);
$this->assertSame($expected_argument_number, $argument_number);
2019-07-19 05:30:44 +02:00
$symbol_information = $codebase->getSignatureInformation($reference_location[0]);
2019-07-19 05:08:54 +02:00
if ($expected_param_count === null) {
$this->assertNull($symbol_information);
} else {
$this->assertNotNull($symbol_information);
$this->assertNotNull($symbol_information->parameters);
$this->assertCount($expected_param_count, $symbol_information->parameters);
}
} else {
$this->assertNull($reference_location);
}
}
public function testGetSignatureHelpIncludesParamDescription(): void
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;
$this->addFile(
'somefile.php',
'<?php
namespace B;
/**
* @param string $a The first param, a.
*/
function foo(string $a) {
}
foo();'
);
$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());
$reference_location = $codebase->getFunctionArgumentAtPosition('somefile.php', new Position(7, 20));
$this->assertNotNull($reference_location);
$symbol_information = $codebase->getSignatureInformation($reference_location[0], 'somefile.php');
$this->assertNotNull($symbol_information);
$this->assertNotNull($symbol_information->parameters);
$this->assertEquals('The first param, a.', $symbol_information->parameters[0]->documentation);
}
}