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
18 changes: 18 additions & 0 deletions src/Type/Generic/TemplateKeyOfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ public function traverse(callable $cb): Type
return $this;
}

protected function getResult(): Type
{
$result = $this->getBound()->getResult();

$type = TemplateTypeFactory::create(
$this->getScope(),
$this->getName(),
$result,
$this->getVariance(),
);

if ($this->isArgument()) {
$type = TemplateTypeHelper::toArgument($type);
}

return $type;
}

protected function shouldGeneralizeInferredType(): bool
{
return false;
Expand Down
8 changes: 7 additions & 1 deletion src/Type/Generic/TemplateTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,14 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
$map = $this->getBound()->inferTemplateTypes($receivedType);
$resolvedBound = TypeUtils::resolveLateResolvableTypes(TemplateTypeHelper::resolveTemplateTypes($this->getBound(), $map));
if ($resolvedBound->isSuperTypeOf($receivedType)->yes()) {
if ($this->shouldGeneralizeInferredType()) {
$generalizedType = $receivedType->generalize(GeneralizePrecision::templateArgument());
if ($resolvedBound->isSuperTypeOf($generalizedType)->yes()) {
$receivedType = $generalizedType;
}
}
return (new TemplateTypeMap([
$this->name => $this->shouldGeneralizeInferredType() ? $receivedType->generalize(GeneralizePrecision::templateArgument()) : $receivedType,
$this->name => $receivedType,
]))->union($map);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ public function testBug7094(): void
$this->assertSame('Return type of call to method Bug7094\Foo::getAttribute() contains unresolvable type.', $errors[4]->getMessage());
$this->assertSame(79, $errors[4]->getLine());

$this->assertSame('Parameter #1 $attr of method Bug7094\Foo::setAttributes() expects array{foo?: string, bar?: 5|6|7, baz?: bool}, non-empty-array<string, 5|6|7|bool|string> given.', $errors[5]->getMessage());
$this->assertSame('Parameter #1 $attr of method Bug7094\Foo::setAttributes() expects array{foo?: string, bar?: 5|6|7, baz?: bool}, non-empty-array<K of string, 5|6|7|bool|string> given.', $errors[5]->getMessage());
$this->assertSame(29, $errors[5]->getLine());
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-argument-type.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7788.php');
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7788.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bug7788;

use function PHPStan\Testing\assertType;

/**
* @template T of array<string, mixed>
*/
final class Props
{
/**
* @param T $props
*/
public function __construct(private array $props = [])
{
}

/**
* @template K of key-of<T>
* @template TDefault
* @param K $propKey
* @param TDefault $default
* @return T[K]|TDefault
*/
public function getProp(string $propKey, mixed $default = null): mixed
{
return $this->props[$propKey] ?? $default;
}
}

function () {
assertType('int', (new Props(['title' => 'test', 'value' => 30]))->getProp('value', 0));
};