Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip intersection of template types during inheritance check #8926

Merged
merged 2 commits into from Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Psalm/Internal/Codebase/Methods.php
Expand Up @@ -768,8 +768,10 @@ public function getMethodReturnType(
$candidate_type
);

if ((!$old_contained_by_new && !$new_contained_by_old)
|| ($old_contained_by_new && $new_contained_by_old)
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()
) {
$attempted_intersection = null;
if ($old_contained_by_new) { //implicitly $new_contained_by_old as well
Expand Down
43 changes: 43 additions & 0 deletions tests/Template/ClassTemplateTest.php
Expand Up @@ -16,6 +16,49 @@ class ClassTemplateTest extends TestCase
public function providerValidCodeParse(): iterable
{
return [
'templateIntersection' => [
'code' => '<?php
interface EntityInterface
{
public function getId(): string;
}
/**
* @phpstan-template T of EntityInterface
*/
interface RepositoryInterface
{
/**
* @return T|null
*/
public function byId(string $id);
}
final class Foo implements EntityInterface
{
public function getId(): string
{
return "42";
}
}
/**
* @phpstan-implements RepositoryInterface<Foo>
*/
final class FooRepository implements RepositoryInterface
{
/**
* @var Foo[]
*/
public array $elements = [];
public function byId(string $id): ?Foo
{
return $this->elements[$id] ?? null;
}
}
'
],
'cachingIterator' => [
'code' => '<?php
Expand Down