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 CompletionTest 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,
|
2019-05-27 19:07:02 +02:00
|
|
|
false
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
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 testCompletionOnThisWithNoAssignment()
|
|
|
|
{
|
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() {
|
|
|
|
$this->
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
$this->assertSame(['B\A', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnThisWithAssignmentBelow()
|
|
|
|
{
|
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() : self {
|
|
|
|
$this->
|
|
|
|
|
|
|
|
$a = "foo";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
$this->assertSame(['B\A', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnThisWithIfBelow()
|
|
|
|
{
|
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() : self {
|
|
|
|
$this
|
|
|
|
|
|
|
|
if(rand(0, 1)) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
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() : self {
|
|
|
|
$this->
|
|
|
|
|
|
|
|
if(rand(0, 1)) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
2018-11-20 21:51:47 +01:00
|
|
|
$codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$this->assertSame(['B\A', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(8, 31)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnThisProperty()
|
|
|
|
{
|
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 C {
|
|
|
|
public function otherFunction() : void
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var C */
|
|
|
|
protected $cee_me;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->cee_me = new C();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
$this->cee_me->
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
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());
|
|
|
|
|
|
|
|
$this->assertSame(['B\C', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 39)));
|
|
|
|
}
|
2019-05-17 18:11:21 +02:00
|
|
|
|
2019-05-17 18:38:29 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnThisPropertyWithCharacter()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function otherFunction() : void
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var C */
|
|
|
|
protected $cee_me;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->cee_me = new C();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
$this->cee_me->o
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
|
|
|
$this->assertSame(['B\C', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 40)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnThisPropertyWithAnotherCharacter()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
public function otherFunction() : void
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var C */
|
|
|
|
protected $cee_me;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->cee_me = new C();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
$this->cee_me->ot
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
|
|
|
$this->assertSame(null, $codebase->getCompletionDataAtPosition('somefile.php', new Position(16, 41)));
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:11:21 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnTemplatedThisProperty()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
/** @template T */
|
|
|
|
class C {
|
|
|
|
/** @var T */
|
|
|
|
private $t;
|
|
|
|
|
|
|
|
/** @param T $t */
|
|
|
|
public function __construct($t) {
|
|
|
|
$this->t = $t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function otherFunction() : void
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var C<string> */
|
|
|
|
protected $cee_me;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->cee_me = new C("hello");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
$this->cee_me->
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
|
|
|
$completion_data = $codebase->getCompletionDataAtPosition('somefile.php', new Position(25, 39));
|
|
|
|
|
|
|
|
$this->assertSame(['B\C<string>', '->'], $completion_data);
|
|
|
|
|
|
|
|
$completion_items = $codebase->getCompletionItemsForClassishThing($completion_data[0], $completion_data[1]);
|
|
|
|
|
|
|
|
$this->assertCount(3, $completion_items);
|
|
|
|
}
|
2019-05-17 18:38:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnMethodReturnValue()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
class A {
|
|
|
|
public function foo() : self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function (A $a) {
|
|
|
|
$a->foo()->
|
|
|
|
}
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
$this->assertSame(['B\A', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(9, 31)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnMethodArgument()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
class A {
|
|
|
|
public function foo(A $a) : self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
function (A $a, C $c) {
|
|
|
|
$a->foo($c->)
|
|
|
|
}
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
$this->assertSame(['B\C', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(11, 32)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCompletionOnMethodReturnValueWithArgument()
|
|
|
|
{
|
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$config = $codebase->config;
|
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
class A {
|
|
|
|
public function foo(A $a) : self {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
function (A $a, C $c) {
|
|
|
|
$a->foo($c)->
|
|
|
|
}
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$codebase->file_provider->openFile('somefile.php');
|
|
|
|
$codebase->scanFiles();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
$this->assertSame(['B\A', '->'], $codebase->getCompletionDataAtPosition('somefile.php', new Position(11, 33)));
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|