mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix jumping to definition on nullable parameters (#3804)
Currently it's not possible to "Go to definition" (LSP) on nullable args like `function( ?MyClass )` as the reference is stored a `MyClass|null` in the reference map, which will now resolve to a class name. This PR removed any nullable type from the union before adding it to the reference map (as the reference map is only use to indicate a symbol was used in a given location, I think this makes sense).
This commit is contained in:
parent
2afbf58324
commit
11af82a97f
@ -977,13 +977,17 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
|
||||
$signature_type_location = $function_param->signature_type_location;
|
||||
|
||||
if ($signature_type && $signature_type_location && $signature_type->hasObjectType()) {
|
||||
$referenced_type = $signature_type;
|
||||
if ($referenced_type->isNullable()) {
|
||||
$referenced_type = clone $referenced_type;
|
||||
$referenced_type->removeType('null');
|
||||
}
|
||||
list($start, $end) = $signature_type_location->getSelectionBounds();
|
||||
|
||||
$codebase->analyzer->addOffsetReference(
|
||||
$this->getFilePath(),
|
||||
$start,
|
||||
$end,
|
||||
(string) $signature_type
|
||||
(string) $referenced_type
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -275,6 +275,35 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
|
||||
$this->assertSame('B\A::foo()', $symbol_at_position[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetSymbolPositionNullableArg()
|
||||
{
|
||||
$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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user