1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix access on matched T[K]

This commit is contained in:
Brown 2019-05-27 18:36:34 -04:00
parent 2eae79060f
commit cf02e9c22c
3 changed files with 32 additions and 10 deletions

View File

@ -488,8 +488,6 @@ class ArrayFetchAnalyzer
if ($offset_as instanceof Type\Atomic\TTemplateKeyOf
&& $offset_as->param_name === $original_type->param_name
&& $offset_as->defining_class === $original_type->defining_class
&& $offset_atomic_type->defining_class
=== $original_type->defining_class
) {
$type->type_params[1] = new Type\Union([
new Type\Atomic\TTemplateIndexedAccess(

View File

@ -189,6 +189,7 @@ abstract class Atomic
if (isset($template_type_map[$value])) {
$first_class = array_keys($template_type_map[$value])[0];
return new TTemplateParam(
$value,
$template_type_map[$value][$first_class][0],

View File

@ -2373,7 +2373,7 @@ class TemplateTest extends TestCase
'$c' => 'int',
]
],
'keyOfClassTemplate' => [
'keyOfClassTemplateAcceptingIndexedAccess' => [
'<?php
/**
* @template TData as array
@ -2391,13 +2391,6 @@ class TemplateTest extends TestCase
$this->data = $data;
}
/**
* @param key-of<TData> $property
*/
public function __get(string $property) {
return $this->data[$property] ?? null;
}
/**
* @template K as key-of<TData>
*
@ -2409,6 +2402,36 @@ class TemplateTest extends TestCase
}
}'
],
'keyOfClassTemplateReturningIndexedAccess' => [
'<?php
/**
* @template TData as array
*/
abstract class DataBag {
/**
* @var TData
*/
protected $data;
/**
* @param TData $data
*/
public function __construct(array $data) {
$this->data = $data;
}
/**
* @template K as key-of<TData>
*
* @param K $property
*
* @return TData[K]
*/
public function __get(string $property) {
return $this->data[$property];
}
}'
],
'unionTOrClassStringTPassedClassString' => [
'<?php
/**