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:
commit
b710aab148
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user