1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #6419 from TysonAndre/in_array-fix

This commit is contained in:
Bruce Weirdan 2021-09-04 21:56:36 +03:00 committed by GitHub
commit b710aab148
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 5 deletions

View File

@ -3442,7 +3442,7 @@ class AssertionFinder
$assertions = [];
if (!$is_sealed) {
if ($value_type->getId() !== '') {
if ($value_type->getId() !== '' && !$value_type->isMixed()) {
$assertions[] = 'in-array-' . $value_type->getId();
}
} else {
@ -3453,10 +3453,7 @@ class AssertionFinder
|| $atomic_value_type instanceof Type\Atomic\TEnumCase
) {
$assertions[] = '=' . $atomic_value_type->getAssertionString();
} elseif ($atomic_value_type instanceof Type\Atomic\TFalse
|| $atomic_value_type instanceof Type\Atomic\TTrue
|| $atomic_value_type instanceof Type\Atomic\TNull
) {
} else {
$assertions[] = $atomic_value_type->getAssertionString();
}
}

View File

@ -169,6 +169,32 @@ class InArrayTest extends \Psalm\Tests\TestCase
[],
'8.0'
],
'in_arrayNullOrString' => [
'<?php
function test(?string $x, string $y): void {
if (in_array($x, [null, $y], true)) {
if ($x === null) {
echo "Saw null\n";
}
echo "Saw $x\n";
}
}',
[],
[],
'8.0'
],
'in_array-mixed-twice' => [
'<?php
function contains(array $list1, array $list2, mixed $element): void
{
if (in_array($element, $list1, true)) {
} elseif (in_array($element, $list2, true)) {
}
}',
[],
[],
'8.0'
],
];
}

View File

@ -961,6 +961,39 @@ class ValueTest extends \Psalm\Tests\TestCase
}',
'error_message' => 'RedundantCondition',
],
'inArrayRemoveNull' => [
'<?php
function x(?string $foo, string $bar): void {
if (!in_array($foo, [$bar], true)) {
throw new Exception();
}
if (is_string($foo)) {}
}',
'error_message' => 'RedundantCondition',
],
'inArrayDetectType' => [
'<?php
function x($foo, string $bar): void {
if (!in_array($foo, [$bar], true)) {
throw new Exception();
}
if (is_string($foo)) {}
}',
// foo is always string
'error_message' => 'RedundantCondition',
],
'inArrayRemoveInvalid' => [
'<?php
function x(?string $foo, int $bar): void {
if (!in_array($foo, [$bar], true)) {
throw new Exception();
}
}',
// Type null|string is never int
'error_message' => 'RedundantCondition',
],
'neverNotIdenticalFloatType' => [
'<?php
$a = 4.1;