1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add transformations for new A, param A

This commit is contained in:
Matthew Brown 2019-06-01 10:32:49 -04:00
parent 7e4de611bf
commit bee87c25eb
3 changed files with 63 additions and 2 deletions

View File

@ -361,6 +361,45 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer implements Statements
$check_stmts = true;
if ($codebase->method_migrations
&& $context->calling_method_id
&& isset($codebase->method_migrations[strtolower($context->calling_method_id)])
) {
$destination_method_id = $codebase->method_migrations[strtolower($context->calling_method_id)];
foreach ($this->function->params as $param) {
$param_name_node = null;
if ($param->type instanceof PhpParser\Node\Name) {
$param_name_node = $param->type;
} elseif ($param->type instanceof PhpParser\Node\NullableType
&& $param->type->type instanceof PhpParser\Node\Name
) {
$param_name_node = $param->type->type;
}
if ($param_name_node) {
$resolved_name = (string) $param_name_node->getAttribute('resolvedName');
$parent_fqcln = $this->getParentFQCLN();
if ($resolved_name === 'self' && $context->self) {
$resolved_name = (string) $context->self;
} elseif ($resolved_name === 'parent' && $parent_fqcln) {
$resolved_name = $parent_fqcln;
}
$codebase->classlikes->airliftClassLikeReference(
$resolved_name,
explode('::', $destination_method_id)[0],
$statements_analyzer->getFilePath(),
(int) $param_name_node->getAttribute('startFilePos'),
(int) $param_name_node->getAttribute('endFilePos') + 1
);
}
}
}
foreach ($params as $offset => $function_param) {
$signature_type = $function_param->signature_type;
$signature_type_location = $function_param->signature_type_location;

View File

@ -246,6 +246,22 @@ class NewAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\CallAna
}
if ($fq_class_name) {
if ($stmt->class instanceof PhpParser\Node\Name
&& $codebase->method_migrations
&& $context->calling_method_id
&& isset($codebase->method_migrations[strtolower($context->calling_method_id)])
) {
$destination_method_id = $codebase->method_migrations[strtolower($context->calling_method_id)];
$codebase->classlikes->airliftClassLikeReference(
$fq_class_name,
explode('::', $destination_method_id)[0],
$statements_analyzer->getFilePath(),
(int) $stmt->class->getAttribute('startFilePos'),
(int) $stmt->class->getAttribute('endFilePos') + 1
);
}
if ($context->check_classes) {
if ($context->isPhantomClass($fq_class_name)) {
return null;

View File

@ -172,12 +172,15 @@ class MoveMethodTest extends \Psalm\Tests\TestCase
/**
* @return void
*/
public static function Foo() : void {
public static function Foo(self $a1, ?self $a2) : void {
echo self::C;
echo A::C;
self::Bar();
A::Bar();
echo \Ns\B::D;
new A();
new self();
new B();
}
public static function Bar() : void {}
@ -203,12 +206,15 @@ class MoveMethodTest extends \Psalm\Tests\TestCase
/**
* @return void
*/
public static function Fedbca() : void {
public static function Fedbca(A $a1, ?A $a2) : void {
echo A::C;
echo A::C;
A::Bar();
A::Bar();
echo self::D;
new A();
new A();
new self();
}
}',
[