1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #5070 – fix static return type inference in static methods

This commit is contained in:
Matt Brown 2021-01-22 09:58:09 -05:00
parent 28d2795e59
commit 6f30399189
3 changed files with 38 additions and 1 deletions

View File

@ -299,7 +299,8 @@ class ExistingAtomicStaticCallAnalyzer
true,
false,
\is_string($static_type)
&& $static_type !== $context->self
&& ($static_type !== $context->self
|| $class_storage->final)
);
$return_type_location = $codebase->methods->getMethodReturnTypeLocation(

View File

@ -165,6 +165,8 @@ class TypeExpander
$return_type->extra_types[$extra_static_type->getKey()] = clone $extra_static_type;
}
}
} elseif ($return_type->was_static && is_string($static_class_type) && $final) {
$return_type->value = $static_class_type;
} elseif ($self_class && $return_type_lc === 'self') {
$return_type->value = $self_class;
} elseif ($parent_class && $return_type_lc === 'parent') {

View File

@ -869,6 +869,40 @@ class MethodSignatureTest extends TestCase
[],
'7.4'
],
'extendStaticReturnTypeInFinal' => [
'<?php
final class B extends A
{
public static function doCretate1(): self
{
return self::create1();
}
public static function doCretate2(): self
{
return self::create2();
}
}
abstract class A
{
final private function __construct() {}
final protected static function create1(): static
{
return new static();
}
/** @return static */
final protected static function create2()
{
return new static();
}
}',
[],
[],
'8.0'
],
];
}