1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +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:
Joe Hoyle 2020-07-11 23:12:03 +02:00 committed by GitHub
parent 2afbf58324
commit 11af82a97f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -977,13 +977,17 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
$signature_type_location = $function_param->signature_type_location; $signature_type_location = $function_param->signature_type_location;
if ($signature_type && $signature_type_location && $signature_type->hasObjectType()) { 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(); list($start, $end) = $signature_type_location->getSelectionBounds();
$codebase->analyzer->addOffsetReference( $codebase->analyzer->addOffsetReference(
$this->getFilePath(), $this->getFilePath(),
$start, $start,
$end, $end,
(string) $signature_type (string) $referenced_type
); );
} }

View File

@ -275,6 +275,35 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
$this->assertSame('B\A::foo()', $symbol_at_position[0]); $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 * @return void
*/ */