Skip to content

Commit

Permalink
Merge pull request #6419 from TysonAndre/in_array-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Sep 4, 2021
2 parents 3a3fde3 + 58e7ae1 commit b710aab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3442,7 +3442,7 @@ private static function getInarrayAssertions(
$assertions = [];

if (!$is_sealed) {
if ($value_type->getId() !== '') {
if ($value_type->getId() !== '' && !$value_type->isMixed()) {
$assertions[] = 'in-array-' . $value_type->getId();
}
} else {
Expand All @@ -3453,10 +3453,7 @@ private static function getInarrayAssertions(
|| $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();
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/TypeReconciliation/InArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ function assertInArray(string|bool $x, $y) {
[],
'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'
],
];
}

Expand Down
33 changes: 33 additions & 0 deletions tests/TypeReconciliation/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,39 @@ public function providerInvalidCodeParse(): iterable
}',
'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;
Expand Down

0 comments on commit b710aab

Please sign in to comment.