2018-10-26 22:17:15 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
namespace Psalm\Tests\LanguageServer;
|
|
|
|
|
|
|
|
use LanguageServerProtocol\Position;
|
2022-02-15 01:18:49 +01:00
|
|
|
use LanguageServerProtocol\Range;
|
2022-12-20 22:09:34 +01:00
|
|
|
use Psalm\Codebase;
|
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;
|
2022-02-15 01:18:49 +01:00
|
|
|
use Psalm\Internal\LanguageServer\Reference;
|
2021-07-02 01:10:21 +02:00
|
|
|
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;
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class SymbolLookupTest extends TestCase
|
2018-10-26 22:17:15 +02: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
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new FakeFileProvider();
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
2021-12-03 20:11:20 +01:00
|
|
|
new ParserInstanceCacheProvider(),
|
2018-10-26 22:17:15 +02:00
|
|
|
null,
|
|
|
|
null,
|
2021-12-04 21:55:53 +01:00
|
|
|
new FakeFileReferenceCacheProvider(),
|
2022-12-18 17:15:15 +01:00
|
|
|
new ProjectCacheProvider(),
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase = new Codebase($config, $providers);
|
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2018-10-26 22:17:15 +02:00
|
|
|
$config,
|
2022-02-15 01:18:49 +01:00
|
|
|
$providers,
|
|
|
|
null,
|
|
|
|
[],
|
|
|
|
1,
|
|
|
|
null,
|
2022-12-20 22:09:34 +01:00
|
|
|
$this->codebase,
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.3', 'tests');
|
2019-02-24 07:33:25 +01:00
|
|
|
$this->project_analyzer->getCodebase()->store_node_types = true;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSimpleSymbolLookup(): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2022-02-18 18:29:09 +01:00
|
|
|
$config = $this->codebase->config;
|
2021-01-29 00:58:02 +01:00
|
|
|
$config->globals['$my_global'] = 'string';
|
2018-10-26 22:17:15 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
2021-01-28 17:18:28 +01:00
|
|
|
const APPLE = "🍏";
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
class A {
|
|
|
|
/** @var int|null */
|
|
|
|
protected $a;
|
|
|
|
|
|
|
|
const BANANA = "🍌";
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
$a = 1;
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar() : int {
|
|
|
|
return 5;
|
2019-06-22 19:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function baz(int $a) : int {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
function qux(int $a, int $b) : int {
|
|
|
|
return $a + $b;
|
2021-01-29 00:58:02 +01:00
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
$_SESSION;',
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$range = new Range(new Position(1, 1), new Position(1, 1));
|
2021-02-24 16:14:04 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\A::foo()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("public function foo(): void", $information->code);
|
|
|
|
$this->assertSame("B\A::foo", $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\A::$a',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame('protected int|null $a', $information->code);
|
|
|
|
$this->assertSame('B\A::$a', $information->title);
|
|
|
|
$this->assertSame('', $information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\bar()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame('function B\bar(): int', $information->code);
|
|
|
|
$this->assertSame('b\bar', $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\A::BANANA',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame('public const BANANA = 🍌;', $information->code);
|
|
|
|
$this->assertSame('B\A::BANANA', $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\baz()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("function B\baz(\n int \$a\n): int", $information->code);
|
|
|
|
$this->assertSame('b\baz', $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'B\qux()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("function B\qux(\n int \$a,\n int \$b\n): int", $information->code);
|
|
|
|
$this->assertSame('b\qux', $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
2022-11-30 19:28:13 +01:00
|
|
|
'$_SESSION',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-11-30 19:28:13 +01:00
|
|
|
$this->assertSame("array<string, mixed>", $information->code);
|
|
|
|
$this->assertSame('$_SESSION', $information->title);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'$my_global',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("string", $information->code);
|
|
|
|
$this->assertSame('$my_global', $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2021-01-28 17:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleSymbolLookupGlobalConst(): void
|
|
|
|
{
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
define("BANANA", "🍌");
|
2022-12-18 17:15:15 +01:00
|
|
|
const APPLE = "🍏";',
|
2021-01-28 17:18:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$range = new Range(new Position(1, 1), new Position(1, 1));
|
2021-01-28 17:18:28 +01:00
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2021-02-24 16:14:04 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'APPLE',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("const APPLE string", $information->code);
|
|
|
|
$this->assertSame("APPLE", $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2022-02-15 01:18:49 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$information = $this->codebase->getMarkupContentForSymbolByReference(
|
2022-02-15 01:18:49 +01:00
|
|
|
new Reference(
|
|
|
|
'somefile.php',
|
|
|
|
'BANANA',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
|
|
|
),
|
2022-02-15 01:18:49 +01:00
|
|
|
);
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($information);
|
2022-02-18 18:29:09 +01:00
|
|
|
$this->assertSame("const BANANA string", $information->code);
|
|
|
|
$this->assertSame("BANANA", $information->title);
|
|
|
|
$this->assertNull($information->description);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSimpleSymbolLocation(): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
|
|
|
$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";
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2019-07-02 00:48:33 +02:00
|
|
|
public function foo() : void {
|
|
|
|
$a = 1;
|
|
|
|
echo $a;
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bar() : int {
|
|
|
|
return 5;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$range = new Range(new Position(1, 1), new Position(1, 1));
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$method_symbol_location = $this->codebase->getSymbolLocationByReference(new Reference(
|
2022-02-15 01:18:49 +01:00
|
|
|
'somefile.php',
|
|
|
|
'B\A::foo()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
2022-02-15 01:18:49 +01:00
|
|
|
));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$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
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$property_symbol_location = $this->codebase->getSymbolLocationByReference(new Reference(
|
2022-02-15 01:18:49 +01:00
|
|
|
'somefile.php',
|
|
|
|
'B\A::$a',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
2022-02-15 01:18:49 +01:00
|
|
|
));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($property_symbol_location);
|
|
|
|
$this->assertSame(6, $property_symbol_location->getLineNumber());
|
|
|
|
$this->assertSame(31, $property_symbol_location->getColumn());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$constant_symbol_location = $this->codebase->getSymbolLocationByReference(new Reference(
|
2022-02-15 01:18:49 +01:00
|
|
|
'somefile.php',
|
|
|
|
'B\A::BANANA',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
2022-02-15 01:18:49 +01:00
|
|
|
));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($constant_symbol_location);
|
|
|
|
$this->assertSame(8, $constant_symbol_location->getLineNumber());
|
|
|
|
$this->assertSame(27, $constant_symbol_location->getColumn());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$function_symbol_location = $this->codebase->getSymbolLocationByReference(new Reference(
|
2022-02-15 01:18:49 +01:00
|
|
|
'somefile.php',
|
|
|
|
'B\bar()',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
2022-02-15 01:18:49 +01:00
|
|
|
));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($function_symbol_location);
|
2019-07-02 00:48:33 +02:00
|
|
|
$this->assertSame(16, $function_symbol_location->getLineNumber());
|
2019-04-16 22:07:48 +02:00
|
|
|
$this->assertSame(26, $function_symbol_location->getColumn());
|
2019-07-02 00:48:33 +02:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$function_symbol_location = $this->codebase->getSymbolLocationByReference(new Reference(
|
2022-02-15 01:18:49 +01:00
|
|
|
'somefile.php',
|
|
|
|
'257-259',
|
2022-12-20 22:09:34 +01:00
|
|
|
$range,
|
2022-02-15 01:18:49 +01:00
|
|
|
));
|
2019-07-02 00:48:33 +02:00
|
|
|
|
|
|
|
$this->assertNotNull($function_symbol_location);
|
|
|
|
$this->assertSame(11, $function_symbol_location->getLineNumber());
|
|
|
|
$this->assertSame(25, $function_symbol_location->getColumn());
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSymbolLookupAfterAlteration(): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2018-10-26 22:17:15 +02:00
|
|
|
$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;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2018-10-26 22:17:15 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->addTemporaryFileChanges(
|
2018-10-26 22:17:15 +02:00
|
|
|
'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;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2018-10-26 22:17:15 +02:00
|
|
|
);
|
2018-11-20 21:51:47 +01:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->reloadFiles($this->project_analyzer, ['somefile.php']);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(10, 30));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('245-246:int|null', $reference->symbol);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(12, 30));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('213-214:1', $reference->symbol);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(17, 30));
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2018-10-26 22:17:15 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('425-426:2', $reference->symbol);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
2019-06-23 14:08:00 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetSymbolPositionMissingArg(): void
|
2019-06-23 14:08:00 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2019-06-23 14:08:00 +02:00
|
|
|
$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();
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-06-23 14:08:00 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2019-06-23 14:08:00 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(9, 33));
|
2019-06-23 14:08:00 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2019-06-23 14:08:00 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('B\A::foo()', $reference->symbol);
|
2019-06-23 14:08:00 +02:00
|
|
|
}
|
2019-06-23 14:47:49 +02:00
|
|
|
|
2021-01-29 00:58:02 +01:00
|
|
|
public function testGetSymbolPositionGlobalVariable(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->reportUnusedVariables();
|
|
|
|
$config = $this->codebase->config;
|
2021-01-29 00:58:02 +01:00
|
|
|
$config->throw_exception = false;
|
|
|
|
$config->globals['$my_global'] = 'string';
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
global $my_global;
|
|
|
|
echo $my_global;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-01-29 00:58:02 +01:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2021-01-29 00:58:02 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(2, 31));
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
|
|
|
$this->assertSame('$my_global', $reference->symbol);
|
2021-01-29 00:58:02 +01:00
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(3, 28));
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
|
|
|
$this->assertSame('73-82:string', $reference->symbol);
|
2021-01-29 00:58:02 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetSymbolPositionNullableArg(): void
|
2020-07-11 23:12:03 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2020-07-11 23:12:03 +02:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
class AClass {
|
|
|
|
}
|
|
|
|
function B( ?AClass $class ) {
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-11 23:12:03 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2020-07-11 23:12:03 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(4, 33));
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2020-07-11 23:12:03 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('B\AClass', $reference->symbol);
|
2020-07-11 23:12:03 +02:00
|
|
|
}
|
2020-08-23 16:32:07 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetSymbolPositionMethodWrongReturnType(): void
|
2020-07-11 23:14:39 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2020-07-11 23:14:39 +02:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
class AClass {
|
|
|
|
/**
|
|
|
|
* @return Some
|
|
|
|
*/
|
|
|
|
protected function get_command() : AClass {
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2020-07-11 23:14:39 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2020-07-11 23:14:39 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(6, 60));
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2020-07-11 23:14:39 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('B\AClass', $reference->symbol);
|
2020-07-11 23:14:39 +02:00
|
|
|
}
|
2020-08-23 16:32:07 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetSymbolPositionUseStatement(): void
|
2020-07-11 23:16:44 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2020-07-11 23:16:44 +02:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
use StreamWrapper;
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2020-07-11 23:16:44 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2020-07-11 23:16:44 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(2, 25));
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2020-07-11 23:16:44 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('StreamWrapper', $reference->symbol);
|
2020-07-11 23:16:44 +02:00
|
|
|
}
|
2020-07-11 23:12:03 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetSymbolPositionRange(): void
|
2020-07-09 22:24:51 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2020-07-09 22:24:51 +02:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
function foo() : string {
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
$active_symbol = foo();',
|
2020-07-09 22:24:51 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2020-07-09 22:24:51 +02:00
|
|
|
$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)
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(6, 26));
|
2020-07-09 22:24:51 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
|
|
|
$this->assertSame(16, $reference->range->start->character);
|
|
|
|
$this->assertSame(30, $reference->range->end->character);
|
2020-07-09 22:24:51 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testGetTypeInDocblock(): void
|
2019-06-23 14:47:49 +02:00
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2019-06-23 14:47:49 +02:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @var \Exception|null */
|
|
|
|
public $prop;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-06-23 14:47:49 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2019-06-23 14:47:49 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2023-03-02 19:02:23 +01:00
|
|
|
$reference = $this->codebase->getReferenceAtPositionAsReference('somefile.php', new Position(4, 35));
|
2019-06-23 14:47:49 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertNotNull($reference);
|
2019-06-23 14:47:49 +02:00
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->assertSame('Exception', $reference->symbol);
|
2019-06-23 14:47:49 +02:00
|
|
|
}
|
2019-07-01 21:54:33 +02:00
|
|
|
|
|
|
|
/**
|
2019-07-19 05:08:54 +02:00
|
|
|
* @return array<int, array{0: Position, 1: ?string, 2: ?int, 3: ?int}>
|
2019-07-01 21:54:33 +02:00
|
|
|
*/
|
|
|
|
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],
|
2020-08-29 19:36:56 +02:00
|
|
|
// [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],
|
2019-07-01 21:54:33 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2019-07-01 21:54:33 +02:00
|
|
|
): void {
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2019-07-01 21:54:33 +02:00
|
|
|
$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();
|
|
|
|
|
2019-07-19 03:50:59 +02:00
|
|
|
strlen();
|
2019-07-01 21:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function bar(string $a) {}
|
|
|
|
|
|
|
|
public static function staticFoo(string $a) {}
|
|
|
|
|
2019-07-02 00:48:33 +02:00
|
|
|
public function __construct() {}
|
2019-07-01 21:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function foo(string $a) {
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-07-01 21:54:33 +02:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2019-07-01 21:54:33 +02:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$reference_location = $this->codebase->getFunctionArgumentAtPosition('somefile.php', $position);
|
2019-07-19 05:08:54 +02:00
|
|
|
|
|
|
|
if ($expected_symbol !== null) {
|
|
|
|
$this->assertNotNull($reference_location);
|
2020-09-02 06:17:41 +02:00
|
|
|
[$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);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$symbol_information = $this->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);
|
|
|
|
}
|
2019-07-01 21:54:33 +02:00
|
|
|
}
|
2021-02-24 16:14:04 +01:00
|
|
|
|
|
|
|
public function testGetSignatureHelpIncludesParamDescription(): void
|
|
|
|
{
|
2022-02-15 01:18:49 +01:00
|
|
|
$config = $this->codebase->config;
|
2021-02-24 16:14:04 +01:00
|
|
|
$config->throw_exception = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace B;
|
|
|
|
/**
|
|
|
|
* @param string $a The first param, a.
|
|
|
|
*/
|
|
|
|
function foo(string $a) {
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
foo();',
|
2021-02-24 16:14:04 +01:00
|
|
|
);
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$this->codebase->file_provider->openFile('somefile.php');
|
|
|
|
$this->codebase->scanFiles();
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
|
2022-02-15 01:18:49 +01:00
|
|
|
$reference_location = $this->codebase->getFunctionArgumentAtPosition('somefile.php', new Position(7, 20));
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($reference_location);
|
2022-02-15 01:18:49 +01:00
|
|
|
$symbol_information = $this->codebase->getSignatureInformation($reference_location[0], 'somefile.php');
|
2021-02-24 16:14:04 +01:00
|
|
|
$this->assertNotNull($symbol_information);
|
|
|
|
$this->assertNotNull($symbol_information->parameters);
|
|
|
|
$this->assertEquals('The first param, a.', $symbol_information->parameters[0]->documentation);
|
|
|
|
}
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|