Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also support non-literal types in strict in_array() check #6419

Merged
merged 1 commit into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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