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

Use different in-array-* check for non-literals

Negating this would not contradict the original condition if the
variables were different. A string can be in one array of strings but
not be in a different array of strings.
This commit is contained in:
Tyson Andre 2021-09-06 21:42:56 -04:00
parent 7df819e585
commit f4f989d1e5
3 changed files with 46 additions and 25 deletions

View File

@ -3456,13 +3456,22 @@ class AssertionFinder
}
} else {
foreach ($value_type->getAtomicTypes() as $atomic_value_type) {
if ($atomic_value_type instanceof Type\Atomic\TFalse
if ($atomic_value_type instanceof Type\Atomic\TLiteralInt
|| $atomic_value_type instanceof Type\Atomic\TLiteralString
|| $atomic_value_type instanceof Type\Atomic\TLiteralFloat
|| $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
) {
$assertions[] = $atomic_value_type->getAssertionString();
} else {
$assertions[] = '=' . $atomic_value_type->getAssertionString();
} elseif (!$atomic_value_type instanceof Type\Atomic\TMixed) {
// mixed doesn't tell us anything and can be omitted.
//
// For the meaning of in-array, see the above comment.
$assertions[] = 'in-array-' . $value_type->getId();
}
}
}

View File

@ -211,6 +211,18 @@ class InArrayTest extends \Psalm\Tests\TestCase
[],
'8.0'
],
'in_array-keyed-array-string-twice' => [
'<?php
function contains(string $a, string $b, mixed $element): void
{
if (in_array($element, [$a], true)) {
} elseif (in_array($element, [$b], true)) {
}
}',
[],
[],
'8.0'
],
];
}
@ -380,6 +392,28 @@ class InArrayTest extends \Psalm\Tests\TestCase
'error_message' => 'MixedReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Could not infer a return type',
'error_level' => ['RedundantConditionGivenDocblockType'],
],
'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',
],
];
}
}

View File

@ -972,28 +972,6 @@ class ValueTest extends \Psalm\Tests\TestCase
}',
'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;