1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #7311 from orklah/inArrayAssertionOnTemplate

don't crash when pushing a template to in_array
This commit is contained in:
orklah 2022-01-06 00:14:22 +01:00 committed by GitHub
commit 3dbc039415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -3556,7 +3556,7 @@ class AssertionFinder
// - The array may have one of the types but not the others.
//
// NOTE: the negation of the negation is the original assertion.
if ($value_type->getId() !== '' && !$value_type->isMixed()) {
if ($value_type->getId() !== '' && !$value_type->isMixed() && !$value_type->hasTemplate()) {
$assertions[] = 'in-array-' . $value_type->getId();
}
} else {

View File

@ -1898,6 +1898,29 @@ class AssertAnnotationTest extends TestCase
function requiresString(string $_str): void {}
',
],
'assertInArrayWithTemplateDontCrash' => [
'<?php
class A{
/**
* @template T
* @param array<T> $objects
* @return array<T>
*/
private function uniquateObjects(array $objects) : array
{
$uniqueObjects = [];
foreach ($objects as $object) {
if (in_array($object, $uniqueObjects, true)) {
continue;
}
$uniqueObjects[] = $object;
}
return $uniqueObjects;
}
}
',
],
'assertionOnMagicProperty' => [
'<?php
/**