Skip to content

Commit

Permalink
Merge pull request #8955 from Nicelocal/fix
Browse files Browse the repository at this point in the history
Fix bug
  • Loading branch information
orklah committed Dec 20, 2022
2 parents 62db5d4 + 3549162 commit a89bbc3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Psalm/Internal/Codebase/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,22 +771,22 @@ public function getMethodReturnType(
if (((!$old_contained_by_new && !$new_contained_by_old)
|| ($old_contained_by_new && $new_contained_by_old))
&& !$candidate_type->hasTemplate()
&& !$overridden_storage->return_type->hasTemplate()
&& !$overridden_storage_return_type->hasTemplate()
) {
$attempted_intersection = null;
if ($old_contained_by_new) { //implicitly $new_contained_by_old as well
try {
$attempted_intersection = Type::intersectUnionTypes(
$candidate_type,
$overridden_storage->return_type,
$overridden_storage_return_type,
$source_analyzer->getCodebase(),
);
} catch (InvalidArgumentException $e) {
// TODO: fix
}
} else {
$attempted_intersection = Type::intersectUnionTypes(
$overridden_storage->return_type,
$overridden_storage_return_type,
$candidate_type,
$source_analyzer->getCodebase(),
);
Expand All @@ -803,15 +803,15 @@ public function getMethodReturnType(
return $candidate_type;
}

if ($old_contained_by_new) {
if ($old_contained_by_new || $overridden_storage_return_type->hasTemplate()) {
$self_class = $appearing_fq_class_storage->name;

return $candidate_type;
}

$self_class = $overridden_method_id->fq_class_name;

return $overridden_storage->return_type;
return $overridden_storage_return_type;
}
}

Expand Down
63 changes: 63 additions & 0 deletions tests/Template/ClassTemplateExtendsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,69 @@ class ClassTemplateExtendsTest extends TestCase
public function providerValidCodeParse(): iterable
{
return [
'interface' => [
'code' => '<?php
/**
* Singleton interface
*
* @template T
*/
interface ISingleton {
/**
* getInstance interface
*
* @return T
*/
public static function getInstance();
}
/**
* @psalm-consistent-constructor
*
* @implements ISingleton<Singleton&static>
*/
abstract class Singleton implements ISingleton {
/**
* By default, disallow construction of child classes.
*/
protected function __construct() {
}
/**
* Instance array
*
* @var array<class-string<static>, static>
*/
private static array $instances = [];
/**
* Clear all instances
*/
public static function clear(): void {
self::$instances = [];
}
/**
* Get instance
*/
public static function getInstance(): static {
$class = static::class;
return self::$instances[$class] ??= new static();
}
}
class a extends Singleton {
}
$a = a::getInstance();
',
'assertions' => [
'$a===' => 'a',
],
],
'phanTuple' => [
'code' => '<?php
namespace Phan\Library;
Expand Down

0 comments on commit a89bbc3

Please sign in to comment.