Skip to content
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
5 changes: 5 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public function findSpecifiedType(

return null;
}

if (!$isNeedleSupertype->no()) {
// Array might be empty, so in_array can return false
return null;
}
}

if (!$haystackType instanceof ConstantArrayType || count($haystackType->getValueTypes()) > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,4 +1195,16 @@ public function testBug13566(): void
]);
}

public function testBug13799(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-13799.php'], [
[
'Call to function in_array() with arguments \'c\', list<\'a\'|\'b\'> and true will always evaluate to false.',
24,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13799.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug13799;

/**
* @phpstan-impure
* @return list<'a'|'b'>
*/
function get_whitelist(): array {
$s = [];
if (rand(0, 1)) {
$s[] = 'a';
}
if (rand(0, 1)) {
$s[] = 'b';
}
return $s;
}

if (in_array('a', get_whitelist(), true)) {
echo 'ok';
}

if (in_array('c', get_whitelist(), true)) {
echo 'ok';
}
Loading