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
36 changes: 34 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use PHPStan\Type\Generic\TypeProjectionHelper;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeAlias;
Expand Down Expand Up @@ -237,7 +238,7 @@ public function getParentClass(): ?ClassReflection
if ($this->isGeneric()) {
$extendedType = TemplateTypeHelper::resolveTemplateTypes(
$extendedType,
$this->getPossiblyIncompleteActiveTemplateTypeMap(),
$this->getActiveTemplateTypeMapForAncestorResolution(),
$this->getCallSiteVarianceMap(),
TemplateTypeVariance::createStatic(),
);
Expand Down Expand Up @@ -1164,7 +1165,7 @@ public function getImmediateInterfaces(): array
if ($this->isGeneric()) {
$implementedType = TemplateTypeHelper::resolveTemplateTypes(
$implementedType,
$this->getPossiblyIncompleteActiveTemplateTypeMap(),
$this->getActiveTemplateTypeMapForAncestorResolution(),
$this->getCallSiteVarianceMap(),
TemplateTypeVariance::createStatic(),
true,
Expand Down Expand Up @@ -1686,6 +1687,37 @@ public function getPossiblyIncompleteActiveTemplateTypeMap(): TemplateTypeMap
return $this->resolvedTemplateTypeMap ?? $this->getTemplateTypeMap();
}

/**
* Returns a template type map for resolving ancestor type declarations (@extends, @implements).
* Like getPossiblyIncompleteActiveTemplateTypeMap(), but resolves ErrorType entries
* to their template bounds when the bound is not mixed. This ensures that when a child
* class narrows a template bound (e.g. `@template T of SpecificType`), the narrowed bound
* is propagated to ancestor declarations instead of being lost as ErrorType.
*/
private function getActiveTemplateTypeMapForAncestorResolution(): TemplateTypeMap
{
$map = $this->getPossiblyIncompleteActiveTemplateTypeMap();
$templateTypeMap = $this->getTemplateTypeMap();

return $map->map(static function (string $name, Type $type) use ($templateTypeMap): Type {
if (!$type instanceof ErrorType) {
return $type;
}

$templateType = $templateTypeMap->getType($name);
if (!$templateType instanceof TemplateType) {
return $type;
}

$bound = $templateType->getBound();
if ($bound instanceof MixedType) {
return $type;
}

return TemplateTypeHelper::resolveToDefaults($templateType);
});
}

private function getDefaultCallSiteVarianceMap(): TemplateTypeVarianceMap
{
if ($this->defaultCallSiteVarianceMap !== null) {
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13204.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug13204;

use function PHPStan\Testing\assertType;

/**
* @template TChild of object
* @extends \ArrayAccess<int, TChild|null>
*/
interface ParentNode extends \ArrayAccess {}

class HelloWorld
{
public function sayHelloBug(object $node): void
{
if ($node instanceof ParentNode) {
assertType('object|null', $node[0]);
}
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-2676.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function (Wallet $wallet): void
assertType('DoctrineIntersectionTypeIsSupertypeOf\Collection&iterable<Bug2676\BankAccount>', $bankAccounts);

foreach ($bankAccounts as $key => $bankAccount) {
assertType('mixed', $key);
assertType('(int|string)', $key);
assertType('Bug2676\BankAccount', $bankAccount);
}
};
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-7185.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug7185;

use function PHPStan\Testing\assertType;

/**
* @template TKey of array-key
* @template TValue of object
* @extends \IteratorAggregate<TKey, TValue>
*/
interface Collection extends \IteratorAggregate {}

function foo(Collection $list): void {
$all = iterator_to_array($list);
assertType('array<object>', $all);
assertType('object|false', current($all));
}
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9045.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug9045;

use function PHPStan\Testing\assertType;

interface TranslationInterface {}
interface TransportTranslationInterface extends TranslationInterface {
public function getAdditionalInformation(): ?string;
}

/**
* @template T of TransportTranslationInterface
* @extends TranslatableInterface<T>
*/
interface TransportInterface extends TranslatableInterface {}

/**
* @template T of TranslationInterface
*/
interface TranslatableInterface
{
/** @phpstan-return T */
public function getTranslation(): TranslationInterface;
}

class Foo {
public function bar(TransportInterface $transport): void {
assertType('Bug9045\TransportTranslationInterface', $transport->getTranslation());
$transport->getTranslation()->getAdditionalInformation();
}
}
Loading