Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Type/Generic/TemplateTypeVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b): I
}

if ($this->invariant()) {
if ($a instanceof TemplateType && $b instanceof TemplateType
&& $a->getScope()->equals($b->getScope())
&& $a->getName() === $b->getName()
) {
return IsSuperTypeOfResult::createYes();
}

$result = $a->equals($b);
$reasons = [];
if (!$result) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,11 @@ public function testBug13565(): void
]);
}

public function testBug13190(): void
{
$this->checkNullables = true;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-13190.php'], []);
}

}
55 changes: 55 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13190.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php // lint >= 8.0
declare(strict_types = 1);

namespace Bug13190;

/**
* @template T
*/
interface Box
{
/**
* @return T
*/
public function toInner(): mixed;
}

/**
* @template T
*
* @implements Box<T>
*/
final class BoxImpl implements Box
{
/**
* @param T $value
*/
public function __construct(
private mixed $value,
) {}

/**
* @return T
*/
#[\Override]
public function toInner(): mixed
{
return $this->value;
}
}

/**
* @template T
*
* @param T|Box<T> $to_box
*
* @return Box<T>
*/
function inbox($to_box): Box
{
if ($to_box instanceof Box) {
return $to_box;
} else {
return new BoxImpl($to_box);
}
}
Loading