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

try to fix generic assertions for list and array

This commit is contained in:
adrew 2021-12-06 02:25:40 +03:00
parent 056497e73d
commit dee3fc4358
3 changed files with 95 additions and 2 deletions

View File

@ -784,8 +784,9 @@ class CallAnalyzer
if ($replacement_atomic_type instanceof Type\Atomic\TArray
|| $replacement_atomic_type instanceof Type\Atomic\TKeyedArray
|| $replacement_atomic_type instanceof Type\Atomic\TList
) {
$ored_type_assertions[] = $prefix . 'array';
$ored_type_assertions[] = $prefix . $replacement_atomic_type->getId();
} elseif ($replacement_atomic_type instanceof Type\Atomic\TNamedObject) {
$ored_type_assertions[] = $prefix . $replacement_atomic_type->value;
} elseif ($replacement_atomic_type instanceof Type\Atomic\Scalar) {

View File

@ -77,7 +77,7 @@ class Assertion
if ($first_type instanceof TTemplateParam) {
$rule_token[0] = $first_type->param_name;
} else {
$rule_token[0] = $first_type->getKey();
$rule_token[0] = $first_type->getId();
}
}
}

View File

@ -768,6 +768,98 @@ class FunctionTemplateAssertTest extends TestCase
}
}'
],
'ifTrueListAssertionFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert-if-true T $toCheck
*/
function is($toCheck): bool
{
throw new RuntimeException("???");
}
}
/**
* @param list<int> $_list
*/
function acceptsIntList(array $_list): void {}
/** @var Type<list<int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
if ($numbersT->is($mixed)) {
acceptsIntList($mixed);
}'
],
'assertListFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert T $toCheck
*/
function assert($toCheck): void
{
}
}
/**
* @param list<int> $_list
*/
function acceptsIntList(array $_list): void {}
/** @var Type<list<int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
$numbersT->assert($mixed);
acceptsIntList($mixed);'
],
'assertArrayFromGeneric' => [
'<?php
/**
* @template T
*/
final class Type
{
/**
* @param mixed $toCheck
* @psalm-assert T $toCheck
*/
function assert($toCheck): void
{
}
}
/**
* @param array<string, int> $_list
*/
function acceptsArray(array $_list): void {}
/** @var Type<array<string, int>> $numbersT */
$numbersT = new Type();
/** @var mixed $mixed */
$mixed = null;
$numbersT->assert($mixed);
acceptsArray($mixed);'
],
];
}