2018-10-26 22:17:15 +02:00
|
|
|
<?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\Providers;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
|
|
|
use Psalm\Tests\TestConfig;
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
class SymbolLookupTest extends \Psalm\Tests\TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
FileAnalyzer::clearCache();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2018-11-12 16:57:05 +01:00
|
|
|
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
2018-11-12 16:57:05 +01:00
|
|
|
new \Psalm\Tests\Internal\Provider\ParserInstanceCacheProvider(),
|
2018-10-26 22:17:15 +02:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new Provider\FakeFileReferenceCacheProvider()
|
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-10-26 22:17:15 +02:00
|
|
|
$config,
|
|
|
|
$providers,
|
|
|
|
false,
|
|
|
|
true,
|
2018-11-06 03:57:36 +01:00
|
|
|
ProjectAnalyzer::TYPE_CONSOLE,
|
2018-10-26 22:17:15 +02:00
|
|
|
1,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2019-02-07 21:27:43 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.3');
|
2019-02-24 07:33:25 +01:00
|
|
|
$this->project_analyzer->getCodebase()->store_node_types = true;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSimpleSymbolLookup()
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var int|null */
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
const BANANA = "🍌";
|
|
|
|
|
|
|
|
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-10-26 22:17:15 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
|
|
|
$this->assertSame('<?php public function foo() : void', $codebase->getSymbolInformation('somefile.php', 'B\A::foo()'));
|
|
|
|
$this->assertSame('<?php protected int|null $a', $codebase->getSymbolInformation('somefile.php', 'B\A::$a'));
|
|
|
|
$this->assertSame('<?php function B\bar() : int', $codebase->getSymbolInformation('somefile.php', 'B\bar()'));
|
2018-10-26 22:29:16 +02:00
|
|
|
$this->assertSame('<?php BANANA', $codebase->getSymbolInformation('somefile.php', 'B\A::BANANA'));
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSimpleSymbolLocation()
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var int|null */
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
const BANANA = "🍌";
|
|
|
|
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() : int {
|
|
|
|
return 5;
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$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());
|
2019-04-16 22:07:48 +02:00
|
|
|
$this->assertSame(37, $method_symbol_location->getColumn());
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$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);
|
|
|
|
$this->assertSame(13, $function_symbol_location->getLineNumber());
|
2019-04-16 22:07:48 +02:00
|
|
|
$this->assertSame(26, $function_symbol_location->getColumn());
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSymbolLookupAfterAlteration()
|
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2018-10-26 22:17:15 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-11-20 22:32:40 +01:00
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
2018-10-26 22:17:15 +02:00
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
|
|
|
$codebase->addTemporaryFileChanges(
|
|
|
|
'somefile.php',
|
2018-11-09 16:41:51 +01:00
|
|
|
'<?php
|
2018-10-26 22:17:15 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
2018-11-20 21:51:47 +01:00
|
|
|
|
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(10, 30));
|
|
|
|
|
|
|
|
$this->assertNotNull($symbol_at_position);
|
|
|
|
|
|
|
|
$this->assertSame('type: int|null', $symbol_at_position[0]);
|
|
|
|
|
|
|
|
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(12, 30));
|
|
|
|
|
|
|
|
$this->assertNotNull($symbol_at_position);
|
|
|
|
|
|
|
|
$this->assertSame('type: int', $symbol_at_position[0]);
|
|
|
|
|
|
|
|
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(17, 30));
|
|
|
|
|
|
|
|
$this->assertNotNull($symbol_at_position);
|
|
|
|
|
|
|
|
$this->assertSame('type: int', $symbol_at_position[0]);
|
|
|
|
}
|
|
|
|
}
|